NodeJS: Bcrypt: Error: Illegal arguments: undefined, string

104 views Asked by At

This is part of my code from the controller I am using to do validations for the login app, so everything is being inserted correctly into the database and its encrypted right, but the moment I try to submit user data it fails to validate the password, says the errors it's at the bcrypt. compare, I put the right one and even if I put the wrong it should redirect me to Home, instead the program crashes.

Error: Illegal arguments: undefined, string
    at _async (C:\Users\CHINO\Desktop\Sistema-Pre-Escolar-Manitas-De-Oro-main (1)\Sistema-Pre-Escolar-Manitas-De-Oro-main\node_modules\bcryptjs\dist\bcrypt.js:286:46)
    at C:\Users\CHINO\Desktop\Sistema-Pre-Escolar-Manitas-De-Oro-main (1)\Sistema-Pre-Escolar-Manitas-De-Oro-main\node_modules\bcryptjs\dist\bcrypt.js:307:17
    at new Promise (<anonymous>)
    at Object.bcrypt.compare (C:\Users\CHINO\Desktop\Sistema-Pre-Escolar-Manitas-De-Oro-main (1)\Sistema-Pre-Escolar-Manitas-De-Oro-main\node_modules\bcryptjs\dist\bcrypt.js:306:20)
    at C:\Users\CHINO\Desktop\Sistema-Pre-Escolar-Manitas-De-Oro-main (1)\Sistema-Pre-Escolar-Manitas-De-Oro-main\controllers\authController.js:31:6
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

AuthController.js :



exports.PostLogin = (req, res, next) => {

    const password = req.body.password
    const email = req.body.email

    User.findOne({ where: {email : email}})
    .then((user) => {
        if (!user) {
           flash.req("errors","Ha ocurrido un error al momento de ingresar el Correo electronico")
    return res.redirect("/login")
    }

    bcrypt
    .compare(password, user.password)
    .then((result) =>{
        if (result){
            req.session.IsloggedIn=true
            req.session.user = user
            return req.session.save((err) =>
            {
                flash.req("errors","ContreseƱa invalida")

                console.log("Error al momento de compare",err)
                res.redirect("/")
            });
        }
        res.redirect("/login")

    });
1

There are 1 answers

0
M.G.S SUPUNTHAKA On

as @epascarello mentioned, You are goin through the users collection and trying get a user based on your entered email which should be wraped with Async - await call. This way it awaits for the user is found then goes to compearing the password with bcrypt

exports.PostLogin = async (req, res, next) => {

    const password = req.body.password
    const email = req.body.email

    await User.findOne({ where: {email : email}})
    .then((user) => {
    bcrypt
    .compare(password, user.password)
    .then((result) =>{
        if (result){
            req.session.IsloggedIn=true
            req.session.user = user
            return req.session.save((err) =>
            {
                flash.req("errors","ContreseƱa invalida")

                console.log("Error al momento de compare",err)
                res.redirect("/")
            });
        }
        res.redirect("/login")

    });