Use token in storage state, not in localStorage
authorymh <ymh.work@gmail.com>
Mon, 19 Jun 2017 18:23:26 +0200
changeset 56 96543c395baa
parent 55 a2761c5be551
child 57 2e4e9f9ebc4f
Use token in storage state, not in localStorage
client/src/APIClient.js
client/src/sagas/index.js
client/src/store/configureStore.js
--- a/client/src/APIClient.js	Mon Jun 19 17:56:43 2017 +0200
+++ b/client/src/APIClient.js	Mon Jun 19 18:23:26 2017 +0200
@@ -1,6 +1,7 @@
 class APIClient {
-  constructor(baseURL) {
+  constructor(baseURL, store) {
     this.baseURL = baseURL;
+    this.store = store;
   }
 
   createRequest = (method, uri, data, headers) => {
@@ -21,8 +22,14 @@
     return new Request(this.baseURL + uri, options);
   }
 
+  getToken = () => {
+    const state = this.store.getState();
+    return state.get('token');
+  }
+
   hasToken = () => {
-    const token = localStorage.getItem('token');
+    //const token = localStorage.getItem('token');
+    const token = this.getToken();
 
     return token !== null;
   }
@@ -30,7 +37,8 @@
   createAuthorizedRequest = (method, uri, data) => {
 
     var headers = new Headers(),
-        token = localStorage.getItem('token') || '';
+        //token = localStorage.getItem('token') || '';
+        token = this.getToken() || '';
     headers.append("Authorization", "JWT " + token);
 
     return this.createRequest(method, uri, data, headers);
--- a/client/src/sagas/index.js	Mon Jun 19 17:56:43 2017 +0200
+++ b/client/src/sagas/index.js	Mon Jun 19 18:23:26 2017 +0200
@@ -120,7 +120,7 @@
           token: response.token,
         }];
 
-        yield actions.map(action => put(action))
+        yield all(actions.map(action => put(action)))
 
     } catch(e) {
         yield put({ type: types.AUTH_LOGIN_ERROR, error: e });
--- a/client/src/store/configureStore.js	Mon Jun 19 17:56:43 2017 +0200
+++ b/client/src/store/configureStore.js	Mon Jun 19 18:23:26 2017 +0200
@@ -40,7 +40,7 @@
   ));
 
   const context = {
-      client: new APIClient(config.apiRootUrl)
+      client: new APIClient(config.apiRootUrl, store)
   }
 
   saga.run(rootSaga, context);