|
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() |