2025-07-22 17:01:12 +02:00
|
|
|
// src/api/services/AuthService.ts
|
2025-07-22 07:37:51 +02:00
|
|
|
import { apiClient } from '../base/apiClient';
|
2025-07-22 17:01:12 +02:00
|
|
|
import {
|
|
|
|
|
ApiResponse,
|
|
|
|
|
LoginRequest,
|
|
|
|
|
RegisterRequest,
|
|
|
|
|
TokenResponse,
|
|
|
|
|
UserProfile,
|
|
|
|
|
} from '../types/api';
|
2025-07-22 07:37:51 +02:00
|
|
|
|
2025-07-22 17:01:12 +02:00
|
|
|
export class AuthService {
|
|
|
|
|
/**
|
|
|
|
|
* User login
|
|
|
|
|
*/
|
|
|
|
|
async login(credentials: LoginRequest): Promise<TokenResponse> {
|
|
|
|
|
const response = await apiClient.post<ApiResponse<TokenResponse>>(
|
|
|
|
|
'/auth/login',
|
|
|
|
|
credentials
|
|
|
|
|
);
|
|
|
|
|
return response.data!;
|
2025-07-22 07:37:51 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-22 17:01:12 +02:00
|
|
|
/**
|
|
|
|
|
* User registration
|
|
|
|
|
*/
|
|
|
|
|
async register(userData: RegisterRequest): Promise<UserProfile> {
|
|
|
|
|
const response = await apiClient.post<ApiResponse<UserProfile>>(
|
|
|
|
|
'/auth/register',
|
|
|
|
|
userData
|
|
|
|
|
);
|
|
|
|
|
return response.data!;
|
2025-07-22 07:37:51 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-22 17:01:12 +02:00
|
|
|
/**
|
|
|
|
|
* Refresh access token
|
|
|
|
|
*/
|
|
|
|
|
async refreshToken(refreshToken: string): Promise<TokenResponse> {
|
|
|
|
|
const response = await apiClient.post<ApiResponse<TokenResponse>>(
|
|
|
|
|
'/auth/refresh',
|
|
|
|
|
{ refresh_token: refreshToken }
|
|
|
|
|
);
|
|
|
|
|
return response.data!;
|
2025-07-22 07:37:51 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-22 17:01:12 +02:00
|
|
|
/**
|
|
|
|
|
* Get current user profile
|
|
|
|
|
*/
|
|
|
|
|
async getProfile(): Promise<UserProfile> {
|
|
|
|
|
const response = await apiClient.get<ApiResponse<UserProfile>>('/users/me');
|
|
|
|
|
return response.data!;
|
2025-07-22 07:37:51 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-22 17:01:12 +02:00
|
|
|
/**
|
|
|
|
|
* Update user profile
|
|
|
|
|
*/
|
2025-07-22 07:37:51 +02:00
|
|
|
async updateProfile(updates: Partial<UserProfile>): Promise<UserProfile> {
|
2025-07-22 17:01:12 +02:00
|
|
|
const response = await apiClient.put<ApiResponse<UserProfile>>(
|
|
|
|
|
'/users/me',
|
|
|
|
|
updates
|
|
|
|
|
);
|
|
|
|
|
return response.data!;
|
2025-07-22 07:37:51 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-22 17:01:12 +02:00
|
|
|
/**
|
|
|
|
|
* Change password
|
|
|
|
|
*/
|
|
|
|
|
async changePassword(
|
|
|
|
|
currentPassword: string,
|
|
|
|
|
newPassword: string
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
await apiClient.post('/auth/change-password', {
|
2025-07-22 07:37:51 +02:00
|
|
|
current_password: currentPassword,
|
2025-07-22 17:01:12 +02:00
|
|
|
new_password: newPassword,
|
2025-07-22 07:37:51 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-22 17:01:12 +02:00
|
|
|
/**
|
|
|
|
|
* Request password reset
|
|
|
|
|
*/
|
|
|
|
|
async requestPasswordReset(email: string): Promise<void> {
|
|
|
|
|
await apiClient.post('/auth/reset-password', { email });
|
2025-07-22 13:46:05 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-22 17:01:12 +02:00
|
|
|
/**
|
|
|
|
|
* Confirm password reset
|
|
|
|
|
*/
|
|
|
|
|
async confirmPasswordReset(
|
|
|
|
|
token: string,
|
|
|
|
|
newPassword: string
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
await apiClient.post('/auth/confirm-reset', {
|
|
|
|
|
token,
|
|
|
|
|
new_password: newPassword,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Verify email
|
|
|
|
|
*/
|
|
|
|
|
async verifyEmail(token: string): Promise<void> {
|
|
|
|
|
await apiClient.post('/auth/verify-email', { token });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resend verification email
|
|
|
|
|
*/
|
|
|
|
|
async resendVerification(): Promise<void> {
|
|
|
|
|
await apiClient.post('/auth/resend-verification');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Logout (invalidate tokens)
|
|
|
|
|
*/
|
|
|
|
|
async logout(): Promise<void> {
|
|
|
|
|
await apiClient.post('/auth/logout');
|
2025-07-22 07:37:51 +02:00
|
|
|
}
|
2025-07-22 13:46:05 +02:00
|
|
|
|
2025-07-22 17:01:12 +02:00
|
|
|
/**
|
|
|
|
|
* Get user permissions
|
|
|
|
|
*/
|
|
|
|
|
async getPermissions(): Promise<string[]> {
|
|
|
|
|
const response = await apiClient.get<ApiResponse<string[]>>('/auth/permissions');
|
|
|
|
|
return response.data!;
|
2025-07-22 13:46:05 +02:00
|
|
|
}
|
2025-07-22 07:37:51 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-22 12:58:32 +02:00
|
|
|
export const authService = new AuthService();
|