mirror of
https://github.com/elyby/accounts-frontend.git
synced 2025-02-20 01:08:37 +05:30
#181: fix logout for guests. Increase logout speed
This commit is contained in:
parent
3d8c646a94
commit
322325d4ad
@ -49,14 +49,24 @@ export function setUser(payload) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function logout() {
|
export function logout() {
|
||||||
return (dispatch, getState) =>
|
return (dispatch, getState) => {
|
||||||
authentication.logout().then(() => {
|
if (getState().user.token) {
|
||||||
dispatch(setUser({
|
authentication.logout();
|
||||||
lang: getState().user.lang,
|
}
|
||||||
isGuest: true
|
|
||||||
}));
|
return new Promise((resolve) => {
|
||||||
dispatch(routeActions.push('/login'));
|
setTimeout(() => { // a tiny timeout to allow logout before user's token will be removed
|
||||||
|
dispatch(setUser({
|
||||||
|
lang: getState().user.lang,
|
||||||
|
isGuest: true
|
||||||
|
}));
|
||||||
|
|
||||||
|
dispatch(routeActions.push('/login'));
|
||||||
|
|
||||||
|
resolve();
|
||||||
|
}, 0);
|
||||||
});
|
});
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function fetchUserData() {
|
export function fetchUserData() {
|
||||||
|
@ -34,62 +34,83 @@ describe('components/user/actions', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('#logout()', () => {
|
describe('#logout()', () => {
|
||||||
it('should post to /api/authentication/logout with user jwt', () => {
|
beforeEach(() => {
|
||||||
getState.returns({
|
|
||||||
user: {
|
|
||||||
lang: 'foo'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
request.post.returns(new Promise((resolve) => {
|
|
||||||
setTimeout(() => {
|
|
||||||
// we must not overwrite user's token till request starts
|
|
||||||
expect(dispatch, 'was not called');
|
|
||||||
|
|
||||||
resolve();
|
|
||||||
}, 0);
|
|
||||||
}));
|
|
||||||
|
|
||||||
return callThunk(logout).then(() => {
|
|
||||||
expect(request.post, 'to have a call satisfying', [
|
|
||||||
'/api/authentication/logout'
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should change user to guest', () => {
|
|
||||||
getState.returns({
|
|
||||||
user: {
|
|
||||||
lang: 'foo'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
request.post.returns(Promise.resolve());
|
request.post.returns(Promise.resolve());
|
||||||
|
|
||||||
return callThunk(logout).then(() => {
|
|
||||||
expect(dispatch, 'to have a call satisfying', [
|
|
||||||
setUser({
|
|
||||||
lang: 'foo',
|
|
||||||
isGuest: true
|
|
||||||
})
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should redirect to /login', () => {
|
describe('user with jwt', () => {
|
||||||
getState.returns({
|
beforeEach(() => {
|
||||||
user: {
|
getState.returns({
|
||||||
lang: 'foo'
|
user: {
|
||||||
}
|
token: 'iLoveRockNRoll',
|
||||||
|
lang: 'foo'
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
request.post.returns(Promise.resolve());
|
it('should post to /api/authentication/logout with user jwt', () => {
|
||||||
|
// TODO: need an integration test with a middleware, because this
|
||||||
|
// test is not reliable to check, whether logout request will have token inside
|
||||||
|
request.post.returns(new Promise((resolve) => {
|
||||||
|
setTimeout(() => {
|
||||||
|
// we must not overwrite user's token till request starts
|
||||||
|
expect(dispatch, 'was not called');
|
||||||
|
|
||||||
return callThunk(logout).then(() => {
|
resolve();
|
||||||
expect(dispatch, 'to have a call satisfying', [
|
}, 0);
|
||||||
routeActions.push('/login')
|
}));
|
||||||
]);
|
|
||||||
|
return callThunk(logout).then(() => {
|
||||||
|
expect(request.post, 'to have a call satisfying', [
|
||||||
|
'/api/authentication/logout'
|
||||||
|
]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testChangedToGuest();
|
||||||
|
testRedirectedToLogin();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('user without jwt', () => { // (a guest with partially filled user's state)
|
||||||
|
beforeEach(() => {
|
||||||
|
getState.returns({
|
||||||
|
user: {
|
||||||
|
lang: 'foo'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not post to /api/authentication/logout', () =>
|
||||||
|
callThunk(logout).then(() => {
|
||||||
|
expect(request.post, 'was not called');
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
testChangedToGuest();
|
||||||
|
testRedirectedToLogin();
|
||||||
|
});
|
||||||
|
|
||||||
|
function testChangedToGuest() {
|
||||||
|
it('should change user to guest', () =>
|
||||||
|
callThunk(logout).then(() => {
|
||||||
|
expect(dispatch, 'to have a call satisfying', [
|
||||||
|
setUser({
|
||||||
|
lang: 'foo',
|
||||||
|
isGuest: true
|
||||||
|
})
|
||||||
|
]);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testRedirectedToLogin() {
|
||||||
|
it('should redirect to /login', () =>
|
||||||
|
callThunk(logout).then(() => {
|
||||||
|
expect(dispatch, 'to have a call satisfying', [
|
||||||
|
routeActions.push('/login')
|
||||||
|
]);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user