mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-06-27 18:50:41 +00:00
# What does this PR do? ## Test Plan # What does this PR do? ## Test Plan # What does this PR do? ## Test Plan
77 lines
1.6 KiB
TypeScript
77 lines
1.6 KiB
TypeScript
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;
|
|
}
|
|
}
|