2023-07-25 19:37:04 +02:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import System from "../../../models/system";
|
|
|
|
import { AUTH_TOKEN } from "../../../utils/constants";
|
2023-09-13 22:44:36 +02:00
|
|
|
import useLogo from "../../../hooks/useLogo";
|
2023-10-23 22:10:34 +02:00
|
|
|
import paths from "../../../utils/paths";
|
2023-07-25 19:37:04 +02:00
|
|
|
|
|
|
|
export default function SingleUserAuth() {
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const [error, setError] = useState(null);
|
2023-09-13 22:44:36 +02:00
|
|
|
const { logo: _initLogo } = useLogo();
|
2023-07-25 19:37:04 +02:00
|
|
|
const handleLogin = async (e) => {
|
|
|
|
setError(null);
|
|
|
|
setLoading(true);
|
|
|
|
e.preventDefault();
|
|
|
|
const data = {};
|
|
|
|
|
|
|
|
const form = new FormData(e.target);
|
|
|
|
for (var [key, value] of form.entries()) data[key] = value;
|
|
|
|
const { valid, token, message } = await System.requestToken(data);
|
|
|
|
if (valid && !!token) {
|
|
|
|
window.localStorage.setItem(AUTH_TOKEN, token);
|
2023-10-23 22:10:34 +02:00
|
|
|
window.location = paths.home();
|
2023-07-25 19:37:04 +02:00
|
|
|
} else {
|
|
|
|
setError(message);
|
|
|
|
setLoading(false);
|
|
|
|
}
|
|
|
|
setLoading(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<form onSubmit={handleLogin}>
|
2023-10-23 22:10:34 +02:00
|
|
|
<div className="flex flex-col justify-center items-center relative bg-white rounded-2xl shadow dark:bg-stone-700 border-2 border-slate-300 border-opacity-20 w-[400px] login-input-gradient">
|
|
|
|
<div className="flex items-start justify-between pt-11 pb-9 rounded-t dark:border-gray-600">
|
2023-09-13 22:44:36 +02:00
|
|
|
<div className="flex items-center flex-col">
|
2023-10-23 22:10:34 +02:00
|
|
|
<h3 className="text-md md:text-2xl font-bold text-gray-900 dark:text-white text-center">
|
|
|
|
Sign In
|
2023-09-13 22:44:36 +02:00
|
|
|
</h3>
|
|
|
|
</div>
|
2023-07-25 19:37:04 +02:00
|
|
|
</div>
|
2023-10-23 22:10:34 +02:00
|
|
|
<div className="px-12 space-y-6 flex h-full w-full">
|
2023-07-25 19:37:04 +02:00
|
|
|
<div className="w-full flex flex-col gap-y-4">
|
|
|
|
<div>
|
|
|
|
<input
|
|
|
|
name="password"
|
|
|
|
type="password"
|
2023-10-23 22:10:34 +02:00
|
|
|
placeholder="Password"
|
|
|
|
className="bg-neutral-800 bg-opacity-40 border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-[#222628] dark:bg-opacity-40 dark:placeholder-[#FFFFFF99] dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
|
2023-07-25 19:37:04 +02:00
|
|
|
required={true}
|
|
|
|
autoComplete="off"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{error && (
|
|
|
|
<p className="text-red-600 dark:text-red-400 text-sm">
|
|
|
|
Error: {error}
|
|
|
|
</p>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-10-23 22:10:34 +02:00
|
|
|
<div className="flex items-center p-12 space-x-2 border-gray-200 rounded-b dark:border-gray-600 w-full">
|
2023-07-25 19:37:04 +02:00
|
|
|
<button
|
|
|
|
disabled={loading}
|
|
|
|
type="submit"
|
2023-10-23 22:10:34 +02:00
|
|
|
className="text-gray-500 bg-white hover:bg-gray-100 focus:ring-4 focus:outline-none focus:ring-blue-300 rounded-lg border border-white text-sm font-bold px-5 py-2.5 hover:text-gray-900 focus:z-10 dark:bg-white dark:text-neutral-700 dark:border-white dark:hover:text-white dark:hover:bg-slate-600 dark:focus:ring-gray-600 w-full"
|
2023-07-25 19:37:04 +02:00
|
|
|
>
|
2023-10-23 22:10:34 +02:00
|
|
|
{loading ? "Validating..." : "Login"}
|
2023-07-25 19:37:04 +02:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|