dynamicaly add and remove passport strategies in your express application during runtime
Last updated a year ago by jwebcoder .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install dynamic-passport-strategies 
SYNC missed versions from official npm registry.

Dynamic Passport Strategies

Dynamically add and remove passport strategies in your express application during runtime

Supports NodeJS cluster by spreading the configuration across the multiple NodeJS instances

Check the example folder for a fully working application


npm i dynamic-passport-strategies


import express from 'express'
import session from 'express-session'
import path from 'path'
import authentication from 'dynamic-passport-strategies'

// set the folder containing your strategies, authentication for example
  modulesPath: path.join(__dirname, './authentication')
  strategies: ['facebook'],

const app = express()
    secret: 'stuff thats cool',
    saveUninitialized: true,
    resave: true,

// setup the strategies to use and add them to the application middleware
app.use('/', authentication.router)

app.listen(9000, function() {
  debug('Process ' + process.pid + ' is listening on port 9000')

Using Cluster Module

On the worker node

// just activate it
  cluster: true,
  modulesPath: path.join(__dirname, './authentication'),
  strategies: ['facebook'],

const app = express()

And on the master node

import cluster from 'cluster'
import { authenticationCluster } from 'dynamic-passport-strategies'

if (cluster.isMaster) {
  // initialize the authentication control
} else {

Adding a strategy

Create a new file called [strategy].[js,ts] inside the folder ./authentication like we set above

All strategies inside the folder ./authentication used on this example can be activated or deactivated

import { Router } from 'express'
import { PassportStatic } from 'passport'
import passportFacebook from 'passport-facebook'

const Strategy = passportFacebook.Strategy

class FacebookAuth {
  private passport: PassportStatic

  // constructor is required and it should add the strategy into passport middleware
  constructor(passport: PassportStatic) {
    this.passport = passport
      new Strategy(/* check passport-facebook module for a complete description https://github.com/jaredhanson/passport-facebook */)

  // routes must return an express.Router with the desired endpoints for authentication
  public routes() {
    const router = Router()

      this.passport.authenticate('facebook', { scope: ['email'], failureRedirect: '/login' })

      this.passport.authenticate('facebook', { scope: ['email'], failureRedirect: '/login' }),
      function(req, res) {
        res.render('facebook/loggedIn.html', {username: req.user.name})

    return router

export default FacebookAuth

De/Activating Strategies On Request

Authenticate with the administration role into the application with one of the currently active authentication systems, and use

/load/:strategy - activates a currently deactivated strategy /unload/:strategy - deactivates a currently active strategy

Current Tags

  • 0.0.5                                ...           latest (a year ago)

5 Versions

  • 0.0.5                                ...           a year ago
  • 0.0.4                                ...           a year ago
  • 0.0.3                                ...           a year ago
  • 0.0.2                                ...           a year ago
  • 0.0.1                                ...           a year ago
Maintainers (1)
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (8)
Dev Dependencies (23)
Dependents (0)

Copyright 2014 - 2016 © taobao.org |