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

83 lines
2.3 KiB
TypeScript
Raw Normal View History

2025-09-12 20:40:17 -06:00
"use client";
2025-09-10 16:39:01 -04:00
import { useState } from "react";
2025-09-10 21:00:00 -04:00
import { loginUser } from "@/app/lib/login";
2025-09-12 20:40:17 -06:00
import { useRouter } from "next/navigation";
2025-09-02 19:50:17 -04:00
2025-09-24 15:49:59 -06:00
import "./Login.css";
2025-09-26 16:56:07 -06:00
import AlertBox from "../../Global/AlertBox/AlertBox";
2025-09-12 18:12:38 -06:00
2025-09-02 19:50:17 -04:00
function Login() {
2025-09-12 20:40:17 -06:00
const [user, setUser] = useState("");
const [password, setPassword] = useState("");
2025-09-19 17:20:05 -06:00
const [error, setError] = useState("");
2025-09-22 13:27:42 -06:00
const [alert, setAlert] = useState("");
2025-09-10 16:39:01 -04:00
2025-09-12 20:40:17 -06:00
const router = useRouter();
2025-09-10 16:39:01 -04:00
2025-09-12 20:40:17 -06:00
const handleLogin = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
2025-09-10 16:39:01 -04:00
2025-09-12 20:40:17 -06:00
const data = await loginUser(user, password);
2025-09-10 16:39:01 -04:00
2025-09-12 20:40:17 -06:00
if ("error" in data) {
2025-09-19 17:20:05 -06:00
setError(data.error);
2025-09-12 20:40:17 -06:00
} else {
2025-09-19 17:20:05 -06:00
const token = data.access_token;
const payload = JSON.parse(atob(token.split(".")[1]));
2025-09-24 17:51:54 -06:00
const usuario = payload.usuario;
2025-09-19 17:20:05 -06:00
document.cookie = `token=${token}; path=/; SameSite=Strict`;
2025-09-24 17:51:54 -06:00
document.cookie = `usuario=${usuario}; path=/; SameSite=Strict`;
2025-09-19 17:20:05 -06:00
2025-09-22 13:27:42 -06:00
setAlert("Inicio de sesión exitoso");
2025-09-12 20:40:17 -06:00
router.push("/Impresiones");
}
};
2025-09-02 19:50:17 -04:00
2025-09-12 20:40:17 -06:00
return (
<section className="centerGrid containerSection">
<form className="login" onSubmit={handleLogin}>
<h2 className="title">Inicio de sesión</h2>
2025-09-02 19:50:17 -04:00
2025-09-12 20:40:17 -06:00
<div className="containerInput">
<label className="label">Usuario</label>
<input
type="text"
value={user}
onChange={(e) => setUser(e.target.value)}
placeholder="Coloca tu usuario..."
/>
</div>
2025-09-02 19:50:17 -04:00
2025-09-12 20:40:17 -06:00
<div className="containerInput relative">
<label className="label">Contraseña</label>
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="Coloca tu contraseña..."
/>
<span
//onClick={() => setShowPassword(!showPassword)}
//className={showPassword ? 'eyeOpen' : 'eyeClose'}
/>
</div>
2025-09-02 19:50:17 -04:00
2025-09-12 20:40:17 -06:00
<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-19 17:20:05 -06:00
{error && <AlertBox message={error} type="error" />}
2025-09-22 13:27:42 -06:00
{alert && <AlertBox message={alert} type="success" />}
2025-09-12 20:40:17 -06:00
</form>
</section>
);
2025-09-02 19:50:17 -04:00
}
2025-09-10 16:39:01 -04:00
export default Login;
2025-09-12 20:40:17 -06:00
//IO