Refactor login method to use unified identifier for user input; update request body to support both email and username
This commit is contained in:
parent
7a36a0d4f0
commit
3c901a35a6
|
|
@ -4,14 +4,19 @@ 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`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ email, password })
|
||||
// Enviamos ambos nombres de campo para cubrir email o userName según espere el backend.
|
||||
body: JSON.stringify({
|
||||
email: identifier,
|
||||
userName: identifier,
|
||||
password
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) return false;
|
||||
|
|
@ -19,7 +24,7 @@ export class Auth {
|
|||
const { token, user } = await response.json();
|
||||
if (!token) return false;
|
||||
|
||||
this.currentUser = user || { email };
|
||||
this.currentUser = user || { email: identifier };
|
||||
this.isAuthenticated = true;
|
||||
localStorage.setItem('user', JSON.stringify(this.currentUser));
|
||||
localStorage.setItem('token', token);
|
||||
|
|
|
|||
Loading…
Reference in New Issue