dimanche 3 janvier 2021

Async call within Conditional in Promise Chain Not Finishing before Progressing

I have a conditional within my promise chain that runs an async function and prior to the async function completing it assumes that the condition was met and jumps to returning the else statement. It might be my lack of understanding the differences between async and promises, but I find it strange that the conditional evaluates that else if(!user.validPassword(password)) is not met prior to the method completing. The value getting returned is true or false depending on if the password matches, but even if false is returned the body of that else if is not run.

Can anyone help me understand what is off in my logic?

Here is the promise chain with the if conditional:

passport.use(new passportLocal({ 
        passReqToCallback: true,
        usernameField: 'email'
    },
    function(req, email, password, done) {
        console.log('passportLocal Callled')
        console.log(email)
        console.log(password)
        User.findOne({ 
            where: {
                email: email
            }
        }).then(function(user){
            console.log('User.findOne - Finished Query')
            if(!user){
                console.log("User not found")
                // User not found
                return done(null, false, { status: "error", body: 'User does not exist in the database.'});
            } else if(!user.validPassword(password)) {
                console.log("Password is incorrect")
                // Wrong password
                done(null, false, { status: "error", body: "Incorrect Password" });
            } else {
                console.log("User found & password correct")
                // User found and password correct
                return done(null, user, { status: "success", body: "You have successfully signed in!" })
            }
        }).catch(function(err){
            console.log("Server error with signin")
            console.log(err)
            // Server error
            return done(null, false, { status: "error", body: "It looks like there was a server issue. Please try again." });
        })        
    })
);

And the if statement by itself for clarity:

if(!user){
                console.log("User not found")
                // User not found
                return done(null, false, { status: "error", body: 'User does not exist in the database.'});
            } else if(!user.validPassword(password)) {
                console.log("Password is incorrect")
                // Wrong password
                done(null, false, { status: "error", body: "Incorrect Password" });
            } else {
                console.log("User found & password correct")
                // User found and password correct
                return done(null, user, { status: "success", body: "You have successfully signed in!" })
            }

And finally user.validPassword(password):

users.prototype.validPassword = async function(password) {
    return bcrypt.compareSync(password, this.password); // returns `true` or `false`
  }

Aucun commentaire:

Enregistrer un commentaire