69 lines
1.7 KiB
TypeScript
69 lines
1.7 KiB
TypeScript
import axios from 'axios'
|
|
|
|
import { apiClient } from './client'
|
|
|
|
export interface AdminUser {
|
|
id: number
|
|
username: string
|
|
nickname: string
|
|
status: string
|
|
last_login_at?: string
|
|
}
|
|
|
|
export interface AdminTokenPair {
|
|
access_token: string
|
|
refresh_token: string
|
|
token_type: string
|
|
expires_in: number
|
|
}
|
|
|
|
export interface AdminLoginData {
|
|
admin: AdminUser
|
|
tokens: AdminTokenPair
|
|
}
|
|
|
|
export interface AdminCaptcha {
|
|
captcha_id: string
|
|
image: string
|
|
expires_in: number
|
|
}
|
|
|
|
interface ApiResponse<T> {
|
|
code: string
|
|
message: string
|
|
data: T
|
|
}
|
|
|
|
export async function fetchAdminCaptcha() {
|
|
const { data } = await apiClient.get<ApiResponse<AdminCaptcha>>('/admin/auth/captcha')
|
|
return data.data
|
|
}
|
|
|
|
export async function loginAdmin(username: string, password: string, captchaId: string, captchaCode: string) {
|
|
const { data } = await apiClient.post<ApiResponse<AdminLoginData>>('/admin/auth/login', {
|
|
username,
|
|
password,
|
|
captcha_id: captchaId,
|
|
captcha_code: captchaCode,
|
|
})
|
|
return data.data
|
|
}
|
|
|
|
export async function fetchAdminMe() {
|
|
const { data } = await apiClient.get<ApiResponse<AdminUser>>('/admin/me')
|
|
return data.data
|
|
}
|
|
|
|
export async function logoutAdmin() {
|
|
const { data } = await apiClient.post<ApiResponse<{ logged_out: boolean }>>('/admin/auth/logout')
|
|
return data.data
|
|
}
|
|
|
|
/** Manually refresh admin token (uses raw axios to avoid interceptor recursion) */
|
|
export async function refreshAdminSession() {
|
|
const refreshToken = localStorage.getItem('admin_refresh_token')
|
|
if (!refreshToken) throw new Error('no refresh token')
|
|
const { data } = await axios.post('/api/admin/auth/refresh', { refresh_token: refreshToken })
|
|
return data.data as AdminTokenPair
|
|
}
|