From c548b3bd3f0de1a1a74606d60ef9fdd323792918 Mon Sep 17 00:00:00 2001
From: Jules Laplace <jules@okfoc.us>
Date: Sat, 18 Mar 2017 21:42:30 +0100
Subject: user authentication in browser

---
 client/components/App.jsx | 56 ++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 51 insertions(+), 5 deletions(-)

(limited to 'client/components/App.jsx')

diff --git a/client/components/App.jsx b/client/components/App.jsx
index f7cfa97..5a026b4 100644
--- a/client/components/App.jsx
+++ b/client/components/App.jsx
@@ -1,10 +1,56 @@
-import React from 'react';
+import React from 'react'
+import LoggedOutView from './LoggedOutView.jsx'
+import CalorieView from './CalorieView.jsx'
+
+import feathers from 'feathers/client'
+import feathersHooks from 'feathers-hooks'
+import feathersRest from 'feathers-rest/client'
+import feathersAuthentication from 'feathers-authentication/client'
+import superagent from 'superagent'
+
+const rest = feathersRest(window.location.origin)
+
+const client = feathers()
+              .configure(feathersHooks())
+              .configure(feathersAuthentication({ storage: localStorage }))
+              .configure(rest.superagent(superagent))
 
 export default class App extends React.Component {
+  constructor() {
+    super()
+    this.state = {
+      ready: true,
+      loggedIn: false,
+      user: {},
+    }
+    client.authenticate()
+      .then(user => {
+        console.log(user)
+        this.setState({ ready: true, loggedIn: true, user: user })
+      })
+      .catch(error => {
+        this.setState({ ready: true })
+        console.error(error)
+      })
+  }
   render() {
-    return (
-     <div style={{textAlign: 'center'}}>
-        <h1>Hello World</h1>
-      </div>);
+    if (this.state.ready) {
+      if (this.state.loggedIn) {
+        return (
+          <CalorieView client={client} />
+        )
+      }
+      else {
+        return (
+          <LoggedOutView client={client} />
+        )
+      }
+    }
+    else {
+      return (
+        <div>LOADING...</div>
+      )
+    }
   }
 }
+
-- 
cgit v1.2.3-70-g09d2

