feat: Enhance login error reporting with backend pre-validation and update signup to require email verification instead of auto-login.

This commit is contained in:
pradeepkumar
2025-12-22 13:02:17 +05:30
parent 060643fed8
commit 6c6129e8d1
3 changed files with 96 additions and 62 deletions

View File

@@ -25,37 +25,33 @@ export const { handlers, signIn, signOut, auth } = NextAuth({
password: { label: "Password", type: "password" },
},
async authorize(credentials) {
try {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/auth/login`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email: credentials?.email,
password: credentials?.password,
}),
}
);
const data = await res.json();
if (res.ok && data.success) {
return {
id: data.data.user.id,
email: data.data.user.email,
name: `${data.data.user.firstName || ""} ${data.data.user.lastName || ""}`.trim(),
role: data.data.user.role,
accessToken: data.data.accessToken,
refreshToken: data.data.refreshToken,
};
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/auth/login`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email: credentials?.email,
password: credentials?.password,
}),
}
);
return null;
} catch (error) {
console.error("Auth error:", error);
return null;
const data = await res.json();
if (res.ok && data.success) {
return {
id: data.data.user.id,
email: data.data.user.email,
name: `${data.data.user.firstName || ""} ${data.data.user.lastName || ""}`.trim(),
role: data.data.user.role,
accessToken: data.data.accessToken,
refreshToken: data.data.refreshToken,
};
}
// Throw the actual error message from the backend
throw new Error(data.message || "Invalid email or password");
},
}),
],