Next.js Discord

Discord Forum

Error with CredentialsProvider | nextAuth

Unanswered
Naeemgg posted this in #help-forum
Open in Discord
Can anyone please take a look at my authOptions and tell me what is wrong I'm doing here, I was getting type errors so I made it .js but still when both username and password are valid only then everything is going fine otherwise it's showing server error instead of wrong username or password.

This is my authOptions:
import NextAuth, { AuthOptions } from "next-auth"; 
 import CredentialsProvider from "next-auth/providers/credentials"; 
 import prisma from "@/prisma/client"; 
  
 export const authOptions = { 
   providers: [ 
     CredentialsProvider({ 
       name: "credentials", 
       credentials: { 
         name: { 
           label: "name", 
           type: "name", 
           placeholder: "Faizan", 
         }, 
         password: { 
           label: "password", 
           type: "password", 
           placeholder: "******", 
         }, 
       }, 
       async authorize(credentials, req) { 
         const { name, password } = credentials; 
  
         const user = await prisma.user.findUnique({ where: { name,password } }); 
  
         if (!user) { 
           throw new Error("Invalid email or password"); 
         } 
         return user; 
       }, 
     }), 
   ], 
   secret: process.env.NEXTAUTH_SECRET, 
 } 
 const handler = NextAuth(authOptions); 
  
 export { handler as GET, handler as POST };

8 Replies

Satin Angora
1. Dont store passwords in plain text.
2. there is no type "name" on an html input. Change "name" to "text"
3. Select the user by name, then check if the record exists, then compare the passwords.

Something like that:
         const user = await prisma.user.findUnique({ where: { name: name } });
  
         if (!user) { 
           throw new Error("User not found"); 
         } 
// You may want to check out bcrypt package for hashing and salting, instead of comparing plain text passwords
         if (user.password !== password) {
              throw new Error("Password is incorrect");
         }
@Satin Angora 1. Dont store passwords in plain text. 2. there is no type "name" on an html input. Change "name" to "text" 3. Select the user by name, then check if the record exists, then compare the passwords. Something like that: const user = await prisma.user.findUnique({ where: { name: name } }); if (!user) { throw new Error("User not found"); } // You may want to check out bcrypt package for hashing and salting, instead of comparing plain text passwords if (user.password !== password) { throw new Error("Password is incorrect"); }
1: stored hashed password
2: changed it
3: done
Now this is how my authOptions looks like:

import NextAuth, { AuthOptions } from "next-auth"; 
 import CredentialsProvider from "next-auth/providers/credentials"; 
 import prisma from "@/prisma/client"; 
 import bcrypt from "bcryptjs" 
 export const authOptions = { 
   providers: [ 
     CredentialsProvider({ 
       name: "credentials", 
       credentials: { 
         name: { 
           label: "name", 
           type: "text", 
           placeholder: "Faizan", 
         }, 
         password: { 
           label: "password", 
           type: "password", 
           placeholder: "******", 
         }, 
       }, 
       async authorize(credentials, req) { 
         const { name, password } = credentials; 
         const user = await prisma.user.findUnique({ where: { name:name } }); 
  
         if (!user) { 
           throw new Error("Invalid email or password"); 
         } 
         const isPasswordMatched = await bcrypt.compare(password, user.password); 
         if (!isPasswordMatched) { 
           alert("Invalid password or email") 
           throw new Error("Invalid email or password"); 
         } 
         return user; 
       }, 
     }), 
   ], 
   secret: process.env.NEXTAUTH_SECRET, 
 } 
 const handler = NextAuth(authOptions); 
  
 export { handler as GET, handler as POST };


I did everything as described but still the issue persists :thinq:
Satin Angora
Can you post the error your receiving?
yes sure, give me a moment...
@Satin Angora Can you post the error your receiving?
If both username and password are ok then I'm redirected to the desired page other wise this happens
@Satin Angora Can you post the error your receiving?
this is the url http://localhost:3000/api/auth/error?error=Invalid email or password
I dont know why error is showing like this and not on the UI
@Satin Angora
??