Files
ai-agent/web/lib/auth.ts
T
mlogclub eb04c344a5 refactor: rename cs-ai-agent to agent-desk in SDK and related files
- Updated message types in support-host-bridge.ts to use agent-desk prefix.
- Added new agent-desk-sdk.min.js file with updated functionality.
- Removed old cs-ai-agent-sdk.min.js file.
- Modified build-sdk script to generate agent-desk-sdk.min.js.
- Changed scrollbar class names in main.scss from cs-ai-agent to agent-desk.
2026-05-31 18:46:05 +08:00

64 lines
1.2 KiB
TypeScript

export type AuthUser = {
id: number
username: string
nickname: string
avatar: string
status: number
roles: string[]
}
export type AuthSession = {
accessToken: string
expiresAt?: string
user: AuthUser
permissions: string[]
roles: string[]
}
const SESSION_STORAGE_KEY = "agent-desk-session"
export const AUTH_SESSION_EXPIRED_EVENT = "agent-desk-auth-expired"
function hasWindow() {
return typeof window !== "undefined"
}
export function readSession(): AuthSession | null {
if (!hasWindow()) {
return null
}
const raw = window.localStorage.getItem(SESSION_STORAGE_KEY)
if (!raw) {
return null
}
try {
return JSON.parse(raw) as AuthSession
} catch {
window.localStorage.removeItem(SESSION_STORAGE_KEY)
return null
}
}
export function writeSession(session: AuthSession) {
if (!hasWindow()) {
return
}
window.localStorage.setItem(SESSION_STORAGE_KEY, JSON.stringify(session))
}
export function clearSession() {
if (!hasWindow()) {
return
}
window.localStorage.removeItem(SESSION_STORAGE_KEY)
}
export function expireSession() {
if (!hasWindow()) {
return
}
clearSession()
window.dispatchEvent(new Event(AUTH_SESSION_EXPIRED_EVENT))
}