client/src/sagas/index.js
changeset 47 64428c7ebc19
parent 44 3b20e2b584fe
child 53 d8588379529e
--- a/client/src/sagas/index.js	Mon Jun 19 11:56:06 2017 +0200
+++ b/client/src/sagas/index.js	Mon Jun 19 12:32:11 2017 +0200
@@ -101,9 +101,14 @@
 function* watchLoginRequest() {
   while (true) {
     try {
+
         const { username, password } = yield take(types.AUTH_LOGIN_REQUEST);
         const response = yield client.post('/api/auth/login/', { username, password });
-        yield put({
+
+        localStorage.setItem('currentUser', JSON.stringify(response.user));
+        localStorage.setItem('token', response.token);
+
+        const actions = [{
           type: types.AUTH_LOGIN_SUCCESS,
           user: response.user,
           token: response.token,
@@ -112,13 +117,26 @@
               pathname: '/sessions',
             }),
           },
-        });
+        }, {
+          type: types.AUTH_STORE_TOKEN_ASYNC,
+          token: response.token,
+        }];
+
+        yield actions.map(action => put(action))
+
     } catch(e) {
         yield put({ type: types.AUTH_LOGIN_ERROR, error: e });
     }
   }
 }
 
+function* watchStoreToken() {
+  while (true) {
+    const { token } = yield take(types.AUTH_STORE_TOKEN_ASYNC);
+    yield put({ type: types.AUTH_STORE_TOKEN, token });
+  }
+}
+
 // ---
 
 export default function* rootSaga() {
@@ -130,5 +148,6 @@
     watchUpdateSession(),
     watchLoginSubmit(),
     watchLoginRequest(),
+    watchStoreToken(),
   ])
 }