45 lines
1013 B
TypeScript
45 lines
1013 B
TypeScript
import { clearSession, writeSession, type AuthSession } from "@/lib/auth"
|
|
import { request } from "@/lib/api/client"
|
|
|
|
export type LoginRequest = {
|
|
username: string
|
|
password: string
|
|
}
|
|
|
|
export async function loginWithPassword(payload: LoginRequest) {
|
|
const data = await request<AuthSession>("/api/auth/login", {
|
|
method: "POST",
|
|
body: JSON.stringify(payload),
|
|
skipAuth: true,
|
|
})
|
|
writeSession(data)
|
|
return data
|
|
}
|
|
|
|
export async function exchangeWxWorkTicket(ticket: string) {
|
|
const data = await request<AuthSession>("/api/auth/wxwork_exchange", {
|
|
method: "POST",
|
|
body: JSON.stringify({ ticket }),
|
|
skipAuth: true,
|
|
})
|
|
writeSession(data)
|
|
return data
|
|
}
|
|
|
|
export async function fetchProfile() {
|
|
return request<AuthSession>("/api/auth/profile")
|
|
}
|
|
|
|
export async function logout(refreshToken?: string) {
|
|
try {
|
|
await request("/api/auth/logout", {
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
refreshToken,
|
|
}),
|
|
})
|
|
} finally {
|
|
clearSession()
|
|
}
|
|
}
|