下一个-Mongodb的authjs问题

f4t66c6m  于 2023-04-05  发布在  Go
关注(0)|答案(1)|浏览(145)

下面是我的[...nextauth].js文件,出于某种原因,当我尝试使用它登录http://localhost:3000/api/auth/signin时,它会显示用户名和密码框,但当我提交它时,我会得到一个错误。

http://localhost:3000/api/auth/error?error=Illegal%20arguments%3A%20undefined%2C%20undefined

但它并没有告诉我非法的论点是什么,有没有办法弄清楚?

import NextAuth from "next-auth"
import CredentialsProvider from "next-auth/providers/credentials"
import clientPromise from "../../../lib/mongodb";
import jwt from "next-auth/jwt";
import { compare } from 'bcryptjs';

export default NextAuth({
    
  session: {
      jwt: true,
  },
    providers: [
        CredentialsProvider({
          // The name to display on the sign in form (e.g. 'Sign in with...')
          name: 'DRN1',
          credentials: {
            username: { label: "Username", type: "text"},
            password: {  label: "Password", type: "password" }
          },
          async authorize(credentials, req) {
          

            const client = await clientPromise
            const { fieldvalue } = req.query

            console.log("RUNNING THIS QUERY "+req.query)

            const database = client.db('DRN1');
            const users = await database.collection('users');
            const result = await users.findOne({
              username: credentials.username,
            });

            if (!result) {
              client.close();
              throw new Error('No user found with the username');
            }

            //Check hased password with DB password
            const checkPassword = await compare(credentials.passowrd, result.passowrd);
            //Incorrect password - send response
            if (!checkPassword) {
                client.close();
                throw new Error('Password doesnt match');
            }
            //Else send success response
            client.close();
            return { username: result.username };

          }
        })
      ],
      theme: {
        colorScheme: "dark", // "auto" | "dark" | "light"
        brandColor: "", // Hex color code
        logo: "https://storage.googleapis.com/radiomedia-images/station_logos/v2/DRN1_small.png" // Absolute URL to image
      }
});
yxyvkwin

yxyvkwin1#

在我的例子中,错误是在比较密码时。你需要检查这个字符串。

const checkPassword = await compare(credentials.passowrd, result.passowrd);

我的credentials.password不等于result.password,因为我写了

const result = await users.findOne({
          username: credentials.username,
        });

而不是

const { result } = await users.findOne({
          username: credentials.username,
        });

希望会有帮助祝你好运。

相关问题