Files
bakery-ia/frontend/src/api/services/auth.ts

116 lines
3.9 KiB
TypeScript
Raw Normal View History

2025-10-06 15:27:01 +02:00
// ================================================================
// frontend/src/api/services/auth.ts
// ================================================================
/**
2025-10-06 15:27:01 +02:00
* Auth Service - Complete backend alignment
*
* Backend API structure (3-tier architecture):
* - ATOMIC: users.py
* - OPERATIONS: auth_operations.py, onboarding_progress.py
*
* Last Updated: 2025-10-05
* Status: Complete - Zero drift with backend
*/
import { apiClient } from '../client';
import {
UserRegistration,
UserLogin,
TokenResponse,
RefreshTokenRequest,
PasswordChange,
PasswordReset,
UserResponse,
UserUpdate,
TokenVerificationResponse,
AuthHealthResponse,
} from '../types/auth';
export class AuthService {
private readonly baseUrl = '/auth';
2025-10-06 15:27:01 +02:00
// ===================================================================
// OPERATIONS: Authentication
// Backend: services/auth/app/api/auth_operations.py
// ===================================================================
async register(userData: UserRegistration): Promise<TokenResponse> {
return apiClient.post<TokenResponse>(`${this.baseUrl}/register`, userData);
}
async login(loginData: UserLogin): Promise<TokenResponse> {
return apiClient.post<TokenResponse>(`${this.baseUrl}/login`, loginData);
}
async refreshToken(refreshToken: string): Promise<TokenResponse> {
const refreshData: RefreshTokenRequest = { refresh_token: refreshToken };
return apiClient.post<TokenResponse>(`${this.baseUrl}/refresh`, refreshData);
}
async verifyToken(token?: string): Promise<TokenVerificationResponse> {
// If token is provided, temporarily set it; otherwise use current token
const currentToken = apiClient.getAuthToken();
if (token && token !== currentToken) {
apiClient.setAuthToken(token);
}
const response = await apiClient.post<TokenVerificationResponse>(`${this.baseUrl}/verify`);
// Restore original token if we temporarily changed it
if (token && token !== currentToken) {
apiClient.setAuthToken(currentToken);
}
return response;
}
async logout(refreshToken: string): Promise<{ message: string }> {
const refreshData: RefreshTokenRequest = { refresh_token: refreshToken };
return apiClient.post<{ message: string }>(`${this.baseUrl}/logout`, refreshData);
}
async changePassword(passwordData: PasswordChange): Promise<{ message: string }> {
return apiClient.post<{ message: string }>(`${this.baseUrl}/change-password`, passwordData);
}
async resetPassword(resetData: PasswordReset): Promise<{ message: string }> {
return apiClient.post<{ message: string }>(`${this.baseUrl}/reset-password`, resetData);
}
2025-10-06 15:27:01 +02:00
// ===================================================================
// ATOMIC: User Profile
// Backend: services/auth/app/api/users.py
// ===================================================================
async getProfile(): Promise<UserResponse> {
2025-09-11 18:21:32 +02:00
return apiClient.get<UserResponse>('/users/me');
}
async updateProfile(updateData: UserUpdate): Promise<UserResponse> {
2025-09-11 18:21:32 +02:00
return apiClient.put<UserResponse>('/users/me', updateData);
}
2025-10-06 15:27:01 +02:00
// ===================================================================
// OPERATIONS: Email Verification
// Backend: services/auth/app/api/auth_operations.py
// ===================================================================
async verifyEmail(
userId: string,
verificationToken: string
): Promise<{ message: string }> {
return apiClient.post<{ message: string }>(`${this.baseUrl}/verify-email`, {
user_id: userId,
verification_token: verificationToken,
});
}
2025-10-06 15:27:01 +02:00
// ===================================================================
// Health Check
// ===================================================================
async healthCheck(): Promise<AuthHealthResponse> {
return apiClient.get<AuthHealthResponse>(`${this.baseUrl}/health`);
}
}
export const authService = new AuthService();