Add new frontend - fix 12

This commit is contained in:
Urtzi Alfaro
2025-07-22 19:40:12 +02:00
parent 6717ce7e0d
commit 5dffe39706
5 changed files with 65 additions and 680 deletions

View File

@@ -1,5 +1,6 @@
// src/api/services/AuthService.ts
// src/api/services/AuthService.ts - UPDATED with missing methods
import { apiClient } from '../base/apiClient';
import { tokenManager } from '../auth/tokenManager';
import {
ApiResponse,
LoginRequest,
@@ -9,6 +10,27 @@ import {
} 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
*/
@@ -17,7 +39,12 @@ export class AuthService {
'/auth/login',
credentials
);
return response.data!;
// Store tokens after successful login
const tokenData = response.data!;
await tokenManager.storeTokens(tokenData);
return tokenData;
}
/**
@@ -43,11 +70,10 @@ export class AuthService {
}
/**
* Get current user profile
* Get current user profile (alias for getCurrentUser)
*/
async getProfile(): Promise<UserProfile> {
const response = await apiClient.get<ApiResponse<UserProfile>>('/users/me');
return response.data!;
return this.getCurrentUser();
}
/**
@@ -112,7 +138,14 @@ export class AuthService {
* Logout (invalidate tokens)
*/
async logout(): Promise<void> {
await apiClient.post('/auth/logout');
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();
}
}
/**