Next.js Discord

Discord Forum

Need Help with NextAuth Authentication Issues in Next.js - Stuck on Redirects!

Unanswered
Cuban Crocodile posted this in #help-forum
Open in Discord
Cuban CrocodileOP
I'm working on a Next.js application using Auth.js v5 for authentication. When I attempt to authenticate with GitHub, the process works smoothly — it successfully authenticates and redirects me to the homepage ("/home"). However, when I try logging in using email and password, I encounter an issue. After submitting the credentials, I get an error message that says "Something went wrong," and the authentication doesn't seem to complete properly. If I perform a hard refresh after that, it redirects me to the homepage ("/home") despite the error.

I'm not sure why email/password authentication is failing while GitHub works, and I'd appreciate any help in troubleshooting this.

9 Replies

Cuban CrocodileOP
try {
await signIn('credentials', {
email,
password,
redirectTo: callbackUrl || DEFAULT_LOGIN_REDIRECT,
})
return { ok: true };


} catch (error) {
if (isRedirectError(error)) {
throw error;
}

if(error instanceof AuthError) {
switch(error.type) {
case "CredentialsSignin":
return {
error: "Invalid credentials!"

}

default:
return {
error: "Something went wrong!"
}
}

}
throw error;

}
providers: [GitHub,
Credentials({
authorize: async (credentials) => {
try {
const validatedFields = LoginSchema.safeParse(credentials);

if (validatedFields.success) {
const { email, password } = validatedFields.data;

const user = await getUserByEmail({ email });

if (!user || !user.password) return null;

const passwordMatch = await bcrypt.compare(password, user.password);

if (passwordMatch) return user;

return null;
}

return null;

} catch (error) {
if (error instanceof z.ZodError) {
console.error('Validation error:', error.errors);
return null;
}

// Catch and log any other unexpected errors
console.error('Authorization error:', error);
return null;
}
}

})
],
Brown bear
Could you please surround the code with three backticks? It would be easier to read that way.👇like that
console.log("Hello");
```typescript console.log("Hello"); ```
Cuban CrocodileOP
ok
i found that doing all your authentication in the client is better than in the server
all of you getting errors, see this proj, https://github.com/yaninyzwitty/next-auth-complete-14