mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 20:29:06 +00:00
chore: sentry and nextjs major bumps (#633)
* chore: remove nextjs-config * build fix * sentry update * nextjs update * feature flags doc * update readme * explicit nextjs env vars + remove feature-unrelated things and obsolete vars from config * full config removal * remove force-dynamic from pages * compile fix * restore claude-deleted tests * no sentry backward compat * better .env.example * AUTHENTIK_REFRESH_TOKEN_URL not so required * accommodate auth system to requiredLogin feature --------- Co-authored-by: Igor Loskutov <igor.loskutoff@gmail.com>
This commit is contained in:
@@ -9,6 +9,7 @@ import { Session } from "next-auth";
|
||||
import { SessionAutoRefresh } from "./SessionAutoRefresh";
|
||||
import { REFRESH_ACCESS_TOKEN_ERROR } from "./auth";
|
||||
import { assertExists } from "./utils";
|
||||
import { featureEnabled } from "./features";
|
||||
|
||||
type AuthContextType = (
|
||||
| { status: "loading" }
|
||||
@@ -27,65 +28,83 @@ type AuthContextType = (
|
||||
};
|
||||
|
||||
const AuthContext = createContext<AuthContextType | undefined>(undefined);
|
||||
const isAuthEnabled = featureEnabled("requireLogin");
|
||||
|
||||
const noopAuthContext: AuthContextType = {
|
||||
status: "unauthenticated",
|
||||
update: async () => {
|
||||
return null;
|
||||
},
|
||||
signIn: async () => {
|
||||
throw new Error("signIn not supposed to be called");
|
||||
},
|
||||
signOut: async () => {
|
||||
throw new Error("signOut not supposed to be called");
|
||||
},
|
||||
};
|
||||
|
||||
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
const { data: session, status, update } = useNextAuthSession();
|
||||
const customSession = session ? assertCustomSession(session) : null;
|
||||
|
||||
const contextValue: AuthContextType = {
|
||||
...(() => {
|
||||
switch (status) {
|
||||
case "loading": {
|
||||
const sessionIsHere = !!customSession;
|
||||
switch (sessionIsHere) {
|
||||
case false: {
|
||||
return { status };
|
||||
const contextValue: AuthContextType = isAuthEnabled
|
||||
? {
|
||||
...(() => {
|
||||
switch (status) {
|
||||
case "loading": {
|
||||
const sessionIsHere = !!session;
|
||||
// actually exists sometimes; nextAuth types are something else
|
||||
switch (sessionIsHere as boolean) {
|
||||
case false: {
|
||||
return { status };
|
||||
}
|
||||
case true: {
|
||||
return {
|
||||
status: "refreshing" as const,
|
||||
user: assertCustomSession(
|
||||
assertExists(session as unknown as Session),
|
||||
).user,
|
||||
};
|
||||
}
|
||||
default: {
|
||||
throw new Error("unreachable");
|
||||
}
|
||||
}
|
||||
}
|
||||
case true: {
|
||||
return {
|
||||
status: "refreshing" as const,
|
||||
user: assertExists(customSession).user,
|
||||
};
|
||||
case "authenticated": {
|
||||
const customSession = assertCustomSession(session);
|
||||
if (customSession?.error === REFRESH_ACCESS_TOKEN_ERROR) {
|
||||
// token had expired but next auth still returns "authenticated" so show user unauthenticated state
|
||||
return {
|
||||
status: "unauthenticated" as const,
|
||||
};
|
||||
} else if (customSession?.accessToken) {
|
||||
return {
|
||||
status,
|
||||
accessToken: customSession.accessToken,
|
||||
accessTokenExpires: customSession.accessTokenExpires,
|
||||
user: customSession.user,
|
||||
};
|
||||
} else {
|
||||
console.warn(
|
||||
"illegal state: authenticated but have no session/or access token. ignoring",
|
||||
);
|
||||
return { status: "unauthenticated" as const };
|
||||
}
|
||||
}
|
||||
case "unauthenticated": {
|
||||
return { status: "unauthenticated" as const };
|
||||
}
|
||||
default: {
|
||||
const _: never = sessionIsHere;
|
||||
const _: never = status;
|
||||
throw new Error("unreachable");
|
||||
}
|
||||
}
|
||||
}
|
||||
case "authenticated": {
|
||||
if (customSession?.error === REFRESH_ACCESS_TOKEN_ERROR) {
|
||||
// token had expired but next auth still returns "authenticated" so show user unauthenticated state
|
||||
return {
|
||||
status: "unauthenticated" as const,
|
||||
};
|
||||
} else if (customSession?.accessToken) {
|
||||
return {
|
||||
status,
|
||||
accessToken: customSession.accessToken,
|
||||
accessTokenExpires: customSession.accessTokenExpires,
|
||||
user: customSession.user,
|
||||
};
|
||||
} else {
|
||||
console.warn(
|
||||
"illegal state: authenticated but have no session/or access token. ignoring",
|
||||
);
|
||||
return { status: "unauthenticated" as const };
|
||||
}
|
||||
}
|
||||
case "unauthenticated": {
|
||||
return { status: "unauthenticated" as const };
|
||||
}
|
||||
default: {
|
||||
const _: never = status;
|
||||
throw new Error("unreachable");
|
||||
}
|
||||
})(),
|
||||
update,
|
||||
signIn,
|
||||
signOut,
|
||||
}
|
||||
})(),
|
||||
update,
|
||||
signIn,
|
||||
signOut,
|
||||
};
|
||||
: noopAuthContext;
|
||||
|
||||
// not useEffect, we need it ASAP
|
||||
// apparently, still no guarantee this code runs before mutations are fired
|
||||
|
||||
Reference in New Issue
Block a user