From 86d231917002ae64691431f9f64f4b57280dd0e5 Mon Sep 17 00:00:00 2001 From: Mike Stone Date: Tue, 11 Oct 2016 14:50:18 -0400 Subject: [PATCH] Bug - authenticate on refresh (#295) * Saves user in state on login success * stop storing user token in state --- frontend/redux/middlewares/auth.js | 2 +- frontend/redux/nodes/auth/actions.js | 13 ++++++++----- frontend/redux/nodes/auth/reducer.js | 7 ++----- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/frontend/redux/middlewares/auth.js b/frontend/redux/middlewares/auth.js index e84fe8268c..f9a473e14c 100644 --- a/frontend/redux/middlewares/auth.js +++ b/frontend/redux/middlewares/auth.js @@ -9,7 +9,7 @@ const authMiddleware = store => next => action => { const { type, payload } = action; if (type === LOGIN_SUCCESS) { - const { token } = payload.data; + const { token } = payload; if (token) { local.setItem('auth_token', token); diff --git a/frontend/redux/nodes/auth/actions.js b/frontend/redux/nodes/auth/actions.js index e5f90e8d94..a6657391bc 100644 --- a/frontend/redux/nodes/auth/actions.js +++ b/frontend/redux/nodes/auth/actions.js @@ -11,11 +11,12 @@ export const LOGOUT_FAILURE = 'LOGOUT_FAILURE'; export const clearAuthErrors = { type: CLEAR_AUTH_ERRORS }; export const loginRequest = { type: LOGIN_REQUEST }; -export const loginSuccess = (user) => { +export const loginSuccess = ({ user, token }) => { return { type: LOGIN_SUCCESS, payload: { - data: user, + user, + token, }, }; }; @@ -38,7 +39,7 @@ export const fetchCurrentUser = () => { const emailHash = md5(email.toLowerCase()); user.gravatarURL = `https://www.gravatar.com/avatar/${emailHash}`; - return dispatch(loginSuccess(user)); + return dispatch(loginSuccess({ user })); }) .catch(response => { dispatch(loginFailure('Unable to authenticate the current user')); @@ -59,7 +60,7 @@ export const loginUser = (formData) => { const emailHash = md5(email.toLowerCase()); user.gravatarURL = `https://www.gravatar.com/avatar/${emailHash}`; - dispatch(loginSuccess(response)); + dispatch(loginSuccess({ ...response, user })); return resolve(user); }) .catch(response => { @@ -74,7 +75,9 @@ export const loginUser = (formData) => { export const logoutFailure = (error) => { return { type: LOGOUT_FAILURE, - error, + payload: { + error, + }, }; }; export const logoutRequest = { type: LOGOUT_REQUEST }; diff --git a/frontend/redux/nodes/auth/reducer.js b/frontend/redux/nodes/auth/reducer.js index 87ba9f0480..fed4d760a4 100644 --- a/frontend/redux/nodes/auth/reducer.js +++ b/frontend/redux/nodes/auth/reducer.js @@ -12,7 +12,6 @@ export const initialState = { loading: false, error: null, user: null, - token: null, }; const reducer = (state = initialState, action) => { @@ -32,8 +31,7 @@ const reducer = (state = initialState, action) => { return { ...state, loading: false, - user: action.payload.data.user, - token: action.payload.data.token, + user: action.payload.user, }; case LOGIN_FAILURE: return { @@ -46,13 +44,12 @@ const reducer = (state = initialState, action) => { ...state, loading: false, user: null, - token: null, }; case LOGOUT_FAILURE: return { ...state, loading: false, - error: action.payload.data.error, + error: action.payload.error, }; default: return state;