简体   繁体   中英

Checking whether user is logged in passport.js

I use passport.js to authenticate user. I have 2 function there to check whether user is logged in or not. First function:

isLoggedIn(req, res, next) {
        if (req.isAuthenticated()) {
            return next();
        }
        else {
            res.redirect('/');
        }
    }

2nd function:

isLoggedInCheck(req, res) {
        if (req.isAuthenticated()) {
            return true;
        }
        else {
            return false;
        }
    }

I take these 2 functions in class called Helper. When I use the 1st function (I pass it in routes function as middleware) it works:

var Helper = require('../helpers/helper');
var helper = new Helper();
router.get('/', helper.isLoggedIn, admin.melihat_daftar_venue);

But when i want to use second function:

if (helper.isLoggedInCheck) {
//code
}
else{

}

The function just return function definition instead of true/false. How to fix it. Thanks

You are using isLoggedIn as a ExpressJS middleware while isLoggedInCheck inside condition that's why you need to call function( helper.isLoggedInCheck(req, res) inside if condition while define inside get function

if (helper.isLoggedInCheck(req, res)) {
//code
}
else{

}

and first one is

router.get('/', helper.isLoggedIn, admin.melihat_daftar_venue);

or (not recommended, just showing example)

router.get('/', (req, res, next) => {
  helper.isLoggedIn(req, res, next)
}, admin.melihat_daftar_venue);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM