我正在使用 Argon2 對我的密碼進行哈希處理,這是我的代碼:
import { ForbiddenException, Injectable } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';
import { AuthDto } from './dto';
import * as argon from 'argon2';
async signup(authDto: AuthDto) {
// generate the password
const hash = await argon.hash(authDto.password);
console.log(`The hashed password is ${authDto.password}`);
// save the new user in the db
try {
const user = await this.prisma.user.create({
data: {
email: authDto.email,
hash: authDto.password,
firstname: '',
lastname: '',
},
});
//delete user.hash;
// return the saved user
return user;
} catch (error) {
// test if the error is commimg from prisma
if (error instanceof PrismaClientKnownRequestError) {
// test if the field is duplicated
if (error.code === 'P2002') {
throw new ForbiddenException('Credentials taken'); //NestJS exception
}
}
throw error;
}
}
當我列印我的散列密碼時,我發現它沒有散列。
PS:我使用 NestJS 作為 nodeJS 后端框架,Manjaro Linux 作為作業系統,Argon2 作為哈希庫。
uj5u.com熱心網友回復:
在對密碼進行哈希處理后,您仍然使用明文密碼進行記錄并將其存盤到 prisma db 中。該變數hash包含散列密碼。
更改代碼以使用hash代替authDto.password。
const hash = await argon.hash(authDto.password);
console.log(`The hashed password is ${hash}`);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/444094.html
