0

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")

    });
3
  • req.body.password seems to beundefined. Can you verify with your debugger? Commented Aug 13, 2023 at 1:51
  • Have you printed password and user.password so you know what you got? Commented Aug 13, 2023 at 1:51
  • 2
    You are treating asynchronous calls like they are synchronous. The look up by email to get the user is not going to be returned before the bcrypt code runs. You either need to use await or you need to put the bcrypt code inside of the user look up then. Commented Aug 13, 2023 at 2:20

1 Answer 1

0

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")

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.