Files
front-AT/app/Components/Login/Login.tsx
T

79 lines
2.5 KiB
TypeScript
Raw Normal View History

2025-09-10 16:39:01 -04:00
'use client';
import { useState } from "react";
2025-09-02 19:50:17 -04:00
import "./Login.css";
2025-09-10 21:00:00 -04:00
import { loginUser } from "@/app/lib/login";
2025-09-02 19:50:17 -04:00
function Login() {
2025-09-10 16:39:01 -04:00
const [user, setUser] = useState("");
const [password, setPassword] = useState("");
const [message, setMessage] = useState("");
const handleLogin = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
2025-09-10 21:00:00 -04:00
const data = await loginUser(user, password);
2025-09-10 16:39:01 -04:00
2025-09-10 21:00:00 -04:00
if ("error" in data) {
setMessage(data.error);
} else {
localStorage.setItem("token", data.access_token);
2025-09-10 16:39:01 -04:00
setMessage("Inicio de sesión exitoso");
2025-09-10 21:00:00 -04:00
console.log("Respuesta del servidor:", data);
2025-09-10 16:39:01 -04:00
}
};
2025-09-02 19:50:17 -04:00
return (
2025-09-08 19:35:24 -04:00
<section className='centerGrid containerSection'>
2025-09-02 19:50:17 -04:00
<form
className='login'
2025-09-10 16:39:01 -04:00
onSubmit={handleLogin}
2025-09-02 19:50:17 -04:00
>
2025-09-08 19:35:24 -04:00
<h2 className='title'>Inicio de sesión</h2>
2025-09-02 19:50:17 -04:00
<div className='containerInput'>
<label className='label'>Usuario</label>
<input
type='text'
2025-09-10 16:39:01 -04:00
value={user}
onChange={(e) => setUser(e.target.value)}
2025-09-02 19:50:17 -04:00
placeholder='Coloca tu usuario...'
/>
</div>
<div className='containerInput relative'>
<label className='label'>Contraseña</label>
<input
2025-09-10 16:39:01 -04:00
type='password'
value={password}
onChange={(e) => setPassword(e.target.value)}
2025-09-02 19:50:17 -04:00
placeholder='Coloca tu contraseña...'
/>
<span
2025-09-10 16:39:01 -04:00
//onClick={() => setShowPassword(!showPassword)}
//className={showPassword ? 'eyeOpen' : 'eyeClose'}
2025-09-02 19:50:17 -04:00
/>
</div>
<button
className='button buttonSearch'
style={{ maxWidth: '100%', width: '100%' }}
type='submit'
>Iniciar sesión
</button>
2025-09-10 19:23:18 -04:00
2025-09-10 21:00:00 -04:00
{message &&
<div
className={`messageBox ${message.includes('exitoso') ? 'success' : 'error'}`}
style={{ marginTop: '10px' }}
>
{message}
</div>
}
2025-09-10 19:23:18 -04:00
</form>
2025-09-02 19:50:17 -04:00
</section>
);
}
2025-09-10 16:39:01 -04:00
export default Login;
//IO