63 lines
2.2 KiB
TypeScript
63 lines
2.2 KiB
TypeScript
|
|
import { api } from './api';
|
|
import { User, UserDTO } from '../types';
|
|
import { logger } from '@/utils/logger';
|
|
|
|
// Helper to map Backend DTO to Frontend Model
|
|
const mapUserDTO = (dto: UserDTO): User => ({
|
|
id: dto.id,
|
|
username: dto.username,
|
|
email: dto.email,
|
|
firstName: dto.first_name,
|
|
lastName: dto.last_name,
|
|
fullName: `${dto.first_name || ''} ${dto.last_name || ''}`.trim() || dto.username,
|
|
avatar: dto.avatar || 'https://via.placeholder.com/200', // Fallback
|
|
banner: dto.banner,
|
|
bio: dto.bio,
|
|
location: dto.location,
|
|
roles: dto.role ? [dto.role] : ['USER'], // Simple mapping, adapt based on actual role structure
|
|
status: 'online', // Default status for self
|
|
joinDate: new Date(dto.created_at).toLocaleDateString(),
|
|
tier: dto.is_verified ? 'Pro' : 'Free', // Logic based on verification or specific plan field
|
|
stats: { followers: 0, following: 0, tracks: 0, plays: 0 } // Fetch separately or include in DTO
|
|
});
|
|
|
|
export const authService = {
|
|
login: async (credentials: any) => {
|
|
const response = await api.post<{ user: UserDTO; access_token: string; refresh_token: string }>('/auth/login', credentials);
|
|
return {
|
|
user: mapUserDTO(response.user),
|
|
token: { access_token: response.access_token, refresh_token: response.refresh_token }
|
|
};
|
|
},
|
|
|
|
register: async (data: any) => {
|
|
const response = await api.post<{ user: UserDTO; access_token: string; refresh_token: string }>('/auth/register', data);
|
|
return {
|
|
user: mapUserDTO(response.user),
|
|
token: { access_token: response.access_token, refresh_token: response.refresh_token }
|
|
};
|
|
},
|
|
|
|
logout: async () => {
|
|
try {
|
|
await api.post('/auth/logout');
|
|
} catch (e) {
|
|
logger.warn('Logout failed on server', { error: e });
|
|
}
|
|
},
|
|
|
|
getCurrentUser: async () => {
|
|
const response = await api.get<{ user: UserDTO }>('/auth/me');
|
|
return mapUserDTO(response.user);
|
|
},
|
|
|
|
checkUsername: async (username: string) => {
|
|
// Assuming endpoint returns { available: boolean }
|
|
return api.get<{ available: boolean }>(`/auth/check-username?username=${username}`);
|
|
},
|
|
|
|
verifyEmail: (token: string) => api.post('/auth/verify-email', { token }),
|
|
|
|
resendVerification: (email: string) => api.post('/auth/resend-verification', { email }),
|
|
};
|