File size: 1,755 Bytes
2c6bb7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { User, LoginRequest, RegisterRequest } from '../../../shared/types'
import { apiService } from './api'

interface AuthResponse {
  user: User
  token: string
}

class AuthService {
  async login(credentials: LoginRequest): Promise<AuthResponse> {
    return await apiService.post<AuthResponse>('/api/auth/login', credentials)
  }

  async register(userData: RegisterRequest): Promise<AuthResponse> {
    return await apiService.post<AuthResponse>('/api/auth/register', userData)
  }

  async logout(): Promise<void> {
    try {
      await apiService.post('/api/auth/logout')
    } catch (error) {
      // Ignore logout errors, clear local storage anyway
      console.warn('Logout request failed:', error)
    }
  }

  async getCurrentUser(): Promise<User> {
    return await apiService.get<User>('/api/auth/me')
  }

  async refreshToken(): Promise<AuthResponse> {
    return await apiService.post<AuthResponse>('/api/auth/refresh')
  }

  async changePassword(data: {
    currentPassword: string
    newPassword: string
  }): Promise<void> {
    await apiService.post('/api/auth/change-password', data)
  }

  async requestPasswordReset(email: string): Promise<void> {
    await apiService.post('/api/auth/forgot-password', { email })
  }

  async resetPassword(data: {
    token: string
    newPassword: string
  }): Promise<void> {
    await apiService.post('/api/auth/reset-password', data)
  }

  async verifyEmail(token: string): Promise<void> {
    await apiService.post('/api/auth/verify-email', { token })
  }

  async resendVerificationEmail(): Promise<void> {
    await apiService.post('/api/auth/resend-verification')
  }
}

export const authService = new AuthService()