| 123456789101112131415161718192021222324252627282930313233 |
- async function login(email,pin){
- const response = await fetch("/api/users/login", {
- method: "POST",
- headers: {
- "Content-Type": "application/json"
- },
- body: JSON.stringify({ email, pin })
- }
- );
- if (response.status == 404) {
- const errorData = await response.json().catch(() => ({ message: "El usuario no fue encontrado." }));
- throw new Error(errorData.message);
- }else if (response.status == 401) {
- const errorData = await response.json().catch(() => ({ message: "Credenciales incorrectas.", attempts: 0 }));
- alert(errorData.message);
- throw new Error(errorData.message);
- }else if (response.status != 200) {
- console.error(response.status, response.statusText);
- const errorData = await response.json().catch(() => ({ message: "Error al iniciar sesión." }));
- alert(errorData.message, `Intentos restantes: ${errorData.attempts_remaining || 0}`);
- throw new Error(errorData.message);
- }
- const data = await response.json();
- if (!data || !data.data.token) {
- alert("Error al iniciar sesión.");
- throw new Error("Error al iniciar sesión.");
- }
- return data;
- }
- export { login };
|