【问题标题】:Passportjs multiple authentication strategies external file with expressjsPassportjs 多种身份验证策略外部文件与 expressjs
【发布时间】:2020-10-30 05:54:06
【问题描述】:

首先,如果我问了一个明显愚蠢的问题,我提前道歉。

我目前有一个护照身份验证策略设置,它工作正常。实现如下。

认证策略(authentication.js):

const passport = require("passport");
const passportJWT = require("passport-jwt");
const params = {
    //Params here
};


module.exports = function LocalStrategy() {
let strategy = new Strategy(params, function (payload, done) {
    //Logic here
});
passport.use(strategy);
return {
    initialize: function () {
        return passport.initialize();
    },
    authenticate: function () {
        return passport.authenticate("jwt", {
            session: false
        });
    }
  };
};

在路线中使用:

const localAuth = require('./authentication/LocalStrategy')();

app.get('/test', localAuth.authenticate(), (req, res) => {
    res.json(req.isAuthenticated());
});

在 server.js 文件中

const localAuth = require('./authentication/LocalStrategy')();
app.use(localAuth.initialize());

我计划在单个路由中使用多种身份验证策略,我发现了这个implementation。但是,我不想将身份验证策略写在同一个 server.js 中,而是希望将策略写在外部文件中(在我的情况下为 authentication.js),并将路由中的策略引用为

passport.authenticate(['SOME_OTHER_STRATEGY', 'jwt']

我该如何实现?

【问题讨论】:

    标签: node.js authentication express passport.js


    【解决方案1】:

    好吧,显然我还不够努力, 除了serializeUser 和deserializeUser 之外,我不需要对我当前的逻辑进行任何更改。只需使用:

    passport.authenticate(['SOME_OTHER_STRATEGY', 'jwt']) 
    

    不是这样。

    【讨论】:

      猜你喜欢
      • 2014-04-06
      • 1970-01-01
      • 2014-02-22
      • 2014-09-27
      • 2020-12-21
      • 2020-10-21
      • 2020-07-03
      • 2019-04-23
      • 2019-06-12
      相关资源
      最近更新 更多