# What does this PR do?


## Test Plan
# What does this PR do?


## Test Plan
# What does this PR do?


## Test Plan
This commit is contained in:
Eric Huang 2025-06-25 20:10:45 -07:00
parent 114946ae88
commit a4d84f7805
22 changed files with 821 additions and 38 deletions

View file

@ -0,0 +1,77 @@
export interface User {
username: string;
email?: string;
name?: string;
avatar_url?: string;
organizations?: string[];
}
export interface AuthResponse {
access_token: string;
token_type: string;
expires_in: number;
user_info: User;
}
const TOKEN_KEY = "llama_stack_token";
const USER_KEY = "llama_stack_user";
export function getAuthToken(): string | null {
if (typeof window === "undefined") return null;
return localStorage.getItem(TOKEN_KEY);
}
export function setAuthToken(token: string): void {
if (typeof window !== "undefined") {
localStorage.setItem(TOKEN_KEY, token);
}
}
export function removeAuthToken(): void {
if (typeof window !== "undefined") {
localStorage.removeItem(TOKEN_KEY);
}
}
export function getStoredUser(): User | null {
if (typeof window === "undefined") return null;
const userStr = localStorage.getItem(USER_KEY);
if (!userStr) return null;
try {
return JSON.parse(userStr);
} catch {
return null;
}
}
export function setStoredUser(user: User): void {
if (typeof window !== "undefined") {
localStorage.setItem(USER_KEY, JSON.stringify(user));
}
}
export function removeStoredUser(): void {
if (typeof window !== "undefined") {
localStorage.removeItem(USER_KEY);
}
}
export function clearAuth(): void {
removeAuthToken();
removeStoredUser();
}
export function isAuthenticated(): boolean {
return !!getAuthToken();
}
export function isTokenExpired(token: string): boolean {
try {
const payload = JSON.parse(atob(token.split(".")[1]));
const exp = payload.exp;
if (!exp) return false;
return Date.now() >= exp * 1000;
} catch {
return true;
}
}