0fcb77b991
CI/CD Pipeline / Check if frontend-only change (push) Has been skipped
CI/CD Pipeline / Frontend Lint (push) Successful in 1m6s
CI Build & Deploy Pipeline / Build Staging Web Image (push) Successful in 1m49s
CI Build & Deploy Pipeline / Build Production API Image (push) Has been skipped
CI Build & Deploy Pipeline / Build Production Web Image (push) Has been skipped
CI Build & Deploy Pipeline / Build Production Worker Image (push) Has been skipped
CI Build & Deploy Pipeline / Deploy Production (push) Has been skipped
CI Build & Deploy Pipeline / Production Browser E2E (push) Has been skipped
CI/CD Pipeline / Validate Code Quality And Tests (push) Successful in 3m1s
CI/CD Pipeline / Unit Tests (push) Successful in 3m20s
CI/CD Pipeline / Integration Tests (push) Successful in 1m29s
CI Build & Deploy Pipeline / Build Staging API Image (push) Successful in 7m4s
CI Build & Deploy Pipeline / Build Staging Worker Image (push) Successful in 7m54s
CI Build & Deploy Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Successful in 45s
CI Build & Deploy Pipeline / Staging E2E Tests (push) Failing after 2m50s
CI Build & Deploy Pipeline / Staging API Integration Tests (push) Successful in 3m28s
Co-authored-by: xiaoxia <dev@xiaoxiajianji.com> Co-committed-by: xiaoxia <dev@xiaoxiajianji.com>
124 lines
2.9 KiB
TypeScript
124 lines
2.9 KiB
TypeScript
/**
|
|
* 认证相关 API
|
|
*/
|
|
import axios from "axios"
|
|
import apiClient from "./client"
|
|
|
|
// 类型定义
|
|
export interface LoginRequest {
|
|
email: string
|
|
password: string
|
|
}
|
|
|
|
export interface LoginResponse {
|
|
access_token: string
|
|
refresh_token?: string | null
|
|
token_type: string
|
|
expires_in: number
|
|
user_id: string
|
|
email: string
|
|
username: string
|
|
display_name: string
|
|
}
|
|
|
|
export interface RegisterRequest {
|
|
email: string
|
|
password: string
|
|
username: string
|
|
display_name?: string
|
|
}
|
|
|
|
export interface User {
|
|
id: string
|
|
user_id: string
|
|
email: string
|
|
username: string
|
|
display_name: string
|
|
is_email_verified: boolean
|
|
email_verified: boolean
|
|
created_at?: string
|
|
}
|
|
|
|
export interface UserResponse {
|
|
id?: string
|
|
user_id?: string
|
|
email: string
|
|
username: string
|
|
display_name: string
|
|
is_email_verified?: boolean
|
|
email_verified?: boolean
|
|
created_at?: string
|
|
}
|
|
|
|
export const normalizeUser = (data: UserResponse): User => {
|
|
const userId = data.id ?? data.user_id ?? ""
|
|
const emailVerified = data.is_email_verified ?? data.email_verified ?? false
|
|
|
|
return {
|
|
id: userId,
|
|
user_id: userId,
|
|
email: data.email,
|
|
username: data.username,
|
|
display_name: data.display_name,
|
|
is_email_verified: emailVerified,
|
|
email_verified: emailVerified,
|
|
created_at: data.created_at,
|
|
}
|
|
}
|
|
|
|
// 登录
|
|
export const login = async (data: LoginRequest): Promise<LoginResponse> => {
|
|
const response = await apiClient.post("/auth/login", data)
|
|
return response.data
|
|
}
|
|
|
|
// 刷新 access_token(使用裸 axios 避免拦截器递归)
|
|
export const refreshAccessToken = async (refreshToken: string): Promise<LoginResponse> => {
|
|
const baseURL = apiClient.defaults.baseURL ?? ""
|
|
const response = await axios.post(`${baseURL}/auth/refresh`, {
|
|
refresh_token: refreshToken,
|
|
})
|
|
return response.data
|
|
}
|
|
|
|
// 注册
|
|
export const register = async (data: RegisterRequest): Promise<{ message: string }> => {
|
|
const response = await apiClient.post("/auth/register", data)
|
|
return response.data
|
|
}
|
|
|
|
// 登出
|
|
export const logout = async (): Promise<void> => {
|
|
await apiClient.post("/auth/logout")
|
|
}
|
|
|
|
// 获取当前用户
|
|
export const getCurrentUser = async (): Promise<User> => {
|
|
const response = await apiClient.get<UserResponse>("/auth/me")
|
|
return normalizeUser(response.data)
|
|
}
|
|
|
|
// 请求密码重置
|
|
export const requestPasswordReset = async (email: string): Promise<{ message: string }> => {
|
|
const response = await apiClient.post("/auth/forgot-password", { email })
|
|
return response.data
|
|
}
|
|
|
|
// 重置密码
|
|
export const resetPassword = async (
|
|
token: string,
|
|
newPassword: string,
|
|
): Promise<{ message: string }> => {
|
|
const response = await apiClient.post("/auth/reset-password", {
|
|
token,
|
|
new_password: newPassword,
|
|
})
|
|
return response.data
|
|
}
|
|
|
|
// 验证邮箱
|
|
export const verifyEmail = async (token: string): Promise<{ message: string }> => {
|
|
const response = await apiClient.post("/auth/verify-email", { token })
|
|
return response.data
|
|
}
|