mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 20:29:06 +00:00
prettier auth state ternary
This commit is contained in:
@@ -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,
|
||||||
|
|||||||
Reference in New Issue
Block a user