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:
52
src/auth.ts
52
src/auth.ts
@@ -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");
|
||||
},
|
||||
}),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user