【发布时间】:2021-11-25 03:30:09
【问题描述】:
我在登录时遇到问题,使用哈希,我在 create 方法中进行了调用,并且它工作正常,但是在登录时没有,我在堆栈上看到类似于我的错误,但我没有明白如何实现它,你能给一个力量吗? 它返回的错误消息是“错误:需要数据和哈希参数”错误所在的行将是: const 有效通行证
const connection = require('../database/connection');
const bcrypt = require('bcrypt');
async create(request, response){
const {id, email, password, cpf, name, lastName, cellphone} = request.body;
let salt = await bcrypt.genSaltSync(10);
let hash = bcrypt.hashSync(password, salt);
const newUser = await connection('login').insert({
id,
email,
password:hash,
cpf,
name,
lastName,
cellphone,
});
return response.json({id: newUser[0], token: hash.toString('hex')});
},
async login(request, response){
const {email, password} = request.body;
const user = await connection('login').first('*').where({email:email});
if(user){
const validPass = await bcrypt.compareSync(password, user.hash);
if(validPass){
response.status(200).json('valid Email and pass!');
}else{
response.json('Wrong pass!')
}
}else{
response.status(400).json({error: 'No user foung with this E-mail'});
}
return response.json(user);
}
【问题讨论】:
标签: javascript node.js authentication hash bcrypt