"use client" import { fetchWithAuth } from "@/lib/apiClient"; import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { ArrowLeft, Eye, EyeOff } from "lucide-react" import Link from "next/link" import { useState } from "react" import { useRouter } from "next/navigation" import { Logo } from "@/components/Logo" export default function LoginPage() { const [showPassword, setShowPassword] = useState(false) const [email, setEmail] = useState("") const [password, setPassword] = useState("") const [error, setError] = useState("") const router = useRouter() const handleLogin = async (e: React.FormEvent) => { e.preventDefault() setError("") try { const res = await fetch("/api/auth/login", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email, password }) }) const data = await res.json() if (data.success) { if (typeof window !== "undefined") { // Set auth_token cookie required by Next.js 16 proxy.ts middleware const cookieVal = data.role === "admin" ? "admin" : (data.token || "authenticated"); document.cookie = `auth_token=${cookieVal}; path=/; max-age=2592000; SameSite=Lax`; localStorage.setItem("activeUserId", data.user_id || data.token); // Store the JWT as the active session token const sessionToken = data.token; localStorage.setItem("active_session_token", sessionToken); if (data.token && data.token !== "admin") { try { await fetchWithAuth(`/api/users/${data.user_id}/session`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ session_token: sessionToken }) }); } catch (e) { console.warn("Session update skipped", e); } } if (data.role === "admin") { window.location.href = "/admin"; } else { window.location.href = "/dashboard"; } } } else { setError(data.error || "Invalid email or password.") } } catch (err) { setError("An error occurred. Please try again.") } } return (
Don't have an account? Sign up