39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
/**
|
|
* Auth API — wraps /api/oa/auth/* and manages the stored Bearer token.
|
|
*/
|
|
import { http, setToken, clearToken, getToken } from './http'
|
|
|
|
export interface OaSession {
|
|
token: string
|
|
id: number
|
|
loginName: string
|
|
displayName: string
|
|
deptId: number | null
|
|
title: string | null
|
|
email: string | null
|
|
roles: string[]
|
|
}
|
|
|
|
/** POST /auth/login -> stores the token and returns the session. */
|
|
export async function login(loginName: string, password: string): Promise<OaSession> {
|
|
const session = await http.post<OaSession>('/auth/login', { loginName, password })
|
|
if (session?.token) setToken(session.token)
|
|
return session
|
|
}
|
|
|
|
/** GET /auth/session -> resolves the current session from the stored token. */
|
|
export async function fetchSession(): Promise<OaSession> {
|
|
return http.get<OaSession>('/auth/session')
|
|
}
|
|
|
|
/** POST /auth/logout -> clears the stored token regardless of server result. */
|
|
export async function logout(): Promise<void> {
|
|
try {
|
|
await http.post<void>('/auth/logout')
|
|
} finally {
|
|
clearToken()
|
|
}
|
|
}
|
|
|
|
export { getToken, setToken, clearToken }
|