admin管理员组文章数量:1355697
Suppose I have the code like this:
var api1 = require('api1');
var api2 = require('api2');
var app = express();
app.use('/api1', api1);
app.use('/api2', api2);
Here is the code for api1 module:
var router = express.Router();
var options = {
jwtFromRequest:ExtractJwt.fromAuthHeader(),
secretOrKey:config.JWTSecret,
algorithms:['HS256']
}
passport.use(new JwtStrategy(options, function(jwt_payload, verify) {
//here I look for the user in database No.1
}));
router.post('/files', passport.authenticate('jwt', { session: false}), function(req, res) {
//...
}
module.exports = router;
And this is is the code for api2 module:
var router = express.Router();
var options = {
jwtFromRequest:ExtractJwt.fromAuthHeader(),
secretOrKey:config.JWTSecret,
algorithms:['HS256']
}
passport.use(new JwtStrategy(options, function(jwt_payload, verify) {
//here I look for the user in database No.2
}));
router.post('/files', passport.authenticate('jwt', { session: false}), function(req, res) {
//...
}
module.exports = router;
This woun't work. In both cases, if I make POST to "/api1/files" and to "/api2/files" it will look for the user in database No2. If there is no solution for this problem, using passport.js api, what are the other posssible approaches for dealing with such kind of issue?
Suppose I have the code like this:
var api1 = require('api1');
var api2 = require('api2');
var app = express();
app.use('/api1', api1);
app.use('/api2', api2);
Here is the code for api1 module:
var router = express.Router();
var options = {
jwtFromRequest:ExtractJwt.fromAuthHeader(),
secretOrKey:config.JWTSecret,
algorithms:['HS256']
}
passport.use(new JwtStrategy(options, function(jwt_payload, verify) {
//here I look for the user in database No.1
}));
router.post('/files', passport.authenticate('jwt', { session: false}), function(req, res) {
//...
}
module.exports = router;
And this is is the code for api2 module:
var router = express.Router();
var options = {
jwtFromRequest:ExtractJwt.fromAuthHeader(),
secretOrKey:config.JWTSecret,
algorithms:['HS256']
}
passport.use(new JwtStrategy(options, function(jwt_payload, verify) {
//here I look for the user in database No.2
}));
router.post('/files', passport.authenticate('jwt', { session: false}), function(req, res) {
//...
}
module.exports = router;
This woun't work. In both cases, if I make POST to "/api1/files" and to "/api2/files" it will look for the user in database No2. If there is no solution for this problem, using passport.js api, what are the other posssible approaches for dealing with such kind of issue?
Share Improve this question asked Jun 15, 2016 at 22:14 Malik RajMalik Raj 431 silver badge4 bronze badges 1- i'm not actually clear on what you're asking. Are you saying that the two API's have different user sets, and pletely separate databases with the users in them? – Paul Commented Jun 16, 2016 at 12:12
1 Answer
Reset to default 12The trick to this is using the named strategy syntax. Basically when you call passport.use()
you can pass an optional first param that tells passport the name of the strategy, then use that name (rather than the default) with the authenticate
call. So in your case you could do something like:
passport.use('jwt-1', new JwtStrategy(options, function(jwt_payload, verify) {
//here I look for the user in database No.1
}));
router.post('/files', passport.authenticate('jwt-1', { session: false}), function(req, res) {
//...
}
Your api2 would then name its strategy 'jwt-2' or whatever makes sense to you.
本文标签: javascriptHow to use the same passport strategy for different routesStack Overflow
版权声明:本文标题:javascript - How to use the same passport strategy for different routes? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744009849a2575361.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论