Skip to content

Commit b2389ce

Browse files
committed
set currentUser and currentGroup correctly in tests
1 parent 4d329a3 commit b2389ce

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

apps/web/src/store/slices/auth.slice.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,36 @@ import { jwtDecode } from 'jwt-decode';
55

66
import type { AuthSlice, SliceCreator } from '../types';
77

8+
const parseAccessToken = (accessToken: string) => {
9+
const { groups, permissions, ...rest } = jwtDecode<TokenPayload>(accessToken);
10+
const ability = createMongoAbility<PureAbility<[AppAction, AppSubjectName], any>>(permissions);
11+
return {
12+
currentGroup: groups[0],
13+
currentUser: {
14+
ability,
15+
groups,
16+
...rest
17+
}
18+
};
19+
};
20+
821
export const createAuthSlice: SliceCreator<AuthSlice> = (set) => {
22+
const accessToken = window.__PLAYWRIGHT_ACCESS_TOKEN__ ?? null;
23+
const initialState = accessToken ? parseAccessToken(accessToken) : null;
24+
925
return {
10-
accessToken: window.__PLAYWRIGHT_ACCESS_TOKEN__ ?? null,
26+
accessToken,
1127
changeGroup: (group) => {
1228
set({ currentGroup: group, currentSession: null });
1329
},
14-
currentGroup: null,
15-
currentUser: null,
30+
currentGroup: initialState?.currentGroup ?? null,
31+
currentUser: initialState?.currentUser ?? null,
1632
login: (accessToken) => {
17-
const { groups, permissions, ...rest } = jwtDecode<TokenPayload>(accessToken);
18-
const ability = createMongoAbility<PureAbility<[AppAction, AppSubjectName], any>>(permissions);
33+
const { currentGroup, currentUser } = parseAccessToken(accessToken);
1934
set({
2035
accessToken,
21-
currentGroup: groups[0],
22-
currentUser: { ability, groups, ...rest }
36+
currentGroup,
37+
currentUser
2338
});
2439
},
2540
logout: () => {

0 commit comments

Comments
 (0)