"use client"; import React, { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react"; import { ApiError } from "@/lib/api"; import { AuthResponse, AuthUser, TwoFactorChallenge, clearToken, getDeviceToken, getToken, saveDeviceToken, saveToken } from "@/lib/auth"; import { apiV2, type V2Result, apiV2Data } from "@/lib/apiV2"; // A v2 "signed_in" answer as the session the app keeps (the user's fields plus the token). type V2Session = Extract["data"], { status: "signed_in" }>; /** A v2 sign-in answer as the app's AuthResponse (also used by the self-service kiosk). */ export const toAuthResponse = (s: V2Session): AuthResponse => ({ ...s.user, token: s.token, newsPromptPending: s.newsPromptPending, noticePromptPending: s.noticePromptPending, ...(s.deviceToken ? { deviceToken: s.deviceToken } : {}), }); export type AuthContextValue = { user: AuthUser | null; token: string | null; loading: boolean; login: (email: string, password: string) => Promise; verifyTwoFactor: (challengeId: string, code: string, rememberDevice: boolean) => Promise; resendTwoFactor: (challengeId: string) => Promise; register: (data: { name: string; email: string; password: string; phoneNumber?: string; notificationPreference?: "email" | "whatsapp" | "both"; newsOptIn?: boolean }) => Promise; logout: () => void; updateToken: (token: string) => void; }; const AuthContext = createContext(undefined); // The last profile the server confirmed, so a page reloaded with no connection (e.g. the // ticket scanner in offline mode) still knows who's signed in. The server checks the // token on every API call regardless, so this never grants anything by itself. const USER_CACHE_KEY = "hope_events_user_cache"; function cacheUser(u: AuthUser | null) { try { if (u) localStorage.setItem(USER_CACHE_KEY, JSON.stringify(u)); else localStorage.removeItem(USER_CACHE_KEY); } catch { /* storage unavailable */ } } function cachedUser(): AuthUser | null { try { const raw = localStorage.getItem(USER_CACHE_KEY); return raw ? JSON.parse(raw) : null; } catch { return null; } } export function AuthProvider({ children }: { children: React.ReactNode }) { const [token, setToken] = useState(null); const [user, setUser] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { const init = async () => { try { const t = getToken(); if (t) { // Validate the stored token by fetching the user profile. // Only update React state after a successful response so we never // hold a half-initialised (token set, user null) state. const me = await (apiV2Data("get", "/me", { authToken: t }) as Promise); setToken(t); setUser(me); cacheUser(me); } } catch (e) { // Only wipe the stored token when the server explicitly rejects it (401). // Transient failures (network down, 5xx) should NOT log the user out — the // token stays in localStorage so the next page load can retry. if (e instanceof ApiError && e.status === 401) { clearToken(); cacheUser(null); setToken(null); setUser(null); } else { // No connection: carry on as the last confirmed user. const t = getToken(); const cached = cachedUser(); if (t && cached) { setToken(t); setUser(cached); } } } finally { setLoading(false); } }; init(); }, []); const startSession = useCallback((res: AuthResponse) => { const { token, deviceToken, ...loggedInUser } = res; if (deviceToken) saveDeviceToken(deviceToken); saveToken(token); setToken(token); setUser(loggedInUser); cacheUser(loggedInUser); return loggedInUser; }, []); const login = useCallback(async (email: string, password: string) => { const { data } = await apiV2("post", "/auth/login", { body: { identifier: email, password, deviceToken: getDeviceToken() || undefined }, }); // 2FA accounts get a challenge back instead of a token — nothing is saved // until verifyTwoFactor exchanges the emailed/WhatsApp code for one. if (data.status === "code_required") { const challenge: TwoFactorChallenge = { twoFactorRequired: true, challengeId: data.challengeId, method: data.method, destination: data.destination || "", ...(data.enrolling ? { enrolling: true } : {}) }; return challenge; } return startSession(toAuthResponse(data)); }, [startSession]); const verifyTwoFactor = useCallback(async (challengeId: string, code: string, rememberDevice: boolean) => { const { data } = await apiV2("post", "/auth/login/code", { body: { challengeId, code, rememberDevice } }); return startSession(toAuthResponse(data)); }, [startSession]); const resendTwoFactor = useCallback((challengeId: string) => { return (apiV2Data("post", "/auth/login/code/resend", { body: { challengeId }, }) as Promise); }, []); const register = useCallback(async (data: { name: string; email: string; password: string; phoneNumber?: string; notificationPreference?: "email" | "whatsapp" | "both"; newsOptIn?: boolean }) => { // The sign-up form only submits once "I agree to the Terms of Use and Privacy Policy" is ticked. const { data: made } = await apiV2("post", "/auth/signup", { body: { name: data.name, email: data.email, password: data.password, acceptTerms: true, ...(data.phoneNumber ? { phone: data.phoneNumber } : {}), ...(data.notificationPreference ? { notificationPreference: data.notificationPreference } : {}), ...(data.newsOptIn !== undefined ? { newsOptIn: data.newsOptIn } : {}), }, }); const { token, ...registeredUser } = toAuthResponse(made); saveToken(token); setToken(token); setUser(registeredUser); }, []); const logout = useCallback(() => { clearToken(); cacheUser(null); setToken(null); setUser(null); }, []); // Some endpoints (profile update, password change, session revocation) issue a // fresh token mid-session. Persist it AND update in-memory state so the app // keeps using a valid token without requiring a page refresh. const updateToken = useCallback((t: string) => { saveToken(t); setToken(t); }, []); const value = useMemo( () => ({ user, token, loading, login, verifyTwoFactor, resendTwoFactor, register, logout, updateToken }), [loading, login, logout, register, resendTwoFactor, token, updateToken, user, verifyTwoFactor] ); return {children}; } export function useAuthContext() { const ctx = useContext(AuthContext); if (!ctx) throw new Error("useAuthContext must be used within AuthProvider"); return ctx; }