fix(auth): handle unexpected empty response during email login (#334)

This commit is contained in:
Chirag Sharma 2025-07-07 00:11:24 +05:30
parent 651f984e52
commit ae4fff9fb4
3 changed files with 30 additions and 8 deletions

View file

@ -1,3 +1,5 @@
// Ensure this route is accessible at /api/auth/[...nextauth] and both GET and POST are exported for NextAuth to work properly.
// If you see 405 errors, check your deployment and NEXTAUTH_URL configuration.
import NextAuth from "next-auth"
import { authOptions } from "@/lib/auth"

View file

@ -34,20 +34,37 @@ export function UserAuthForm({ className, ...props }: UserAuthFormProps) {
async function onSubmit(data: FormData) {
setIsLoading(true)
const signInResult = await signIn("email", {
email: data.email.toLowerCase(),
redirect: false,
callbackUrl: searchParams?.get("from") || "/dashboard",
})
let signInResult: any = null;
try {
signInResult = await signIn("email", {
email: data.email.toLowerCase(),
redirect: false,
callbackUrl: searchParams?.get("from") || "/dashboard",
});
} catch (err) {
setIsLoading(false);
return toast({
title: "Something went wrong.",
description: "Could not reach the authentication server. Please try again later.",
variant: "destructive",
});
}
setIsLoading(false)
if (!signInResult?.ok) {
// Handle empty or invalid response
if (!signInResult || signInResult.error || !signInResult.ok) {
let description = "Your sign in request failed. Please try again.";
if (signInResult && signInResult.error === "Callback") {
description = "Authentication server returned an error. Please check your configuration or try again later.";
} else if (signInResult && signInResult.status === 405) {
description = "Authentication method not allowed. Please contact support or try again later.";
}
return toast({
title: "Something went wrong.",
description: "Your sign in request failed. Please try again.",
description,
variant: "destructive",
})
});
}
return toast({

View file

@ -1,3 +1,6 @@
// IMPORTANT: Set NEXTAUTH_URL to your local or production domain to avoid 405 errors and authentication issues.
// For local dev: NEXTAUTH_URL=http://localhost:3000
// For prod: NEXTAUTH_URL=https://yourdomain.com
import { createEnv } from "@t3-oss/env-nextjs"
import { z } from "zod"