File
Blob: src/client/hooks/use-auth.ts
| 1 | import { useCallback } from "react"; |
| 2 | import { useAuthStore, selectIsAuthenticated } from "@/client/stores/auth-store"; |
| 3 | import { resetWorkspaceLocalOwner } from "@/client/stores/bootstrap"; |
| 4 | import { api } from "@/client/lib/api"; |
| 5 | |
| 6 | export function useAuth() { |
| 7 | const isAuthenticated = useAuthStore(selectIsAuthenticated); |
| 8 | const user = useAuthStore((s) => s.user); |
| 9 | const clearAuth = useAuthStore((s) => s.clearAuth); |
| 10 | |
| 11 | const logout = useCallback(async () => { |
| 12 | try { |
| 13 | await api.auth.logout(); |
| 14 | } catch { |
| 15 | // Ignore errors - clear local state regardless |
| 16 | } |
| 17 | clearAuth(); |
| 18 | await resetWorkspaceLocalOwner(); |
| 19 | }, [clearAuth]); |
| 20 | |
| 21 | return { isAuthenticated, user, logout }; |
| 22 | } |