"use client"; import { fetchData } from "@/lib/data-service"; import { useState } from "react"; import { useRouter } from "next/navigation"; import Image from "next/image"; import Link from "next/link"; import { Button } from "@/components/ui/button"; import { Checkbox } from "@/components/ui/checkbox"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { toast } from "sonner"; export default function LoginPage() { const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [isLoading, setIsLoading] = useState(false); const router = useRouter(); async function handleLogin(e: React.FormEvent) { e.preventDefault(); setIsLoading(true); try { const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/auth/login`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ username, password }), }); const data = await response.json(); if (response.ok && data.token) { document.cookie = `Authorization=${data.token}; path=/; Secure; SameSite=Strict; max-age=604800`; router.push("/dashboard"); } else { // Handle HTTP error responses (including 401) const errorMessage = data.error || "Invalid credentials"; toast.error("Login Failed", { description: errorMessage, }); } } catch (error) { // This will now only catch network errors or JSON parsing errors toast.error("Connection Error", { description: "Unable to connect to the server. Please check your internet connection and try again.", }); } finally { setIsLoading(false); } } return (
Please sign in to your account
Don't have an account?{" "} Sign up