forked from invoke-ai/InvokeAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthSlice.ts
More file actions
97 lines (87 loc) · 3.09 KB
/
authSlice.ts
File metadata and controls
97 lines (87 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import type { PayloadAction } from '@reduxjs/toolkit';
import { createSlice } from '@reduxjs/toolkit';
import type { SliceConfig } from 'app/store/types';
import { z } from 'zod';
const zUser = z.object({
user_id: z.string(),
email: z.string(),
display_name: z.string().nullable(),
is_admin: z.boolean(),
is_active: z.boolean(),
});
const zAuthState = z.object({
isAuthenticated: z.boolean(),
token: z.string().nullable(),
user: zUser.nullable(),
isLoading: z.boolean(),
sessionExpired: z.boolean(),
});
type User = z.infer<typeof zUser>;
type AuthState = z.infer<typeof zAuthState>;
// Helper to safely access localStorage (not available in test environment)
const getStoredAuthToken = (): string | null => {
if (typeof window !== 'undefined' && window.localStorage) {
return localStorage.getItem('auth_token');
}
return null;
};
const initialState: AuthState = {
isAuthenticated: !!getStoredAuthToken(),
token: getStoredAuthToken(),
user: null,
isLoading: false,
sessionExpired: false,
};
const getInitialAuthState = (): AuthState => initialState;
const authSlice = createSlice({
name: 'auth',
initialState,
reducers: {
setCredentials: (state, action: PayloadAction<{ token: string; user: User }>) => {
state.token = action.payload.token;
state.user = action.payload.user;
state.isAuthenticated = true;
state.sessionExpired = false;
if (typeof window !== 'undefined' && window.localStorage) {
localStorage.setItem('auth_token', action.payload.token);
}
},
logout: (state) => {
state.token = null;
state.user = null;
state.isAuthenticated = false;
state.sessionExpired = false;
if (typeof window !== 'undefined' && window.localStorage) {
localStorage.removeItem('auth_token');
}
},
sessionExpiredLogout: (state) => {
state.token = null;
state.user = null;
state.isAuthenticated = false;
state.sessionExpired = true;
if (typeof window !== 'undefined' && window.localStorage) {
localStorage.removeItem('auth_token');
}
},
setLoading: (state, action: PayloadAction<boolean>) => {
state.isLoading = action.payload;
},
},
});
export const { setCredentials, logout, sessionExpiredLogout, setLoading } = authSlice.actions;
export const authSliceConfig: SliceConfig<typeof authSlice> = {
slice: authSlice,
schema: zAuthState,
getInitialState: getInitialAuthState,
persistConfig: {
migrate: () => getInitialAuthState(),
// Don't persist auth state - token is stored in localStorage
persistDenylist: ['isAuthenticated', 'token', 'user', 'isLoading', 'sessionExpired'],
},
};
export const selectIsAuthenticated = (state: { auth: AuthState }) => state.auth.isAuthenticated;
export const selectCurrentUser = (state: { auth: AuthState }) => state.auth.user;
export const selectAuthToken = (state: { auth: AuthState }) => state.auth.token;
export const selectIsAuthLoading = (state: { auth: AuthState }) => state.auth.isLoading;
export const selectSessionExpired = (state: { auth: AuthState }) => state.auth.sessionExpired;