bcrypt.compare receiving illegal argument string, undefined

22 views Asked by At

I am creating a web app, and using a mysql database, when i ty to use the bcryptjs library to compare a hash password with the data gathered from a login form, i get that the bcrypt.compare function is receiving string,undefined

this is in my controller, this handles the post login, some parts are in spanish, contrasena = password,

exports.post_login = (request, response, next) => {
    const {email, password} = request.body;
    if(!email || !password){
        return response.render("login", {error: "Llena todos los campos"})
    }

    Usuarios.findByEmail(email)
        .then(user => {
            if (user) {
                // Use bcrypt.compare to check if passwords match
                console.log(user);
                bcrypt.compare(password, user.contrasena)
                    .then(doMatch => {
                        if (doMatch) {
                            request.session.isLoggedIn = true;
                            request.session.user = user;
                            return request.session.save(err => {
                                response.redirect('/');
                            });
                        } else {
                            response.redirect('/users/login');
                        }
                    })
                    .catch(err => {
                        console.error('Error during login', err);
                        response.redirect('/users/login');
                    });
            } else {
                response.redirect('/users/login');
            }
        })
        .catch(err => {
            console.error('Error during login', err);
            response.redirect('/users/login');
        });

};

here is my code from the model.js file, which saves the user info that is received from a signup form

    save() {
        const userData = {
            idUsuario: this.idUsuario,
            IdRol: this.idRol,
            Nombre: this.nombre,
            Contrasena: this.contrasena,
            Correo: this.email,
        }

        return bcrypt.hash(userData.Contrasena, 12)
            .then((hashedPassword) => {
                userData.Contrasena = hashedPassword;
                const values = Object.values(userData);
                return db.execute('INSERT INTO usuario (idUsuario,IdRol,Nombre,Contrasena,Correo) VALUES (?,?,?,?,?)',values);
            })
            .then(([result]) => {
                console.log('Usuario Guardado:', result);
                return result; // Return the ResultSetHeader
            })
            .catch(err => {
                console.log('Error guardando usuario:', err);
                throw err;
            });
    } 


    static findByEmail(email) {
        return db.execute('SELECT * FROM usuario WHERE Correo = ?', [email])
            .then(([rows]) => {
                if (rows.length > 0) {
                    const userData = rows[0];
                    console.log('Esto es lo que esta recuperando de la base de datos: ', userData)
                    const user = new Usuarios(userData.Nombre, userData.Correo, userData.Contrasena, userData.idUsuario, userData.IDRol);
                    console.log('Esto es lo que se va a retornar: ',user);
                    return { user: user, passwordMatch: true }; // Return user data with passwordMatch true
                }
                return { user: null, passwordMatch: false }; // Return null user and passwordMatch false if user not found
            })
            .catch(err => {
                console.error('Error fetching user by email from database:', err);
                throw err;
            });
    }

i have tried printing the users that are retrieved from the database, the info given from the login form, and they all appear to be in order, the database is in fact saving the user info correctly

0

There are 0 answers