mirror of
https://github.com/complexcaresolutions/dak.c2s.git
synced 2026-03-18 06:03:42 +00:00
Extend User and UserResponse interfaces with first_name, last_name, display_name, avatar_url fields. Add ProfileUpdatePayload, ChangePasswordPayload, MFASetupResponse, MFAVerifyPayload types. Add authService functions for profile update, avatar upload/delete, password change, and MFA setup/verify/disable. Add refreshUser to AuthContext so components can re-fetch user data after changes. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
67 lines
2.2 KiB
TypeScript
67 lines
2.2 KiB
TypeScript
import api from './api'
|
|
import type { LoginRequest, RegisterRequest, TokenResponse, User, ProfileUpdatePayload, ChangePasswordPayload, MFASetupResponse, MFAVerifyPayload } from '@/types'
|
|
|
|
export async function login(data: LoginRequest): Promise<TokenResponse> {
|
|
const response = await api.post<TokenResponse>('/auth/login', data)
|
|
localStorage.setItem('access_token', response.data.access_token)
|
|
localStorage.setItem('refresh_token', response.data.refresh_token)
|
|
return response.data
|
|
}
|
|
|
|
export async function register(data: RegisterRequest): Promise<{ user: User }> {
|
|
const response = await api.post<{ user: User }>('/auth/register', data)
|
|
return response.data
|
|
}
|
|
|
|
export async function logout(): Promise<void> {
|
|
try {
|
|
const refreshToken = localStorage.getItem('refresh_token')
|
|
if (refreshToken) {
|
|
await api.post('/auth/logout', { refresh_token: refreshToken })
|
|
}
|
|
} finally {
|
|
localStorage.removeItem('access_token')
|
|
localStorage.removeItem('refresh_token')
|
|
}
|
|
}
|
|
|
|
export async function getMe(): Promise<User> {
|
|
const response = await api.get<User>('/auth/me')
|
|
return response.data
|
|
}
|
|
|
|
export async function updateProfile(data: ProfileUpdatePayload): Promise<User> {
|
|
const response = await api.put<User>('/auth/profile', data)
|
|
return response.data
|
|
}
|
|
|
|
export async function uploadAvatar(file: File): Promise<User> {
|
|
const formData = new FormData()
|
|
formData.append('file', file)
|
|
const response = await api.post<User>('/auth/avatar', formData, {
|
|
headers: { 'Content-Type': 'multipart/form-data' },
|
|
})
|
|
return response.data
|
|
}
|
|
|
|
export async function deleteAvatar(): Promise<User> {
|
|
const response = await api.delete<User>('/auth/avatar')
|
|
return response.data
|
|
}
|
|
|
|
export async function changePassword(data: ChangePasswordPayload): Promise<void> {
|
|
await api.post('/auth/change-password', data)
|
|
}
|
|
|
|
export async function setupMFA(): Promise<MFASetupResponse> {
|
|
const response = await api.post<MFASetupResponse>('/auth/mfa/setup')
|
|
return response.data
|
|
}
|
|
|
|
export async function verifyMFA(data: MFAVerifyPayload): Promise<void> {
|
|
await api.post('/auth/mfa/verify', data)
|
|
}
|
|
|
|
export async function disableMFA(password: string): Promise<void> {
|
|
await api.delete('/auth/mfa', { data: { password } })
|
|
}
|