Update page.tsx

This commit is contained in:
NotII
2025-02-27 20:30:34 +00:00
parent 517fa75acd
commit 64853f9ff6

View File

@@ -9,17 +9,19 @@ import { Button } from "@/components/ui/button";
import { Checkbox } from "@/components/ui/checkbox"; import { Checkbox } from "@/components/ui/checkbox";
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label"; import { Label } from "@/components/ui/label";
import { toast } from "sonner";
export default function LoginPage() { export default function LoginPage() {
const [username, setUsername] = useState(""); const [username, setUsername] = useState("");
const [password, setPassword] = useState(""); const [password, setPassword] = useState("");
const [error, setError] = useState(""); const [isLoading, setIsLoading] = useState(false);
const router = useRouter(); const router = useRouter();
async function handleLogin(e: React.FormEvent) { async function handleLogin(e: React.FormEvent) {
e.preventDefault(); e.preventDefault();
setError(""); setIsLoading(true);
try {
const res = await fetchData(`${process.env.NEXT_PUBLIC_API_URL}/auth/login`, { const res = await fetchData(`${process.env.NEXT_PUBLIC_API_URL}/auth/login`, {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
@@ -31,7 +33,20 @@ export default function LoginPage() {
router.push("/dashboard"); router.push("/dashboard");
} else { } else {
const data = await res.json(); const data = await res.json();
setError(data.error || "Invalid credentials"); const errorMessage = data.error || "Invalid credentials";
// Show toast notification for failed login
toast.error("Login Failed", {
description: errorMessage,
});
}
} catch (error) {
// Handle network errors or other exceptions
toast.error("Login Error", {
description: "An error occurred while trying to log in. Please try again.",
});
} finally {
setIsLoading(false);
} }
} }
@@ -43,12 +58,10 @@ export default function LoginPage() {
<p className="mt-2 text-sm text-gray-600 dark:text-gray-400">Please sign in to your account</p> <p className="mt-2 text-sm text-gray-600 dark:text-gray-400">Please sign in to your account</p>
</div> </div>
{error && <p className="text-red-500 text-sm text-center">{error}</p>} {/* ✅ Display login errors */}
<form className="mt-8 space-y-6" onSubmit={handleLogin}> <form className="mt-8 space-y-6" onSubmit={handleLogin}>
<div className="space-y-4"> <div className="space-y-4">
<div> <div>
<Label htmlFor="username">Username</Label> {/* ✅ Changed Email to Username */} <Label htmlFor="username">Username</Label>
<Input <Input
id="username" id="username"
name="username" name="username"
@@ -75,9 +88,8 @@ export default function LoginPage() {
</div> </div>
</div> </div>
<Button type="submit" className="w-full" disabled={isLoading}>
<Button type="submit" className="w-full"> {isLoading ? "Signing in..." : "Sign in"}
Sign in
</Button> </Button>
</form> </form>