mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 08:55:24 +00:00
Bug - authenticate on refresh (#295)
* Saves user in state on login success * stop storing user token in state
This commit is contained in:
parent
5d0cac882a
commit
86d2319170
@ -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);
|
||||
|
@ -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 };
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user