File size: 1,163 Bytes
84121fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const API_BASE = '/api'

interface AuthResponse {
  success: boolean
  token?: string
  message?: string
}

class AuthService {
  async validateCode(code: string): Promise<AuthResponse> {
    const response = await fetch(`${API_BASE}/auth`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ code }),
    })

    if (!response.ok) {
      throw new Error('Error de autenticación')
    }

    return response.json()
  }

  async logout(): Promise<void> {
    try {
      await fetch(`${API_BASE}/logout`, {
        method: 'POST',
        headers: {
          'Authorization': `Bearer ${localStorage.getItem('daddytv_token')}`,
        },
      })
    } catch (error) {
      console.error('Logout error:', error)
    }
  }

  async ping(): Promise<boolean> {
    try {
      const response = await fetch(`${API_BASE}/ping`, {
        method: 'POST',
        headers: {
          'Authorization': `Bearer ${localStorage.getItem('daddytv_token')}`,
        },
      })
      return response.ok
    } catch (error) {
      return false
    }
  }
}

export const authService = new AuthService()