41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import type { AuthConfig } from "../../shared/auth-config";
|
|
import type { AuthStateValue } from "../../shared/auth-messages";
|
|
|
|
export function createLoggedOutAuthState(
|
|
config?: Pick<AuthConfig, "apiResource">,
|
|
lastError?: string | null
|
|
): AuthStateValue {
|
|
return {
|
|
isAuthenticated: false,
|
|
lastError: lastError ?? null,
|
|
resource: config?.apiResource ?? null
|
|
};
|
|
}
|
|
|
|
export function createLoggedInAuthState(
|
|
claims: Record<string, unknown> | null | undefined,
|
|
config?: Pick<AuthConfig, "apiResource" | "scopes">
|
|
): AuthStateValue {
|
|
return {
|
|
accessTokenExpiresAt: null,
|
|
isAuthenticated: true,
|
|
resource: config?.apiResource ?? null,
|
|
scopes: config?.scopes ?? [],
|
|
tokenAvailable: true,
|
|
userInfo: {
|
|
email: readStringClaim(claims, "email"),
|
|
name: readStringClaim(claims, "name"),
|
|
sub: readStringClaim(claims, "sub"),
|
|
username: readStringClaim(claims, "username")
|
|
}
|
|
};
|
|
}
|
|
|
|
function readStringClaim(
|
|
claims: Record<string, unknown> | null | undefined,
|
|
key: string
|
|
): string | undefined {
|
|
const value = claims?.[key];
|
|
return typeof value === "string" ? value : undefined;
|
|
}
|