prettier auth state ternary

This commit is contained in:
Igor Loskutov
2025-09-04 15:11:16 -04:00
parent ad780551b7
commit 6f29d08d1c

View File

@@ -32,28 +32,52 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
const customSession = session ? assertCustomSession(session) : null; const customSession = session ? assertCustomSession(session) : null;
const contextValue: AuthContextType = { const contextValue: AuthContextType = {
...(status === "loading" && !customSession ...(() => {
? { status } switch (status) {
: status === "loading" && customSession case "loading": {
? { status: "refreshing" as const } const sessionIsHere = !!customSession;
: status === "authenticated" && switch (sessionIsHere) {
customSession?.error === REFRESH_ACCESS_TOKEN_ERROR case false: {
? { status: "unauthenticated" } return { status };
: status === "authenticated" && customSession?.accessToken }
? { case true: {
return { status: "refreshing" as const };
}
default: {
const _: never = sessionIsHere;
throw new Error("unreachable");
}
}
}
case "authenticated": {
if (customSession?.error === REFRESH_ACCESS_TOKEN_ERROR) {
// token had chance to expire but next auth still returns "authenticated" so show user unauthenticated state
return {
status: "unauthenticated" as const,
};
} else if (customSession?.accessToken) {
return {
status, status,
accessToken: customSession.accessToken, accessToken: customSession.accessToken,
accessTokenExpires: customSession.accessTokenExpires, accessTokenExpires: customSession.accessTokenExpires,
user: customSession.user, user: customSession.user,
} };
: status === "authenticated" && !customSession?.accessToken } else {
? (() => {
console.warn( console.warn(
"illegal state: authenticated but have no session/or access token. ignoring", "illegal state: authenticated but have no session/or access token. ignoring",
); );
return { status: "unauthenticated" as const }; return { status: "unauthenticated" as const };
})() }
: { status: "unauthenticated" as const }), }
case "unauthenticated": {
return { status: "unauthenticated" as const };
}
default: {
const _: never = status;
throw new Error("unreachable");
}
}
})(),
update, update,
signIn, signIn,
signOut, signOut,