2025-08-21 15:07:10 -04:00
|
|
|
import React, { useState, useRef, useEffect } from "react";
|
2025-08-09 07:10:04 -04:00
|
|
|
import { toast } from "react-toastify";
|
|
|
|
import { API_URL } from "@/config";
|
|
|
|
|
|
|
|
export default function LoginPage() {
|
|
|
|
const [username, setUsername] = useState("");
|
|
|
|
const [password, setPassword] = useState("");
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
2025-08-21 15:07:10 -04:00
|
|
|
const passwordRef = useRef();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (passwordRef.current && password === "") {
|
|
|
|
passwordRef.current.value = "";
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
2025-08-09 07:10:04 -04:00
|
|
|
async function handleSubmit(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
setLoading(true);
|
|
|
|
|
|
|
|
try {
|
2025-08-11 15:52:38 -04:00
|
|
|
if (!username) {
|
|
|
|
setLoading(false);
|
2025-08-28 11:15:17 -04:00
|
|
|
if (!toast.isActive("login-username-required-toast")) {
|
|
|
|
return toast.error("Username and password are required",
|
|
|
|
{
|
|
|
|
toastId: "login-missing-data-toast",
|
|
|
|
});
|
|
|
|
}
|
2025-08-11 15:52:38 -04:00
|
|
|
}
|
|
|
|
if (!password) {
|
|
|
|
setLoading(false);
|
2025-08-28 11:15:17 -04:00
|
|
|
if (!toast.isActive("login-password-required-toast")) {
|
|
|
|
return toast.error("Username and password are required",
|
|
|
|
{
|
|
|
|
toastId: "login-missing-data-toast",
|
|
|
|
});
|
|
|
|
}
|
2025-08-11 15:52:38 -04:00
|
|
|
}
|
2025-08-21 15:07:10 -04:00
|
|
|
|
2025-08-09 07:10:04 -04:00
|
|
|
const formData = new URLSearchParams();
|
|
|
|
formData.append("username", username);
|
|
|
|
formData.append("password", password);
|
|
|
|
formData.append("grant_type", "password");
|
|
|
|
formData.append("scope", "");
|
|
|
|
formData.append("client_id", "");
|
|
|
|
formData.append("client_secret", "");
|
|
|
|
|
|
|
|
const resp = await fetch(`${API_URL}/auth/login`, {
|
|
|
|
method: "POST",
|
2025-08-21 15:07:10 -04:00
|
|
|
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
|
|
credentials: "include",
|
2025-08-09 07:10:04 -04:00
|
|
|
body: formData.toString(),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (resp.status === 401) {
|
2025-08-28 11:15:17 -04:00
|
|
|
if (!toast.isActive("login-error-invalid-toast")) {
|
|
|
|
toast.error("Invalid username or password", {
|
|
|
|
toastId: "login-error-invalid-toast",
|
|
|
|
});
|
|
|
|
}
|
2025-08-09 07:10:04 -04:00
|
|
|
setLoading(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!resp.ok) {
|
2025-08-21 15:07:10 -04:00
|
|
|
const data = await resp.json().catch(() => ({}));
|
2025-08-28 11:15:17 -04:00
|
|
|
if (!toast.isActive("login-error-failed-toast")) {
|
|
|
|
toast.error(data.detail ? `Login failed: ${data.detail}` : "Login failed",
|
|
|
|
{
|
|
|
|
toastId: "login-error-failed-toast",
|
|
|
|
});
|
|
|
|
}
|
2025-08-09 07:10:04 -04:00
|
|
|
setLoading(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = await resp.json();
|
|
|
|
if (data.access_token) {
|
2025-08-28 11:15:17 -04:00
|
|
|
if (!toast.isActive("login-success-toast")) {
|
|
|
|
toast.success("Login successful!",
|
|
|
|
{
|
|
|
|
toastId: "login-success-toast",
|
|
|
|
});
|
|
|
|
}
|
2025-08-14 13:26:51 -04:00
|
|
|
window.location.href = "/TRip"; // TODO: fix, hardcoded
|
2025-08-09 07:10:04 -04:00
|
|
|
} else {
|
2025-08-28 11:15:17 -04:00
|
|
|
if (!toast.isActive("login-error-no-token-toast")) {
|
|
|
|
toast.error("Login failed: no access token received",
|
|
|
|
{
|
|
|
|
toastId: "login-error-no-token-toast",
|
|
|
|
});
|
|
|
|
setLoading(false);
|
|
|
|
}
|
2025-08-09 07:10:04 -04:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2025-08-28 11:15:17 -04:00
|
|
|
if (!toast.isActive("login-error-network-toast")) {
|
|
|
|
toast.error("Network error during login",
|
|
|
|
{
|
|
|
|
toastId: "login-error-network-toast",
|
|
|
|
});
|
|
|
|
}
|
2025-08-09 07:10:04 -04:00
|
|
|
console.error("Login error:", error);
|
|
|
|
setLoading(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2025-08-21 15:07:10 -04:00
|
|
|
<div className="flex items-start justify-center bg-gray-50 dark:bg-[#121212] px-4 pt-20 py-10">
|
2025-08-09 07:10:04 -04:00
|
|
|
<div className="max-w-md w-full bg-white dark:bg-[#1E1E1E] rounded-2xl shadow-xl px-10 pb-6">
|
|
|
|
<h2 className="flex flex-col items-center text-3xl font-semibold text-gray-900 dark:text-white mb-8 font-sans">
|
|
|
|
<img className="logo-auth mb-4" src="/images/kode.png" alt="Logo" />
|
|
|
|
Authentication Required
|
|
|
|
</h2>
|
|
|
|
|
2025-08-21 15:07:10 -04:00
|
|
|
<form className="space-y-6 relative" onSubmit={handleSubmit} noValidate>
|
|
|
|
{/* Username */}
|
|
|
|
<div className="relative">
|
2025-08-09 07:10:04 -04:00
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
id="username"
|
|
|
|
name="username"
|
2025-08-11 15:52:38 -04:00
|
|
|
autoComplete="off"
|
2025-08-09 07:10:04 -04:00
|
|
|
value={username}
|
|
|
|
onChange={(e) => setUsername(e.target.value)}
|
|
|
|
required
|
|
|
|
disabled={loading}
|
2025-08-21 15:07:10 -04:00
|
|
|
className="peer block w-full px-4 pt-5 pb-2 border border-gray-300 dark:border-gray-700 rounded-lg bg-transparent text-gray-900 dark:text-white placeholder-transparent focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
|
2025-08-09 07:10:04 -04:00
|
|
|
/>
|
|
|
|
<label
|
2025-08-21 15:07:10 -04:00
|
|
|
htmlFor="username"
|
|
|
|
className="absolute left-4 top-2 text-gray-500 dark:text-gray-400 text-sm transition-all
|
|
|
|
peer-placeholder-shown:top-5 peer-placeholder-shown:text-gray-400 peer-placeholder-shown:text-base
|
|
|
|
peer-focus:top-2 peer-focus:text-sm peer-focus:text-blue-500 dark:peer-focus:text-blue-400"
|
2025-08-09 07:10:04 -04:00
|
|
|
>
|
2025-08-21 15:07:10 -04:00
|
|
|
Username
|
2025-08-09 07:10:04 -04:00
|
|
|
</label>
|
2025-08-21 15:07:10 -04:00
|
|
|
</div>
|
|
|
|
|
|
|
|
{/* Password */}
|
|
|
|
<div className="relative">
|
2025-08-09 07:10:04 -04:00
|
|
|
<input
|
|
|
|
type="password"
|
|
|
|
id="password"
|
|
|
|
name="password"
|
2025-08-21 15:07:10 -04:00
|
|
|
autoComplete="new-password"
|
|
|
|
spellCheck="false"
|
|
|
|
ref={passwordRef}
|
2025-08-09 07:10:04 -04:00
|
|
|
value={password}
|
|
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
|
|
required
|
|
|
|
disabled={loading}
|
2025-08-21 15:07:10 -04:00
|
|
|
className="peer block w-full px-4 pt-5 pb-2 border border-gray-300 dark:border-gray-700 rounded-lg bg-transparent text-gray-900 dark:text-white placeholder-transparent focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
|
2025-08-09 07:10:04 -04:00
|
|
|
/>
|
2025-08-21 15:07:10 -04:00
|
|
|
<label
|
|
|
|
htmlFor="password"
|
|
|
|
className="absolute left-4 top-2 text-gray-500 dark:text-gray-400 text-sm transition-all
|
|
|
|
peer-placeholder-shown:top-5 peer-placeholder-shown:text-gray-400 peer-placeholder-shown:text-base
|
|
|
|
peer-focus:top-2 peer-focus:text-sm peer-focus:text-blue-500 dark:peer-focus:text-blue-400"
|
|
|
|
>
|
|
|
|
Password
|
|
|
|
</label>
|
2025-08-09 07:10:04 -04:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
disabled={loading}
|
|
|
|
className={`w-full py-3 bg-blue-600 hover:bg-blue-700 focus:ring-4 focus:ring-blue-300 text-white rounded-lg font-semibold shadow-md transition-colors ${loading ? "opacity-60 cursor-not-allowed" : ""
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
{loading ? "Signing In..." : "Sign In"}
|
|
|
|
</button>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|