Add new frontend - fix 6

This commit is contained in:
Urtzi Alfaro
2025-07-22 12:58:32 +02:00
parent 915be54349
commit 5230915bc2
4 changed files with 75 additions and 54 deletions

View File

@@ -1,4 +1,5 @@
// src/api/auth/authService.ts
// File: frontend/src/api/services/authService.ts
import { tokenManager } from '../auth/tokenManager';
import { apiClient } from '../base/apiClient';
@@ -26,41 +27,37 @@ export interface UserProfile {
class AuthService {
async login(credentials: LoginCredentials): Promise<UserProfile> {
// OAuth2 password flow
const formData = new URLSearchParams();
formData.append('username', credentials.email);
formData.append('password', credentials.password);
formData.append('grant_type', 'password');
// FIXED: Use correct endpoint and method
const response = await apiClient.post('/api/v1/auth/login', credentials);
// Store tokens from login response
await tokenManager.storeTokens(response);
const response = await fetch('/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: formData
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Login failed');
// Get user profile from the response or make separate call
if (response.user) {
return response.user;
} else {
return this.getCurrentUser();
}
const tokenResponse = await response.json();
await tokenManager.storeTokens(tokenResponse);
// Get user profile
return this.getCurrentUser();
}
async register(data: RegisterData): Promise<UserProfile> {
const response = await apiClient.post('/auth/register', data);
// FIXED: Use correct endpoint path
const response = await apiClient.post('/api/v1/auth/register', data);
return response;
// Registration only returns user data, NOT tokens
// So we need to login separately to get tokens
await this.login({
email: data.email,
password: data.password
});
return response; // This is the user profile from registration
}
async logout(): Promise<void> {
try {
await apiClient.post('/auth/logout');
await apiClient.post('/api/v1/auth/logout');
} finally {
tokenManager.clearTokens();
window.location.href = '/login';
@@ -68,15 +65,15 @@ class AuthService {
}
async getCurrentUser(): Promise<UserProfile> {
return apiClient.get('/auth/me');
return apiClient.get('/api/v1/auth/me');
}
async updateProfile(updates: Partial<UserProfile>): Promise<UserProfile> {
return apiClient.patch('/auth/profile', updates);
return apiClient.patch('/api/v1/auth/profile', updates);
}
async changePassword(currentPassword: string, newPassword: string): Promise<void> {
await apiClient.post('/auth/change-password', {
await apiClient.post('/api/v1/auth/change-password', {
current_password: currentPassword,
new_password: newPassword
});
@@ -87,4 +84,4 @@ class AuthService {
}
}
export const authService = new AuthService();
export const authService = new AuthService();