Merge branch 'main' into Josep
This commit is contained in:
commit
eae9f65a87
|
|
@ -4,28 +4,45 @@ export class Auth {
|
||||||
this.isAuthenticated = false;
|
this.isAuthenticated = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async login(email, password) {
|
async login(identifier, password) {
|
||||||
if (!email || !password) return false;
|
if (!identifier || !password) return false;
|
||||||
|
|
||||||
try {
|
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',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
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 (!response.ok) {
|
||||||
if (!token) return false;
|
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;
|
this.isAuthenticated = true;
|
||||||
localStorage.setItem('user', JSON.stringify(this.currentUser));
|
localStorage.setItem('user', JSON.stringify(this.currentUser));
|
||||||
localStorage.setItem('token', token);
|
localStorage.setItem('token', token);
|
||||||
|
console.log('✅ Login successful, guardado en localStorage');
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Login failed', error);
|
console.error('❌ Login error:', error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue