From 1ea22df2f7ccd837a059bd8d31529fa981835d0e Mon Sep 17 00:00:00 2001 From: Hardik Date: Fri, 22 May 2026 17:14:21 +0530 Subject: [PATCH] fix(auth): stabilize login page rendering --- App/app/(auth)/login/login-form.tsx | 91 ++++++++++++++++++++++++++++ App/app/(auth)/login/page.tsx | 94 +++-------------------------- App/auth.ts | 1 + 3 files changed, 100 insertions(+), 86 deletions(-) create mode 100644 App/app/(auth)/login/login-form.tsx diff --git a/App/app/(auth)/login/login-form.tsx b/App/app/(auth)/login/login-form.tsx new file mode 100644 index 0000000..cfea2b5 --- /dev/null +++ b/App/app/(auth)/login/login-form.tsx @@ -0,0 +1,91 @@ +"use client"; + +import { useState } from "react"; +import { signIn } from "next-auth/react"; +import { useRouter, useSearchParams } from "next/navigation"; + +export function LoginForm() { + const [email, setEmail] = useState(""); + const [password, setPassword] = useState(""); + const [error, setError] = useState(""); + const [loading, setLoading] = useState(false); + const router = useRouter(); + const searchParams = useSearchParams(); + const callbackUrl = searchParams.get("callbackUrl") ?? "/dashboard"; + + async function handleSubmit(e: React.FormEvent) { + e.preventDefault(); + setLoading(true); + setError(""); + + const result = await signIn("credentials", { + email, + password, + redirect: false, + }); + + if (result?.error) { + setError("Invalid email or password. Please try again."); + setLoading(false); + } else { + router.push(callbackUrl); + router.refresh(); + } + } + + return ( +
+
+ + setEmail(e.target.value)} + className="w-full rounded-lg border border-neutral-300 px-3 py-2.5 text-sm text-neutral-900 placeholder:text-neutral-400 focus:border-primary-500 focus:outline-none focus:ring-2 focus:ring-primary-500/20" + placeholder="you@company.com" + /> +
+ +
+ + setPassword(e.target.value)} + className="w-full rounded-lg border border-neutral-300 px-3 py-2.5 text-sm text-neutral-900 placeholder:text-neutral-400 focus:border-primary-500 focus:outline-none focus:ring-2 focus:ring-primary-500/20" + placeholder="••••••••" + /> +
+ + {error && ( +

+ {error} +

+ )} + + +
+ ); +} diff --git a/App/app/(auth)/login/page.tsx b/App/app/(auth)/login/page.tsx index 125ece0..657422c 100644 --- a/App/app/(auth)/login/page.tsx +++ b/App/app/(auth)/login/page.tsx @@ -1,39 +1,11 @@ -"use client"; - -import { useState } from "react"; -import { signIn } from "next-auth/react"; -import { useRouter, useSearchParams } from "next/navigation"; +import { Suspense } from "react"; import { Anchor } from "lucide-react"; +import { LoginForm } from "./login-form"; +import type { Metadata } from "next"; + +export const metadata: Metadata = { title: "Sign in" }; export default function LoginPage() { - const [email, setEmail] = useState(""); - const [password, setPassword] = useState(""); - const [error, setError] = useState(""); - const [loading, setLoading] = useState(false); - const router = useRouter(); - const searchParams = useSearchParams(); - const callbackUrl = searchParams.get("callbackUrl") ?? "/dashboard"; - - async function handleSubmit(e: React.FormEvent) { - e.preventDefault(); - setLoading(true); - setError(""); - - const result = await signIn("credentials", { - email, - password, - redirect: false, - }); - - if (result?.error) { - setError("Invalid email or password. Please try again."); - setLoading(false); - } else { - router.push(callbackUrl); - router.refresh(); - } - } - return (
@@ -51,59 +23,9 @@ export default function LoginPage() {

Sign in

-
-
- - setEmail(e.target.value)} - className="w-full rounded-lg border border-neutral-300 px-3 py-2.5 text-sm text-neutral-900 placeholder:text-neutral-400 focus:border-primary-500 focus:outline-none focus:ring-2 focus:ring-primary-500/20" - placeholder="you@company.com" - /> -
- -
- - setPassword(e.target.value)} - className="w-full rounded-lg border border-neutral-300 px-3 py-2.5 text-sm text-neutral-900 placeholder:text-neutral-400 focus:border-primary-500 focus:outline-none focus:ring-2 focus:ring-primary-500/20" - placeholder="••••••••" - /> -
- - {error && ( -

- {error} -

- )} - - -
+ + +

diff --git a/App/auth.ts b/App/auth.ts index 7170e34..5c77f56 100644 --- a/App/auth.ts +++ b/App/auth.ts @@ -6,6 +6,7 @@ import { loginSchema } from "@/lib/validations/user"; import type { Role } from "@prisma/client"; export const { handlers, auth, signIn, signOut } = NextAuth({ + trustHost: true, session: { strategy: "jwt" }, pages: { signIn: "/login",