2025-07-22 12:58:32 +02:00
|
|
|
// File: frontend/src/api/services/authService.ts
|
|
|
|
|
|
2025-07-22 08:50:18 +02:00
|
|
|
import { tokenManager } from '../auth/tokenManager';
|
2025-07-22 07:37:51 +02:00
|
|
|
import { apiClient } from '../base/apiClient';
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
created_at: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class AuthService {
|
|
|
|
|
async login(credentials: LoginCredentials): Promise<UserProfile> {
|
2025-07-22 12:58:32 +02:00
|
|
|
// 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);
|
2025-07-22 07:37:51 +02:00
|
|
|
|
2025-07-22 12:58:32 +02:00
|
|
|
// Get user profile from the response or make separate call
|
|
|
|
|
if (response.user) {
|
|
|
|
|
return response.user;
|
|
|
|
|
} else {
|
|
|
|
|
return this.getCurrentUser();
|
2025-07-22 07:37:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async register(data: RegisterData): Promise<UserProfile> {
|
2025-07-22 12:58:32 +02:00
|
|
|
// FIXED: Use correct endpoint path
|
|
|
|
|
const response = await apiClient.post('/api/v1/auth/register', data);
|
2025-07-22 07:37:51 +02:00
|
|
|
|
2025-07-22 12:58:32 +02:00
|
|
|
// 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
|
2025-07-22 07:37:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async logout(): Promise<void> {
|
|
|
|
|
try {
|
2025-07-22 12:58:32 +02:00
|
|
|
await apiClient.post('/api/v1/auth/logout');
|
2025-07-22 07:37:51 +02:00
|
|
|
} finally {
|
|
|
|
|
tokenManager.clearTokens();
|
|
|
|
|
window.location.href = '/login';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getCurrentUser(): Promise<UserProfile> {
|
2025-07-22 12:58:32 +02:00
|
|
|
return apiClient.get('/api/v1/auth/me');
|
2025-07-22 07:37:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async updateProfile(updates: Partial<UserProfile>): Promise<UserProfile> {
|
2025-07-22 12:58:32 +02:00
|
|
|
return apiClient.patch('/api/v1/auth/profile', updates);
|
2025-07-22 07:37:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async changePassword(currentPassword: string, newPassword: string): Promise<void> {
|
2025-07-22 12:58:32 +02:00
|
|
|
await apiClient.post('/api/v1/auth/change-password', {
|
2025-07-22 07:37:51 +02:00
|
|
|
current_password: currentPassword,
|
|
|
|
|
new_password: newPassword
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isAuthenticated(): boolean {
|
|
|
|
|
return tokenManager.isAuthenticated();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-22 12:58:32 +02:00
|
|
|
export const authService = new AuthService();
|