diff options
Diffstat (limited to 'src/client/stores')
| -rw-r--r-- | src/client/stores/auth.test.tsx | 1 | ||||
| -rw-r--r-- | src/client/stores/auth.tsx | 24 |
2 files changed, 18 insertions, 7 deletions
diff --git a/src/client/stores/auth.test.tsx b/src/client/stores/auth.test.tsx index 72ab9e3..1769011 100644 --- a/src/client/stores/auth.test.tsx +++ b/src/client/stores/auth.test.tsx @@ -14,6 +14,7 @@ vi.mock("../api/client", () => ({ logout: vi.fn(), isAuthenticated: vi.fn(), getTokens: vi.fn(), + onSessionExpired: vi.fn(() => vi.fn()), }, ApiClientError: class ApiClientError extends Error { constructor( diff --git a/src/client/stores/auth.tsx b/src/client/stores/auth.tsx index b34717b..3f2681b 100644 --- a/src/client/stores/auth.tsx +++ b/src/client/stores/auth.tsx @@ -31,6 +31,15 @@ export interface AuthProviderProps { export function AuthProvider({ children }: AuthProviderProps) { const [user, setUser] = useState<User | null>(null); const [isLoading, setIsLoading] = useState(true); + const [isAuthenticated, setIsAuthenticated] = useState( + apiClient.isAuthenticated(), + ); + + const logout = useCallback(() => { + apiClient.logout(); + setUser(null); + setIsAuthenticated(false); + }, []); // Check for existing auth on mount useEffect(() => { @@ -45,18 +54,19 @@ export function AuthProvider({ children }: AuthProviderProps) { } }, []); + // Subscribe to session expired events from the API client + useEffect(() => { + return apiClient.onSessionExpired(() => { + logout(); + }); + }, [logout]); + const login = useCallback(async (username: string, password: string) => { const response = await apiClient.login(username, password); setUser(response.user); + setIsAuthenticated(true); }, []); - const logout = useCallback(() => { - apiClient.logout(); - setUser(null); - }, []); - - const isAuthenticated = apiClient.isAuthenticated(); - const value = useMemo<AuthContextValue>( () => ({ user, |
