aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/client/stores/auth.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/stores/auth.tsx')
-rw-r--r--src/client/stores/auth.tsx24
1 files changed, 17 insertions, 7 deletions
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,