diff --git a/src/services/auth.js b/src/services/auth.js index 8260d07..fe5d257 100644 --- a/src/services/auth.js +++ b/src/services/auth.js @@ -4,28 +4,45 @@ export class Auth { this.isAuthenticated = false; } - async login(email, password) { - if (!email || !password) return false; + async login(identifier, password) { + if (!identifier || !password) return false; try { - const response = await fetch(`${import.meta.env.VITE_API_BASE_URL}/api/Auth/login`, { + const apiUrl = `${import.meta.env.VITE_API_BASE_URL}/api/Auth/login`; + const body = { Username: identifier, Password: password }; + console.log('🔐 Login attempt - Datos enviados:', body); + + const response = await fetch(apiUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ email, password }) + body: JSON.stringify(body) }); - if (!response.ok) return false; + console.log('📡 Response status:', response.status, response.ok); - const { token, user } = await response.json(); - if (!token) return false; + if (!response.ok) { + const errorData = await response.json().catch(() => null); + console.error('❌ Error del servidor:', errorData); + return false; + } - this.currentUser = user || { email }; + const data = await response.json(); + console.log('✅ Respuesta exitosa:', data); + + const { token, user } = data; + if (!token) { + console.error('❌ No token in response'); + return false; + } + + this.currentUser = user || { identifier }; this.isAuthenticated = true; localStorage.setItem('user', JSON.stringify(this.currentUser)); localStorage.setItem('token', token); + console.log('✅ Login successful, guardado en localStorage'); return true; } catch (error) { - console.error('Login failed', error); + console.error('❌ Login error:', error); return false; } }