// 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 { const response = await apiClient.get>('/users/me'); return response.data!; } /** * User login */ async login(credentials: LoginRequest): Promise { const response = await apiClient.post>( '/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 { const response = await apiClient.post>( '/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 { const response = await apiClient.post>( '/auth/refresh', { refresh_token: refreshToken } ); return response.data!; } /** * Get current user profile (alias for getCurrentUser) */ async getProfile(): Promise { return this.getCurrentUser(); } /** * Update user profile */ async updateProfile(updates: Partial): Promise { const response = await apiClient.put>( '/users/me', updates ); return response.data!; } /** * Change password */ async changePassword( currentPassword: string, newPassword: string ): Promise { await apiClient.post('/auth/change-password', { current_password: currentPassword, new_password: newPassword, }); } /** * Request password reset */ async requestPasswordReset(email: string): Promise { await apiClient.post('/auth/reset-password', { email }); } /** * Confirm password reset */ async confirmPasswordReset( token: string, newPassword: string ): Promise { await apiClient.post('/auth/confirm-reset', { token, new_password: newPassword, }); } /** * Verify email */ async verifyEmail(token: string): Promise { await apiClient.post('/auth/verify-email', { token }); } /** * Resend verification email */ async resendVerification(): Promise { await apiClient.post('/auth/resend-verification'); } /** * Logout (invalidate tokens) */ async logout(): Promise { 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 { const response = await apiClient.get>('/auth/permissions'); return response.data!; } } export const authService = new AuthService();