import { createContext, useContext } from 'react'; import type { User } from '@/features/auth/services/types'; /** * 'unreachable': the start-up refresh could not get a verdict (rate limit, restart, network) after the quiet retries. * The session is neither confirmed nor ended - the refresh cookie is untouched - so the app offers a retry * instead of treating the visitor as signed out. */ export type AuthStatus = 'loading' | 'authenticated' | 'guest' | 'unreachable'; export interface AuthContextValue { status: AuthStatus; user: User | null; /** Access token, kept only in memory (BR-U1-02). */ accessToken: string | null; expiresAt: string | null; isAuthenticated: boolean; login: (email: string, password: string) => Promise; logout: () => Promise; /** * Cookie-based silent refresh, one at a time (callers share the running one). Resolves to the new token, to * null when the server rejected the session (it is then cleared), and rejects on a transient failure (429, 5xx, * network) with the session left as it was. */ refresh: () => Promise; } export const AuthContext = createContext(null); export function useAuth(): AuthContextValue { const ctx = useContext(AuthContext); if (ctx === null) { throw new Error('useAuth must be used within an AuthProvider'); } return ctx; }