0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I did as instructed, got out of that error, and now it is entering the if, but it is not validating the password yet, I created a new user already, and I put the correct password, but it enters the else "Wrong pass!", I would know why ?
Then I would suggest you to console.log password and user.password to see if something is wrong with their values. There is very likely that one of them is not having the value that you expect to have.
I found out what it was, my bank was limited to 6 digits so there wasn't an exact comparison, it wasn't going all the characters of the hash, I apologize, it was my trip haha, but your help was essential, thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.