debug(auth): log session cookie source
This commit is contained in:
@@ -11,6 +11,7 @@ import {
|
||||
} from "react";
|
||||
import type { AuthUser, AuthSession } from "@mosaic/shared";
|
||||
import { apiGet, apiPost } from "../api/client";
|
||||
import { IS_MOCK_AUTH_MODE } from "../config";
|
||||
import { parseAuthError } from "./auth-errors";
|
||||
|
||||
/**
|
||||
@@ -23,6 +24,11 @@ const SESSION_EXPIRY_WARNING_MINUTES = 5;
|
||||
|
||||
/** Interval in milliseconds to check session expiry */
|
||||
const SESSION_CHECK_INTERVAL_MS = 60_000;
|
||||
const MOCK_AUTH_USER: AuthUser = {
|
||||
id: "dev-user-local",
|
||||
email: "dev@localhost",
|
||||
name: "Local Dev User",
|
||||
};
|
||||
|
||||
interface AuthContextValue {
|
||||
user: AuthUser | null;
|
||||
@@ -70,6 +76,14 @@ function logAuthError(message: string, error: unknown): void {
|
||||
}
|
||||
|
||||
export function AuthProvider({ children }: { children: ReactNode }): React.JSX.Element {
|
||||
if (IS_MOCK_AUTH_MODE) {
|
||||
return <MockAuthProvider>{children}</MockAuthProvider>;
|
||||
}
|
||||
|
||||
return <RealAuthProvider>{children}</RealAuthProvider>;
|
||||
}
|
||||
|
||||
function RealAuthProvider({ children }: { children: ReactNode }): React.JSX.Element {
|
||||
const [user, setUser] = useState<AuthUser | null>(null);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [authError, setAuthError] = useState<AuthErrorType>(null);
|
||||
@@ -176,6 +190,33 @@ export function AuthProvider({ children }: { children: ReactNode }): React.JSX.E
|
||||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
|
||||
}
|
||||
|
||||
function MockAuthProvider({ children }: { children: ReactNode }): React.JSX.Element {
|
||||
const [user, setUser] = useState<AuthUser | null>(MOCK_AUTH_USER);
|
||||
|
||||
const signOut = useCallback((): Promise<void> => {
|
||||
setUser(null);
|
||||
return Promise.resolve();
|
||||
}, []);
|
||||
|
||||
const refreshSession = useCallback((): Promise<void> => {
|
||||
setUser(MOCK_AUTH_USER);
|
||||
return Promise.resolve();
|
||||
}, []);
|
||||
|
||||
const value: AuthContextValue = {
|
||||
user,
|
||||
isLoading: false,
|
||||
isAuthenticated: user !== null,
|
||||
authError: null,
|
||||
sessionExpiring: false,
|
||||
sessionMinutesRemaining: 0,
|
||||
signOut,
|
||||
refreshSession,
|
||||
};
|
||||
|
||||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
|
||||
}
|
||||
|
||||
export function useAuth(): AuthContextValue {
|
||||
const context = useContext(AuthContext);
|
||||
if (context === undefined) {
|
||||
|
||||
Reference in New Issue
Block a user