Skip to content

Commit 2b72881

Browse files
committed
add revalidate
1 parent 33668a5 commit 2b72881

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { jwtDecode } from 'jwt-decode';
22

33
import type { AuthSlice, SliceCreator } from '../types';
44

5-
export const createAuthSlice: SliceCreator<AuthSlice> = (set) => ({
5+
export const createAuthSlice: SliceCreator<AuthSlice> = (set, get) => ({
66
auth: null,
77
login: (accessToken) => {
88
set((state) => {
@@ -11,5 +11,13 @@ export const createAuthSlice: SliceCreator<AuthSlice> = (set) => ({
1111
payload: jwtDecode(accessToken)
1212
};
1313
});
14+
},
15+
revalidateToken: () => {
16+
const { auth } = get();
17+
if (auth?.payload.exp && Date.now() / 1000 > auth.payload.exp) {
18+
set((state) => {
19+
state.auth = null;
20+
});
21+
}
1422
}
1523
});

apps/playground/src/store/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export type AuthSlice = {
4141
payload: JwtPayload;
4242
};
4343
login: (accessToken: string) => void;
44+
revalidateToken: () => void;
4445
};
4546

4647
export type EditorState = {

0 commit comments

Comments
 (0)