【问题标题】:Use Passport Local & JWT Strategy on same app (on same route)在同一应用程序上使用 Passport Local 和 JWT 策略(在同一条路线上)
【发布时间】:2017-05-11 13:06:51
【问题描述】:

所以我的路线(用于'/dash')如下所示:

// validating using JWT
router.post('/dash', passport.authenticate('jwt', {session: false}), function (req, res) {
    res.json({'success': true});
});

// validating using LOCAL
router.post('/dash', authenticationHelpers.isAuth, function (req, res) {
    res.json({'success': true});
});

// authenticationHelpers.isAuth
function isAuth(req, res, next) {
    if (req.isAuthenticated())
        return next();
    res.status(401).json({"authenticated": false});
}

那么,我如何在同一个应用程序上(在同一条路线上)同时使用 Local 和 JWT 策略?我如何将它们结合起来。

注意:本地为网页应用,JWT 为移动应用

【问题讨论】:

    标签: node.js authentication express passport.js jwt


    【解决方案1】:

    终于明白了。

    修改isAuth函数:

    function isAuth(req, res, next) {
        if (req.headers.authorization) {
            passport.authenticate('jwt', {session: false}, function (err, user, info) {
                if ((!err || !info) && user) {
                    req.user = user;
                    return next();
                }
                res.status(401).json({authenticated: false, message: "Login expired."});
            })(req, res, next);
        } else {
            if (req.isAuthenticated())
                return next();
            res.status(401).json({authenticated: false});
        }
    }
    

    欢迎提出建议...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-23
      • 2017-03-26
      相关资源
      最近更新 更多