chatgpt2api / web /src /lib /auth-session.ts
tx1538's picture
Upload 179 files
9d7ddb9 verified
Raw
History Blame
718 Bytes
"use client";
import { login } from "@/lib/api";
import { clearStoredAuthSession, getStoredAuthSession, setStoredAuthSession, type StoredAuthSession } from "@/store/auth";
export async function getValidatedAuthSession(): Promise<StoredAuthSession | null> {
const storedSession = await getStoredAuthSession();
if (!storedSession) {
return null;
}
try {
const data = await login(storedSession.key);
const nextSession: StoredAuthSession = {
key: storedSession.key,
role: data.role,
subjectId: data.subject_id,
name: data.name,
};
await setStoredAuthSession(nextSession);
return nextSession;
} catch {
await clearStoredAuthSession();
return null;
}
}