Next.js Discord

Discord Forum

NextAuth Credentials not giving error

Unanswered
Mini Lop posted this in #help-forum
Open in Discord
Mini LopOP
Hello, i have read in docs that while using credentials provider for nextauth returning null in authorize function will give you error, but for me it doesn't. when i return object i get the same exact thing as i used to get with error which is response: ok status code 200 with error being null. any help?

3 Replies

Mini LopOP
import { connectToMongoDB } from "@/lib/mongo/connect";
import Users from "@/lib/mongo/models/users";
import NextAuth from "next-auth/next";
import CredentialsProvider from "next-auth/providers/credentials";
import bcrypt from "bcryptjs";

const authOptions: any = {
  providers: [
    CredentialsProvider({
      name: "credentials",
      credentials: {},

      async authorize(credentials) {
        const { email, password }: any = credentials;
        try {
          await connectToMongoDB();
          const user = await Users.findOne({ email: email });
          return null;
          const passwordsMatch = await bcrypt.compare(password, user.password);
          console.log(passwordsMatch);
          if (!passwordsMatch) {
            return null;
          } else {
            console.log("hello");
            return user;
          }
        } catch (err) {
          console.log("Error", err);
        }
      },
    }),
  ],
  session: {
    strategy: "jwt",
  },
  secret: process.env.NEXTAUTH_SECRET,
  pages: {
    signIn: "/login",
  },
};

const handler = NextAuth(authOptions);

export { handler as GET, handler as POST };
return null is what i just wrote for debugs
but you get the point, even when i return object i get same exact response