style(frontend): 统一 API 模块代码风格
- 移除所有语句末尾分号 - 统一对象属性尾随逗号格式 - 优化类型定义导入顺序 - 提升代码一致性和可读性
This commit is contained in:
@@ -3,29 +3,37 @@
|
||||
* Handles user login, registration, and logout operations
|
||||
*/
|
||||
|
||||
import { apiClient } from './client';
|
||||
import type { LoginRequest, RegisterRequest, AuthResponse, User, SendVerifyCodeRequest, SendVerifyCodeResponse, PublicSettings } from '@/types';
|
||||
import { apiClient } from './client'
|
||||
import type {
|
||||
LoginRequest,
|
||||
RegisterRequest,
|
||||
AuthResponse,
|
||||
User,
|
||||
SendVerifyCodeRequest,
|
||||
SendVerifyCodeResponse,
|
||||
PublicSettings
|
||||
} from '@/types'
|
||||
|
||||
/**
|
||||
* Store authentication token in localStorage
|
||||
*/
|
||||
export function setAuthToken(token: string): void {
|
||||
localStorage.setItem('auth_token', token);
|
||||
localStorage.setItem('auth_token', token)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get authentication token from localStorage
|
||||
*/
|
||||
export function getAuthToken(): string | null {
|
||||
return localStorage.getItem('auth_token');
|
||||
return localStorage.getItem('auth_token')
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear authentication token from localStorage
|
||||
*/
|
||||
export function clearAuthToken(): void {
|
||||
localStorage.removeItem('auth_token');
|
||||
localStorage.removeItem('auth_user');
|
||||
localStorage.removeItem('auth_token')
|
||||
localStorage.removeItem('auth_user')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,13 +42,13 @@ export function clearAuthToken(): void {
|
||||
* @returns Authentication response with token and user data
|
||||
*/
|
||||
export async function login(credentials: LoginRequest): Promise<AuthResponse> {
|
||||
const { data } = await apiClient.post<AuthResponse>('/auth/login', credentials);
|
||||
const { data } = await apiClient.post<AuthResponse>('/auth/login', credentials)
|
||||
|
||||
// Store token and user data
|
||||
setAuthToken(data.access_token);
|
||||
localStorage.setItem('auth_user', JSON.stringify(data.user));
|
||||
setAuthToken(data.access_token)
|
||||
localStorage.setItem('auth_user', JSON.stringify(data.user))
|
||||
|
||||
return data;
|
||||
return data
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,13 +57,13 @@ export async function login(credentials: LoginRequest): Promise<AuthResponse> {
|
||||
* @returns Authentication response with token and user data
|
||||
*/
|
||||
export async function register(userData: RegisterRequest): Promise<AuthResponse> {
|
||||
const { data } = await apiClient.post<AuthResponse>('/auth/register', userData);
|
||||
const { data } = await apiClient.post<AuthResponse>('/auth/register', userData)
|
||||
|
||||
// Store token and user data
|
||||
setAuthToken(data.access_token);
|
||||
localStorage.setItem('auth_user', JSON.stringify(data.user));
|
||||
setAuthToken(data.access_token)
|
||||
localStorage.setItem('auth_user', JSON.stringify(data.user))
|
||||
|
||||
return data;
|
||||
return data
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,8 +71,8 @@ export async function register(userData: RegisterRequest): Promise<AuthResponse>
|
||||
* @returns User profile data
|
||||
*/
|
||||
export async function getCurrentUser(): Promise<User> {
|
||||
const { data } = await apiClient.get<User>('/auth/me');
|
||||
return data;
|
||||
const { data } = await apiClient.get<User>('/auth/me')
|
||||
return data
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -72,7 +80,7 @@ export async function getCurrentUser(): Promise<User> {
|
||||
* Clears authentication token and user data from localStorage
|
||||
*/
|
||||
export function logout(): void {
|
||||
clearAuthToken();
|
||||
clearAuthToken()
|
||||
// Optionally redirect to login page
|
||||
// window.location.href = '/login';
|
||||
}
|
||||
@@ -82,7 +90,7 @@ export function logout(): void {
|
||||
* @returns True if user has valid token
|
||||
*/
|
||||
export function isAuthenticated(): boolean {
|
||||
return getAuthToken() !== null;
|
||||
return getAuthToken() !== null
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,8 +98,8 @@ export function isAuthenticated(): boolean {
|
||||
* @returns Public settings including registration and Turnstile config
|
||||
*/
|
||||
export async function getPublicSettings(): Promise<PublicSettings> {
|
||||
const { data } = await apiClient.get<PublicSettings>('/settings/public');
|
||||
return data;
|
||||
const { data } = await apiClient.get<PublicSettings>('/settings/public')
|
||||
return data
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,9 +107,11 @@ export async function getPublicSettings(): Promise<PublicSettings> {
|
||||
* @param request - Email and optional Turnstile token
|
||||
* @returns Response with countdown seconds
|
||||
*/
|
||||
export async function sendVerifyCode(request: SendVerifyCodeRequest): Promise<SendVerifyCodeResponse> {
|
||||
const { data } = await apiClient.post<SendVerifyCodeResponse>('/auth/send-verify-code', request);
|
||||
return data;
|
||||
export async function sendVerifyCode(
|
||||
request: SendVerifyCodeRequest
|
||||
): Promise<SendVerifyCodeResponse> {
|
||||
const { data } = await apiClient.post<SendVerifyCodeResponse>('/auth/send-verify-code', request)
|
||||
return data
|
||||
}
|
||||
|
||||
export const authAPI = {
|
||||
@@ -114,7 +124,7 @@ export const authAPI = {
|
||||
getAuthToken,
|
||||
clearAuthToken,
|
||||
getPublicSettings,
|
||||
sendVerifyCode,
|
||||
};
|
||||
sendVerifyCode
|
||||
}
|
||||
|
||||
export default authAPI;
|
||||
export default authAPI
|
||||
|
||||
Reference in New Issue
Block a user