* Saves user in state on login success

* stop storing user token in state
This commit is contained in:
Mike Stone 2016-10-11 14:50:18 -04:00 коммит произвёл GitHub
Родитель 5d0cac882a
Коммит 86d2319170
3 изменённых файлов: 11 добавлений и 11 удалений

Просмотреть файл

@ -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,
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;