const API_BASE_URL = process.env.REACT_APP_API_URL || 'http://localhost:5000/api'; export interface LoginRequest { email: string; password: string; twoFactorCode?: string; } export interface RegisterRequest { email: string; password: string; firstName: string; lastName: string; phone?: string; company?: string; role?: 'channel_partner_admin' | 'channel_partner_manager' | 'channel_partner_sales' | 'channel_partner_support' | 'channel_partner_finance' | 'channel_partner_analyst' | 'reseller_admin' | 'reseller_manager' | 'reseller_sales' | 'reseller_support' | 'reseller_finance' | 'reseller_analyst' | 'system_admin' | 'system_support' | 'system_analyst' | 'read_only'; userType?: 'channel_partner' | 'reseller' | 'system'; } export interface AuthResponse { success: boolean; message: string; data?: { user: { id: number; email: string; firstName: string; lastName: string; phone?: string; company?: string; role: string; status: string; emailVerified: boolean; twoFactorEnabled: boolean; lastLogin?: string; roles: Array<{ id: number; name: string; description: string; permissions: string[]; }>; }; accessToken: string; refreshToken: string; sessionId: string; }; } export interface User { id: number; email: string; firstName: string; lastName: string; phone?: string; company?: string; role: string; status: string; emailVerified: boolean; twoFactorEnabled: boolean; lastLogin?: string; roles: Array<{ id: number; name: string; description: string; permissions: string[]; }>; } class ApiService { private baseURL: string; constructor() { this.baseURL = API_BASE_URL; } private async request( endpoint: string, options: RequestInit = {} ): Promise { const url = `${this.baseURL}${endpoint}`; const config: RequestInit = { headers: { 'Content-Type': 'application/json', ...options.headers, }, ...options, }; // Add auth token if available const token = localStorage.getItem('accessToken'); if (token) { config.headers = { ...config.headers, Authorization: `Bearer ${token}`, }; } try { const response = await fetch(url, config); const data = await response.json(); if (!response.ok) { throw new Error(data.message || 'API request failed'); } return data; } catch (error) { console.error('API request failed:', error); throw error; } } // Authentication endpoints async login(credentials: LoginRequest): Promise { return this.request('/auth/login', { method: 'POST', body: JSON.stringify(credentials), }); } async register(userData: RegisterRequest): Promise { return this.request('/auth/register', { method: 'POST', body: JSON.stringify(userData), }); } async verifyEmail(email: string, otp: string): Promise { return this.request('/auth/verify-email', { method: 'POST', body: JSON.stringify({ email, otp }), }); } async resendVerificationEmail(email: string): Promise { return this.request('/auth/resend-verification', { method: 'POST', body: JSON.stringify({ email }), }); } async refreshToken(refreshToken: string): Promise { return this.request('/auth/refresh-token', { method: 'POST', body: JSON.stringify({ refreshToken }), }); } async getCurrentUser(): Promise<{ success: boolean; data: User }> { return this.request<{ success: boolean; data: User }>('/auth/me'); } async logout(sessionId: string): Promise { return this.request('/auth/logout', { method: 'POST', body: JSON.stringify({ sessionId }), }); } async forgotPassword(email: string): Promise { return this.request('/auth/forgot-password', { method: 'POST', body: JSON.stringify({ email }), }); } async resetPassword(token: string, password: string): Promise { return this.request('/auth/reset-password', { method: 'POST', body: JSON.stringify({ token, password }), }); } // Two-factor authentication async setupTwoFactor(): Promise { return this.request('/auth/setup-2fa', { method: 'POST', }); } async enableTwoFactor(code: string): Promise { return this.request('/auth/enable-2fa', { method: 'POST', body: JSON.stringify({ code }), }); } async disableTwoFactor(code: string): Promise { return this.request('/auth/disable-2fa', { method: 'POST', body: JSON.stringify({ code }), }); } // Profile management async updateProfile(profileData: Partial): Promise<{ success: boolean; data: User }> { return this.request<{ success: boolean; data: User }>('/auth/profile', { method: 'PUT', body: JSON.stringify(profileData), }); } async changePassword(currentPassword: string, newPassword: string): Promise<{ success: boolean; message: string }> { return this.request<{ success: boolean; message: string }>('/auth/change-password', { method: 'POST', body: JSON.stringify({ currentPassword, newPassword }), }); } } export const apiService = new ApiService(); export default apiService;