Files
bakery-ia/frontend/src/api/services/authService.ts
2025-07-22 20:22:27 +02:00

166 lines
3.8 KiB
TypeScript

// src/api/services/AuthService.ts - UPDATED with missing methods
import { apiClient } from '../base/apiClient';
import { tokenManager } from '../auth/tokenManager';
import {
ApiResponse,
LoginRequest,
RegisterRequest,
TokenResponse,
UserProfile,
} from '../types/api';
export class AuthService {
/**
* Check if user is authenticated (has valid token)
* Note: This is a synchronous check using the tokenManager's isAuthenticated method
*/
isAuthenticated(): boolean {
try {
return tokenManager.isAuthenticated();
} catch (error) {
console.error('Error checking authentication status:', error);
return false;
}
}
/**
* Get current user profile
*/
async getCurrentUser(): Promise<UserProfile> {
const response = await apiClient.get<ApiResponse<UserProfile>>('/users/me');
return response.data!;
}
/**
* User login
*/
async login(credentials: LoginRequest): Promise<TokenResponse> {
const response = await apiClient.post<ApiResponse<TokenResponse>>(
'/auth/login',
credentials
);
// Store tokens after successful login
const tokenData = response.data!;
await tokenManager.storeTokens(tokenData);
return tokenData;
}
/**
* User registration
*/
async register(userData: RegisterRequest): Promise<TokenResponse> {
const response = await apiClient.post<ApiResponse<TokenResponse>>(
'/auth/register',
userData
);
// Store tokens after successful registration
const tokenData = response.data!;
await tokenManager.storeTokens(tokenData);
return tokenData;
}
/**
* Refresh access token
*/
async refreshToken(refreshToken: string): Promise<TokenResponse> {
const response = await apiClient.post<ApiResponse<TokenResponse>>(
'/auth/refresh',
{ refresh_token: refreshToken }
);
return response.data!;
}
/**
* Get current user profile (alias for getCurrentUser)
*/
async getProfile(): Promise<UserProfile> {
return this.getCurrentUser();
}
/**
* Update user profile
*/
async updateProfile(updates: Partial<UserProfile>): Promise<UserProfile> {
const response = await apiClient.put<ApiResponse<UserProfile>>(
'/users/me',
updates
);
return response.data!;
}
/**
* Change password
*/
async changePassword(
currentPassword: string,
newPassword: string
): Promise<void> {
await apiClient.post('/auth/change-password', {
current_password: currentPassword,
new_password: newPassword,
});
}
/**
* Request password reset
*/
async requestPasswordReset(email: string): Promise<void> {
await apiClient.post('/auth/reset-password', { email });
}
/**
* 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> {
try {
await apiClient.post('/auth/logout');
} catch (error) {
console.error('Logout API call failed:', error);
} finally {
// Always clear tokens regardless of API call success
tokenManager.clearTokens();
}
}
/**
* Get user permissions
*/
async getPermissions(): Promise<string[]> {
const response = await apiClient.get<ApiResponse<string[]>>('/auth/permissions');
return response.data!;
}
}
export const authService = new AuthService();