MongoDB的下一个authjs发行

发布于 2025-01-21 03:27:55 字数 2190 浏览 0 评论 0原文

以下是我的[... NextAuth] .js文件,由于某种原因,当我尝试使用它来登录时,请访问http:// localhost:3000/api/auth/aign/sign,它显示了用户名和密码框,但是当我提交了我的错误。

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
      }
});

Below is my [...nextauth].js file and for some reason when I try and use it to login by going to http://localhost:3000/api/auth/signin it presents the username and password box but then when I submit it I get an error.

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

But it is not telling me what the illegal argument is, is there any way to find out?

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
      }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋日私语 2025-01-28 03:27:55

就我而言,错误在于比较密码。您需要检查该字符串。

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

我的凭证.密码不等于结果.密码,因为我写的

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

是而不是

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

希望,它会有所帮助。祝你好运。

In my case, the error was in comparing passwords. You need to check this string.

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

My credentials.password wasn't equal to result.password, becase I wrote

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

instead of

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

Hope, it will help. Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文