Store token & user.
--- a/client/src/constants/actionTypes.js Mon Jun 19 11:56:06 2017 +0200
+++ b/client/src/constants/actionTypes.js Mon Jun 19 12:32:11 2017 +0200
@@ -14,3 +14,5 @@
export const AUTH_LOGIN_REQUEST = 'AUTH_LOGIN_REQUEST';
export const AUTH_LOGIN_SUCCESS = 'AUTH_LOGIN_SUCCESS';
export const AUTH_LOGIN_ERROR = 'AUTH_LOGIN_ERROR';
+export const AUTH_STORE_TOKEN_ASYNC = 'AUTH_STORE_TOKEN_ASYNC';
+export const AUTH_STORE_TOKEN = 'AUTH_STORE_TOKEN';
--- a/client/src/reducers/authReducer.js Mon Jun 19 11:56:06 2017 +0200
+++ b/client/src/reducers/authReducer.js Mon Jun 19 12:32:11 2017 +0200
@@ -18,8 +18,7 @@
export const token = (state = null, action) => {
switch (action.type) {
- case types.AUTH_LOGIN_SUCCESS:
- console.log('token', action.token)
+ case types.AUTH_STORE_TOKEN:
return action.token;
default:
return state;
--- 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(),
])
}
--- a/client/src/store/configureStore.js Mon Jun 19 11:56:06 2017 +0200
+++ b/client/src/store/configureStore.js Mon Jun 19 12:32:11 2017 +0200
@@ -7,13 +7,16 @@
import createSagaMiddleware from 'redux-saga'
import Immutable from 'immutable';
+const token = localStorage.getItem('token');
+const currentUser = localStorage.getItem('currentUser');
+
const defaultState = {
currentSession: null,
sessions: Immutable.List([]),
notes: Immutable.List([]),
- isAuthenticated: false,
- currentUser: null,
- token: null,
+ isAuthenticated: token !== null,
+ currentUser: currentUser ? JSON.parse(currentUser) : null,
+ token: token,
login: {
loading: false,
success: false,