在 Nextjs 应用程序路由器项目中使用带有 argon2 的 NextAuth 凭证提供程序时出错

问题描述 投票:0回答:1

: 我正在使用 argon2 包来哈希和验证我的密码,如果我在 auth.config.ts 的授权函数中使用 argon2.verify,我会收到以下错误:

UnhandledSchemeError: Reading from "node:crypto" is not handled by plugins (Unhandled scheme). Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.

这是我的 auth.config.ts 文件:

import type { NextAuthConfig } from 'next-auth'
import Credentials from 'next-auth/providers/credentials'

import { LoginSchema } from './lib/zod'
import { getUserByEmail } from './utils/db'
import { verifyPassword } from './utils/hash'

export default {
  providers: [
    Credentials({
      async authorize(credentials: Partial<Record<string, unknown>>): Promise<object | null> {
        // Validate the provided credentials against the LoginSchema
        const validatedFields = LoginSchema.safeParse(credentials)

        if (!validatedFields.success) {
          return null
        }

        const { email, password } = validatedFields.data
        const user = await getUserByEmail(email)

        if (!user || !user.password) {
          return null
        }
        const passwordsMatch = await verifyPassword(password, user.password)

        if (passwordsMatch) {
          return user
        }

        // if passwords do not match
        return null
      },
    }),
  ],
} satisfies NextAuthConfig

这是我的 verifyPassword 函数:

export const verifyPassword = async (password: string, hash: string): Promise<boolean> => {
  try {
    return await argon2.verify(hash, password)
  } catch {
    throw new Error('Verification failed')
  }
}

我已经验证了authorize函数之外的verifiyPassword函数,它工作得很好。但是它在授权内部不起作用。像 bcrypt 这样的东西可以工作,但我想使用 argon2。我也尝试过@node-rs/argon2,但这根本不起作用。

next.js middleware next-auth
1个回答
0
投票

使用特定导入代替通配符导入

import { verify } from "argon2";
© www.soinside.com 2019 - 2024. All rights reserved.