简体   繁体   English

检查用户是否登录passport.js

[英]Checking whether user is logged in passport.js

I use passport.js to authenticate user. 我使用passport.js对用户进行身份验证。 I have 2 function there to check whether user is logged in or not. 我那里有2个功能,用于检查用户是否已登录。 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. 我在名为Helper的类中使用了这两个函数。 When I use the 1st function (I pass it in routes function as middleware) it works: 当我使用第一个函数时(我将它作为中间件传递给routes函数):

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. 该函数仅返回函数定义,而不是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 您正在使用isLoggedIn作为ExpressJS中间件而isLoggedInCheck内部条件,这就是为什么你需要调用函数( helper.isLoggedInCheck(req, res)内,如果条件而确定的内部get的功能

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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