# dynamic-passport-strategies

> dynamicaly add and remove passport strategies in your express application during runtime

Latest version **0.0.5** (published 2019-08-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install dynamic-passport-strategies
pnpm add dynamic-passport-strategies
yarn add dynamic-passport-strategies
bun add dynamic-passport-strategies
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.5 |
| Published | 2019-08-20 |
| First published | 2019-08-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 92.9 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | João Moura |
| Maintainers | jwebcoder |
| Keywords | dynamic, passport, strategies, express, runtime |

## Links

- npm: https://www.npmjs.com/package/dynamic-passport-strategies
- Repository: https://github.com/JWebCoder/dynamic-passport-strategies
- Homepage: https://github.com/JWebCoder/dynamic-passport-strategies#readme
- Issues: https://github.com/JWebCoder/dynamic-passport-strategies/issues
- npm.io page: https://npm.io/package/dynamic-passport-strategies

## Dependencies (8)

- [async](https://npm.io/package/async.md) ^3.1.0
- [debug](https://npm.io/package/debug.md) ^4.1.1
- [dotenv](https://npm.io/package/dotenv.md) ^8.0.0
- [express](https://npm.io/package/express.md) ^4.17.1
- [shortid](https://npm.io/package/shortid.md) ^2.2.14
- [passport](https://npm.io/package/passport.md) ^0.4.0
- [http-errors](https://npm.io/package/http-errors.md) ^1.7.3
- [express-session](https://npm.io/package/express-session.md) ^1.16.2

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@oxyhq/services](https://npm.io/package/@oxyhq/services.md) — 2.3K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads

## Recent versions

- 0.0.5 (latest) — 2019-08-20
- 0.0.4 — 2019-08-05
- 0.0.3 — 2019-08-05
- 0.0.2 — 2019-08-02
- 0.0.1 — 2019-08-02

## README

# 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**

## Installation

`npm i dynamic-passport-strategies`

## Usage

```js
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
authentication.configure({
  modulesPath: path.join(__dirname, './authentication')
  strategies: ['facebook'],
})

const app = express()
app.use([
  session({
    secret: 'stuff thats cool',
    saveUninitialized: true,
    resave: true,
  }),
  authentication.initialize(),
  authentication.session(),
])

// 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**
```js
...
// just activate it
authentication.configure({
  cluster: true,
  modulesPath: path.join(__dirname, './authentication'),
  strategies: ['facebook'],
})

const app = express()
app.use([
...
```

**And on the master node**

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

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

```

## 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

```js
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
    this.passport.use(
      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()

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

    router.get('/login/facebook/return',
      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

---
_Source: https://npm.io/package/dynamic-passport-strategies · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
