【问题标题】:Problems with bcrypt hash encryption - node.jsbcrypt 哈希加密的问题 - node.js
【发布时间】: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


    【解决方案1】:

    在这一行:

    const validPass = await bcrypt.compareSync(password, user.hash);
    

    user.hash 更改为user.password

    额外说明:在同步函数调用之前,您不需要那些等待。

    【讨论】:

    • 我按照指示做了,摆脱了那个错误,现在它正在输入if,但它还没有验证密码,我已经创建了一个新用户,我输入了正确的密码,但它进入 else "Wrong pass!",我会知道为什么?
    • 那我建议你去 console.log 密码和 user.password 看看他们的值是否有问题。其中一个很可能没有您期望的价值。
    • 我发现了它是什么,我的银行被限制为 6 位数字,所以没有精确的比较,它没有去哈希的所有字符,我很抱歉,这是我的旅行哈哈,但是你的帮助很重要,谢谢
    猜你喜欢
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 2013-08-07
    • 1970-01-01
    相关资源
    最近更新 更多