Home > database >  Problems with bcrypt hash encryption - node.js
Problems with bcrypt hash encryption - node.js

Time:10-07

I'm having trouble logging in, with hash, I made the call in the create method, and it's working normally, but in the login no, I saw errors similar to mine here on the stack, but I didn't understand how to implement it, could you give a strength? the error message it returns is "Error: data and hash arguments required" the line where the error is would be do: const validPass

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);
    }

CodePudding user response:

In this line:

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

Change user.hash to user.password.

Extra note: You don't need those awaits before the synchronous function calls.

  • Related