"use client"; import { createContext, ReactElement, useContext, useMemo, useState } from "react"; import { AcceptInvitation, ActivateAccount, ForgotPassword, LandingComponent, Login, ResetPassword, TwoFactorChallenge, } from "../components"; import Register from "../components/forms/Register"; import { AuthComponent } from "../enums"; import { TwoFactorChallengeInterface } from "../data/two-factor-challenge.interface"; interface AuthContextType { activeComponent: ReactElement | null; setComponentType: (componentType: AuthComponent) => void; setParams: (params?: { code?: string }) => void; params?: { code?: string }; pendingTwoFactor?: TwoFactorChallengeInterface; setPendingTwoFactor: (challenge?: TwoFactorChallengeInterface) => void; } const AuthContext = createContext(undefined); export const AuthContextProvider = ({ children, initialComponentType, initialParams, }: { children: React.ReactNode; initialComponentType?: AuthComponent; initialParams?: { code?: string }; }) => { const [componentType, setComponentType] = useState(initialComponentType); const [params, setParams] = useState<{ code?: string } | undefined>(initialParams); const [pendingTwoFactor, setPendingTwoFactor] = useState(); const activeComponent = useMemo(() => { if (componentType === undefined) return null; switch (componentType) { case AuthComponent.Login: return ; case AuthComponent.Register: return ; case AuthComponent.ForgotPassword: return ; case AuthComponent.ActivateAccount: return ; case AuthComponent.ResetPassword: return ; case AuthComponent.AcceptInvitation: return ; case AuthComponent.TwoFactorChallenge: return ; default: return ; } }, [componentType]); return ( {children} ); }; export const useAuthContext = (): AuthContextType => { const context = useContext(AuthContext); if (context === undefined) { throw new Error("useAuthContext must be used within a AuthComponentProvider"); } return context; };