Sync current user with auth login/logout

This commit is contained in:
Kimmo Puputti 2017-04-20 14:22:01 +03:00
parent 30f9909b45
commit f2dd3c9105
3 changed files with 26 additions and 5 deletions

View file

@ -1,3 +1,5 @@
import { clearCurrentUser, fetchCurrentUser } from './user.duck';
const authenticated = authInfo => authInfo.grantType === 'refresh_token';
// ================ Action types ================ //
@ -98,6 +100,7 @@ export const login = (username, password) =>
// just dispatches the login error action.
return sdk
.login({ username, password })
.then(() => dispatch(fetchCurrentUser()))
.then(() => dispatch(loginSuccess()))
.catch(e => dispatch(loginError(e)));
};
@ -111,5 +114,9 @@ export const logout = () =>
// Not that the thunk does not reject when the logout fails, it
// just dispatches the logout error action.
return sdk.logout().then(() => dispatch(logoutSuccess())).catch(e => dispatch(logoutError(e)));
return sdk
.logout()
.then(() => dispatch(clearCurrentUser()))
.then(() => dispatch(logoutSuccess()))
.catch(e => dispatch(logoutError(e)));
};

View file

@ -1,5 +1,4 @@
import { call, put, take, fork, cancel } from 'redux-saga/effects';
import { createMockTask } from 'redux-saga/utils';
import { clearCurrentUser } from './user.duck';
import reducer, {
LOGIN_REQUEST,
LOGOUT_REQUEST,
@ -104,7 +103,11 @@ describe('Auth duck', () => {
return login(username, password)(dispatch, getState, sdk).then(() => {
expect(sdk.login.mock.calls).toEqual([[{ username, password }]]);
expect(dispatch.mock.calls).toEqual([[loginRequest()], [loginSuccess()]]);
expect(dispatch.mock.calls).toEqual([
[loginRequest()],
[expect.anything()], // fetchCurrentUser
[loginSuccess()],
]);
});
});
it('should dispatch error', () => {
@ -172,7 +175,11 @@ describe('Auth duck', () => {
return logout()(dispatch, getState, sdk).then(() => {
expect(sdk.logout.mock.calls.length).toEqual(1);
expect(dispatch.mock.calls).toEqual([[logoutRequest()], [logoutSuccess()]]);
expect(dispatch.mock.calls).toEqual([
[logoutRequest()],
[clearCurrentUser()],
[logoutSuccess()],
]);
});
});
it('should dispatch error', () => {

View file

@ -4,6 +4,8 @@ export const USERS_ME_REQUEST = 'app/user/USERS_ME_REQUEST';
export const USERS_ME_SUCCESS = 'app/user/USERS_ME_SUCCESS';
export const USERS_ME_ERROR = 'app/user/USERS_ME_ERROR';
export const CLEAR_CURRENT_USER = 'app/user/CLEAR_CURRENT_USER';
export const STRIPE_ACCOUNT_CREATE_REQUEST = 'app/user/STRIPE_ACCOUNT_CREATE_REQUEST';
export const STRIPE_ACCOUNT_CREATE_SUCCESS = 'app/user/STRIPE_ACCOUNT_CREATE_SUCCESS';
export const STRIPE_ACCOUNT_CREATE_ERROR = 'app/user/STRIPE_ACCOUNT_CREATE_ERROR';
@ -27,6 +29,9 @@ export default function reducer(state = initialState, action = {}) {
console.error(payload);
return { ...state, usersMeError: payload };
case CLEAR_CURRENT_USER:
return { ...state, currentUser: null, usersMeError: null };
default:
return state;
}
@ -47,6 +52,8 @@ export const usersMeError = e => ({
error: true,
});
export const clearCurrentUser = () => ({ type: CLEAR_CURRENT_USER });
export const stripeAccountCreateRequest = () => ({ type: STRIPE_ACCOUNT_CREATE_REQUEST });
export const stripeAccountCreateSuccess = response => ({