This commit is contained in:
Igor Loskutov
2025-09-02 14:44:10 -04:00
parent 5ffc312d4a
commit 31c44ac0bb
11 changed files with 52 additions and 78 deletions

View File

@@ -3,7 +3,7 @@
import { createContext, useContext, useEffect } from "react";
import { useSession as useNextAuthSession } from "next-auth/react";
import { configureApiAuth } from "./apiClient";
import { CustomSession } from "./types";
import { assertExtendedToken, CustomSession } from "./types";
type AuthContextType =
| { status: "loading" }
@@ -12,26 +12,24 @@ type AuthContextType =
status: "authenticated";
accessToken: string;
accessTokenExpires: number;
user: CustomSession["user"];
};
const AuthContext = createContext<AuthContextType | undefined>(undefined);
export function AuthProvider({ children }: { children: React.ReactNode }) {
const { data: session, status } = useNextAuthSession();
const customSession = session as CustomSession;
if (session) {
debugger;
}
const customSession = session ? assertExtendedToken(session) : null;
const contextValue: AuthContextType =
status === "loading"
? { status: "loading" as const }
: customSession?.accessToken
: status === "authenticated" && customSession?.accessToken
? {
status: "authenticated" as const,
accessToken: customSession.accessToken,
accessTokenExpires: customSession.accessTokenExpires,
user: customSession.user,
}
: { status: "unauthenticated" as const };