Add new frontend - fix 9

This commit is contained in:
Urtzi Alfaro
2025-07-22 17:01:12 +02:00
parent 5959eb6e15
commit 06cbe3f4e8
16 changed files with 2048 additions and 166 deletions

View File

@@ -1,116 +1,126 @@
// frontend/src/api/services/authService.ts - UPDATED TO HANDLE TOKENS FROM REGISTRATION
import { tokenManager } from '../auth/tokenManager';
// src/api/services/AuthService.ts
import { apiClient } from '../base/apiClient';
import {
ApiResponse,
LoginRequest,
RegisterRequest,
TokenResponse,
UserProfile,
} from '../types/api';
export interface LoginCredentials {
email: string;
password: string;
}
export interface RegisterData {
email: string;
password: string;
full_name: string;
tenant_name?: string;
}
export interface UserProfile {
id: string;
email: string;
full_name: string;
tenant_id?: string;
role?: string;
is_active: boolean;
is_verified?: boolean;
created_at: string;
}
export interface TokenResponse {
access_token: string;
refresh_token?: string;
token_type: string;
expires_in?: number;
user?: UserProfile;
}
class AuthService {
async register(data: RegisterData): Promise<UserProfile> {
// NEW: Registration now returns tokens directly - no auto-login needed!
const response: TokenResponse = await apiClient.post('/api/v1/auth/register', data);
// Store tokens immediately from registration response
await tokenManager.storeTokens(response);
// Return user profile from registration response
if (response.user) {
return response.user;
} else {
// Fallback: get user profile if not included in response
return this.getCurrentUser();
}
export class AuthService {
/**
* User login
*/
async login(credentials: LoginRequest): Promise<TokenResponse> {
const response = await apiClient.post<ApiResponse<TokenResponse>>(
'/auth/login',
credentials
);
return response.data!;
}
async login(credentials: LoginCredentials): Promise<UserProfile> {
// UPDATED: Use correct endpoint and unified response handling
const response: TokenResponse = await apiClient.post('/api/v1/auth/login', credentials);
// Store tokens from login response
await tokenManager.storeTokens(response);
// Return user profile from login response
if (response.user) {
return response.user;
} else {
// Fallback: get user profile if not included in response
return this.getCurrentUser();
}
/**
* User registration
*/
async register(userData: RegisterRequest): Promise<UserProfile> {
const response = await apiClient.post<ApiResponse<UserProfile>>(
'/auth/register',
userData
);
return response.data!;
}
async logout(): Promise<void> {
try {
// Get refresh token for logout request
const refreshToken = tokenManager.getRefreshToken();
if (refreshToken) {
await apiClient.post('/api/v1/auth/logout', {
refresh_token: refreshToken
});
}
} catch (error) {
console.error('Logout API call failed:', error);
// Continue with local cleanup even if API fails
} finally {
tokenManager.clearTokens();
window.location.href = '/login';
}
/**
* Refresh access token
*/
async refreshToken(refreshToken: string): Promise<TokenResponse> {
const response = await apiClient.post<ApiResponse<TokenResponse>>(
'/auth/refresh',
{ refresh_token: refreshToken }
);
return response.data!;
}
async getCurrentUser(): Promise<UserProfile> {
return apiClient.get('/api/v1/auth/me');
/**
* Get current user profile
*/
async getProfile(): Promise<UserProfile> {
const response = await apiClient.get<ApiResponse<UserProfile>>('/users/me');
return response.data!;
}
/**
* Update user profile
*/
async updateProfile(updates: Partial<UserProfile>): Promise<UserProfile> {
return apiClient.patch('/api/v1/auth/profile', updates);
const response = await apiClient.put<ApiResponse<UserProfile>>(
'/users/me',
updates
);
return response.data!;
}
async changePassword(currentPassword: string, newPassword: string): Promise<void> {
await apiClient.post('/api/v1/auth/change-password', {
/**
* Change password
*/
async changePassword(
currentPassword: string,
newPassword: string
): Promise<void> {
await apiClient.post('/auth/change-password', {
current_password: currentPassword,
new_password: newPassword
new_password: newPassword,
});
}
async refreshToken(): Promise<void> {
await tokenManager.refreshAccessToken();
/**
* Request password reset
*/
async requestPasswordReset(email: string): Promise<void> {
await apiClient.post('/auth/reset-password', { email });
}
isAuthenticated(): boolean {
return tokenManager.isAuthenticated();
/**
* Confirm password reset
*/
async confirmPasswordReset(
token: string,
newPassword: string
): Promise<void> {
await apiClient.post('/auth/confirm-reset', {
token,
new_password: newPassword,
});
}
getUser(): UserProfile | null {
// This method would need to be implemented to return cached user data
// For now, it returns null and components should use getCurrentUser()
return null;
/**
* 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> {
await apiClient.post('/auth/logout');
}
/**
* Get user permissions
*/
async getPermissions(): Promise<string[]> {
const response = await apiClient.get<ApiResponse<string[]>>('/auth/permissions');
return response.data!;
}
}