Skip to content

Commit 0564a19

Browse files
authored
Merge pull request #76 from js-accounts/feat/redux-store-stokens
feat(redux-tokens): expose js-accounts tokens to redux store
2 parents 6004d34 + 840bab8 commit 0564a19

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

packages/client/src/AccountsClient.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {
1515
} from '@accounts/common';
1616
import config from './config';
1717
import createStore from './createStore';
18-
import reducer, { loggingIn, setUser, clearUser } from './module';
18+
import reducer, { loggingIn, setUser, clearUser, setTokens, clearTokens as clearStoreTokens } from './module';
1919
import type { TransportInterface } from './TransportInterface';
2020

2121
const isValidUserObject = (user: PasswordLoginUserIdentityType) => has(user, 'user') || has(user, 'email') || has(user, 'id');
@@ -94,6 +94,7 @@ export class AccountsClient {
9494
}
9595

9696
async clearTokens(): Promise<void> {
97+
this.store.dispatch(clearStoreTokens());
9798
await this.removeStorageData(getTokenKey(ACCESS_TOKEN, this.options));
9899
await this.removeStorageData(getTokenKey(REFRESH_TOKEN, this.options));
99100
}
@@ -135,6 +136,7 @@ export class AccountsClient {
135136
await this.transport.refreshTokens(accessToken, refreshToken);
136137

137138
await this.storeTokens(refreshedSession);
139+
this.store.dispatch(setTokens(refreshedSession.tokens));
138140
this.store.dispatch(setUser(refreshedSession.user));
139141
}
140142
} catch (err) {
@@ -198,6 +200,7 @@ export class AccountsClient {
198200
const res : LoginReturnType = await this.transport.loginWithPassword(user, password);
199201
this.store.dispatch(loggingIn(false));
200202
await this.storeTokens(res);
203+
this.store.dispatch(setTokens(res.tokens));
201204
this.store.dispatch(setUser(res.user));
202205
this.options.onSignedInHook();
203206
if (callback && isFunction(callback)) {

packages/client/src/AccountsClient.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,16 @@ describe('Accounts', () => {
305305
...loggedInUser.user,
306306
}));
307307
});
308+
it('stores tokens in redux', async () => {
309+
const transport = {
310+
loginWithPassword: () => Promise.resolve(loggedInUser),
311+
};
312+
Accounts.config({ history }, transport);
313+
await Accounts.loginWithPassword('username', 'password');
314+
expect(Accounts.instance.getState().get('tokens')).toEqual(Map({
315+
...loggedInUser.tokens,
316+
}));
317+
});
308318
});
309319
describe('logout', () => {
310320
it('calls callback on successful logout', async () => {
@@ -342,6 +352,35 @@ describe('Accounts', () => {
342352
expect(callback.mock.calls[0][0]).toEqual({ message: 'error message' });
343353
}
344354
});
355+
356+
it('clear tokens in redux', async () => {
357+
const transport = {
358+
logout: () => Promise.reject({ message: 'error message' }),
359+
};
360+
Accounts.instance.storeTokens({ tokens: { accessToken: '1' } });
361+
Accounts.config({ history }, transport);
362+
const callback = jest.fn();
363+
try {
364+
await Accounts.logout(callback);
365+
throw new Error();
366+
} catch (err) {
367+
expect(Accounts.instance.getState().get('tokens')).toEqual(null);
368+
}
369+
});
370+
it('clear user in redux', async () => {
371+
const transport = {
372+
logout: () => Promise.reject({ message: 'error message' }),
373+
};
374+
Accounts.instance.storeTokens({ tokens: { accessToken: '1' } });
375+
Accounts.config({ history }, transport);
376+
const callback = jest.fn();
377+
try {
378+
await Accounts.logout(callback);
379+
throw new Error();
380+
} catch (err) {
381+
expect(Accounts.instance.getState().get('user')).toEqual(null);
382+
}
383+
});
345384
});
346385
describe('refreshSession', async () => {
347386
// TODO test that user and tokens are cleared if refreshToken is expired

packages/client/src/module.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ import { Map } from 'immutable';
33
const PATH = 'js-accounts/';
44
const LOGIN = `${PATH}LOGIN`;
55
const SET_USER = `${PATH}SET_USER`;
6+
const SET_TOKENS = `${PATH}SET_TOKENS`;
7+
const CLEAR_TOKENS = `${PATH}CLEAR_TOKENS`;
68
const CLEAR_USER = `${PATH}CLEAR_USER`;
79
const LOGGING_IN = `${PATH}LOGGING_IN`;
810

911
const initialState = Map({
1012
isLoading: false,
1113
user: null,
14+
tokens: null,
1215
loggingIn: false,
1316
});
1417

@@ -22,6 +25,13 @@ const reducer = (state = initialState, action) => {
2225
const { user } = action.payload;
2326
return state.set('user', Map(user));
2427
}
28+
case SET_TOKENS: {
29+
const { tokens } = action.payload;
30+
return state.set('tokens', Map(tokens));
31+
}
32+
case CLEAR_TOKENS: {
33+
return state.set('tokens', null);
34+
}
2535
case CLEAR_USER: {
2636
return state.set('user', null);
2737
}
@@ -51,6 +61,17 @@ export const setUser = user => ({
5161
},
5262
});
5363

64+
export const setTokens = tokens => ({
65+
type: SET_TOKENS,
66+
payload: {
67+
tokens,
68+
},
69+
});
70+
71+
export const clearTokens = () => ({
72+
type: CLEAR_TOKENS,
73+
});
74+
5475
export const clearUser = () => ({
5576
type: CLEAR_USER,
5677
});

0 commit comments

Comments
 (0)