I have a node application with passport library. I use passport strategy like this:
passport.use(someStrategy)
Is it possible to override that strategy later on dynamically? During the application run, I would want to at some point use a different strategy. Actually same strategy, but with a different configuration.
If I just make another passport.use(someOtherStrategy), then doesn't that just add another 'middleware' to passport? Then that wouldn't delete the old one, just add one more. I would want the old one to be deleted. So either override, or delete and add a new one.
I have a node application with passport library. I use passport strategy like this:
passport.use(someStrategy)
Is it possible to override that strategy later on dynamically? During the application run, I would want to at some point use a different strategy. Actually same strategy, but with a different configuration.
If I just make another passport.use(someOtherStrategy), then doesn't that just add another 'middleware' to passport? Then that wouldn't delete the old one, just add one more. I would want the old one to be deleted. So either override, or delete and add a new one.
Share Improve this question edited Feb 19, 2018 at 17:07 Martijn Pieters 1.1m321 gold badges4.2k silver badges3.4k bronze badges asked Aug 28, 2017 at 7:15 Ville Miekk-ojaVille Miekk-oja 21k34 gold badges78 silver badges116 bronze badges2 Answers
Reset to default 10Digging in passport source code revealed that overriding can be done easily. Here is the relevant part of the code:
Authenticator.prototype.use = function(name, strategy) {
if (!strategy) {
strategy = name;
name = strategy.name;
}
if (!name) { throw new Error('Authentication strategies must have a name'); }
this._strategies[name] = strategy;
return this;
};
...
...
Authenticator.prototype.unuse = function(name) {
delete this._strategies[name];
return this;
};
As can be seen from the code, if the strategy that you use has a name that is already used by another strategy in the _strategies list, then it is replaced by the new strategy. Also one can delete the strategy with method unuse, as seen in the code also.
@Mitch Your answer is helpfull, but little off-topic. Probably partly because I was not super clear that I was searching for a way to override an existing strategy, not just how to configure multiple strategies. Sorry, I wasn't super clear in my question description.
It's possible to configure multiple named strategies in passport, even of the same type. Below I can have two instances of myStrategy with different configs, but named differently,
For example:
passport.use('someStrategy', new myStrategy(options))
passport.use('anotherStrategy', new myStrategy(differentOptions));
Then later, when authenticating you can specify which strategy to use:
passport.authenticate('someStrategy', ...);
Using the above, you can configure multiple strategies, and based on a conditional, decide which strategy to use:
if (condition){
passport.authenticate('someStrategy', ...);
}
else{
passport.authenticate('anotherStrategy', ...);
}
Or:
let strategyToUse = determineStrategy(); //dynamically choose which strategy you want
passport.authenticate(strategyToUse, ...);
Deleting the strategy from the middleware stack is a little trickier and there's no inbuilt function to do it I don't think. It may involve splicing the strategy out of the stack manually. This question might help you with removing middleware from the stack; its aimed towards express/connect so should also apply to passport to some extent.