: 我正在使用 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,但这根本不起作用。
使用特定导入代替通配符导入
import { verify } from "argon2";