fixed bugs in auth for frontend
This commit is contained in:
+31
-1
@@ -1,5 +1,29 @@
|
||||
import { WebStorageStateStore } from "oidc-client-ts";
|
||||
|
||||
let accessToken: string | null = null;
|
||||
|
||||
function getStoredAccessToken(): string | null {
|
||||
if (typeof window === "undefined" || !isOidcConfigured()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const authority = import.meta.env.VITE_OIDC_ISSUER as string;
|
||||
const clientId = import.meta.env.VITE_OIDC_CLIENT_ID as string;
|
||||
const raw = window.localStorage.getItem(`oidc.user:${authority}:${clientId}`);
|
||||
if (!raw) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(raw) as { access_token?: unknown };
|
||||
return typeof parsed.access_token === "string" && parsed.access_token
|
||||
? parsed.access_token
|
||||
: null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function isOidcConfigured(): boolean {
|
||||
const enabled =
|
||||
(import.meta.env.VITE_OIDC_ENABLED ?? "true").toLowerCase() !== "false";
|
||||
@@ -11,6 +35,10 @@ export function isOidcConfigured(): boolean {
|
||||
}
|
||||
|
||||
export function getOidcConfig() {
|
||||
const storage = new WebStorageStateStore({
|
||||
store: window.localStorage,
|
||||
});
|
||||
|
||||
return {
|
||||
authority: import.meta.env.VITE_OIDC_ISSUER as string,
|
||||
client_id: import.meta.env.VITE_OIDC_CLIENT_ID as string,
|
||||
@@ -23,6 +51,8 @@ export function getOidcConfig() {
|
||||
response_type: "code" as const,
|
||||
automaticSilentRenew: false,
|
||||
loadUserInfo: true,
|
||||
stateStore: storage,
|
||||
userStore: storage,
|
||||
onSigninCallback: () => {
|
||||
window.history.replaceState(
|
||||
{},
|
||||
@@ -38,5 +68,5 @@ export function setAccessToken(token: string | null | undefined) {
|
||||
}
|
||||
|
||||
export function getAccessToken(): string | null {
|
||||
return accessToken;
|
||||
return accessToken ?? getStoredAccessToken();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user