【发布时间】: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