Search code examples
node.jsexpresspassport-facebook

Node.js/ mysql passport-facebook authenticate


I'm setting up a login page using passport-facebook authentication by accepting the email address from facebook. But when duplicate entry came it is redirecting to success page.Please help me

function(accessToken, refreshToken, profile, done) {
    process.nextTick(function() {
        if (fbconfig.use_database === true) {
            const qr = ("SELECT * from tbl_user where email ='" + profile.emails[0].value + "';");
            pool.query(qr, (err, rows) => {
                if (err) {
                    throw err;
                }
                if (rows && rows.length === 0) {
                    console.log(profile.emails[0].value);
                    console.log(profile.id);
                    console.log(profile.displayName);
                    var dt = dateTime.create();
                    var d = dt.format('Y-m-d H:M:S');
                    console.log("Creating profile");
                    let sql = ("INSERT into tbl_user(fb_id,token,name,email,cur_date) VALUES('" + profile.id + "','" + accessToken + "','" + profile.displayName + "','" + profile.emails[0].value + "','" + d + "');");
                    pool.query(sql, function(err, result) {
                        if (err) {
                            throw err;

                        }
                        console.log("fb inserted");


                    });



                } else {

                    console.log("Error Email already exist");

                }
            });

        }
        return done(null, profile);
    });
}));
router.get('/auth/facebook', passport.authenticate('facebook', { scope: 'email' }));
router.get('/auth/facebook/callback',
passport.authenticate('facebook', { successRedirect: '/sample', failureRedirect: '/', failureFlash: true }),
function(req, res) {
    pool.query(qr, (err, rows) => {
        if (err) {
            throw err;
        }
        if (rows && rows.length > 0) {
            res.render('/');
        }


    })


}
);

This is my code i cant find the solution


Solution

  •     function(accessToken, refreshToken, profile, done) {
    process.nextTick(function() {
        if (fbconfig.use_database === true) {
            const qr = ("SELECT * from tbl_user where email ='" + profile.emails[0].value + "';");
            pool.query(qr, (err, rows) => {
                if (err) {
                    throw err;
                }
                if (rows && rows.length === 0) {
                    console.log(profile.emails[0].value);
                    console.log(profile.id);
                    console.log(profile.displayName);
                    var dt = dateTime.create();
                    var d = dt.format('Y-m-d H:M:S');
                    console.log("Creating profile");
                    let sql = ("INSERT into tbl_user(fb_id,token,name,email,cur_date) VALUES('" + profile.id + "','" + accessToken + "','" + profile.displayName + "','" + profile.emails[0].value + "','" + d + "');");
                    pool.query(sql, function(err, result) {
                        if (err) {
                            throw err;
    
                        }
                        console.log("fb inserted");
    
    
                    });
    return done(null, true);
    
                } else {
    
                    console.log("Error Email already exist");
    
    
    return done(null, false);
    
                }
            });
    
        }
    
    });
    }));
    router.get('/auth/facebook', passport.authenticate('facebook', { scope: 'email' }));
    router.get('/auth/facebook/callback',
    passport.authenticate('facebook', { successRedirect: '/sample', failureRedirect: '/', failureFlash: true }),
    function(req, res) {
    res.redirect('/sample')   
    }
    
    );