diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-02-13 23:08:50 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-02-13 23:08:50 +0900 |
| commit | 470b7235b80d082009ad350e2b33ef6637209e02 (patch) | |
| tree | 60ffe938a4051255ea0d6b35001be50c28b76497 /frontend/app/hooks/useAuth.ts | |
| parent | 482c3a52a0fcc5870a7db4a190475caf61b211a3 (diff) | |
| parent | 6c30f383a65cb000d66a85cadc96253ce7061942 (diff) | |
| download | phperkaigi-2026-albatross-470b7235b80d082009ad350e2b33ef6637209e02.tar.gz phperkaigi-2026-albatross-470b7235b80d082009ad350e2b33ef6637209e02.tar.zst phperkaigi-2026-albatross-470b7235b80d082009ad350e2b33ef6637209e02.zip | |
Merge branch 'feat/frontend-rearchitecture'
Diffstat (limited to 'frontend/app/hooks/useAuth.ts')
| -rw-r--r-- | frontend/app/hooks/useAuth.ts | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/frontend/app/hooks/useAuth.ts b/frontend/app/hooks/useAuth.ts new file mode 100644 index 0000000..8762734 --- /dev/null +++ b/frontend/app/hooks/useAuth.ts @@ -0,0 +1,58 @@ +import { useCallback, useSyncExternalStore } from "react"; +import { apiLogin } from "../api/client"; +import { + type User, + clearToken, + getToken, + getUserFromToken, + isTokenExpired, + setToken, +} from "../auth"; + +// Simple external store to trigger re-renders when auth state changes. +let authVersion = 0; +const listeners = new Set<() => void>(); + +function subscribe(callback: () => void) { + listeners.add(callback); + return () => listeners.delete(callback); +} + +function getSnapshot() { + return authVersion; +} + +function notifyAuthChange() { + authVersion++; + for (const listener of listeners) { + listener(); + } +} + +export function useAuth(): { + user: User | null; + token: string | null; + isLoggedIn: boolean; + login: (username: string, password: string) => Promise<void>; + logout: () => void; +} { + useSyncExternalStore(subscribe, getSnapshot); + + const token = getToken(); + const isExpired = isTokenExpired(); + const user = isExpired ? null : getUserFromToken(); + const isLoggedIn = user !== null && !isExpired; + + const login = useCallback(async (username: string, password: string) => { + const { token } = await apiLogin(username, password); + setToken(token); + notifyAuthChange(); + }, []); + + const logout = useCallback(() => { + clearToken(); + notifyAuthChange(); + }, []); + + return { user, token: isLoggedIn ? token : null, isLoggedIn, login, logout }; +} |
