https://github.com/compwright/passport-compose
Utilities for chaining multiple Passport authentication middlewares
https://github.com/compwright/passport-compose
Last synced: 2 months ago
JSON representation
Utilities for chaining multiple Passport authentication middlewares
- Host: GitHub
- URL: https://github.com/compwright/passport-compose
- Owner: compwright
- License: mit
- Created: 2018-08-17T15:25:02.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-07-18T21:16:58.000Z (over 2 years ago)
- Last Synced: 2025-01-30T09:02:53.006Z (about 1 year ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/passport-compose
- Size: 160 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# passport-compose
[](https://david-dm.org/compwright/passport-compose)
[](https://www.npmjs.com/package/passport-compose)
[](https://github.com/sponsors/compwright)
Utilities for chaining multiple Passport authentication middlewares
## Use case
You have multiple Passport authentication strategies, and you need _all_ of them to run before considering the user to be authenticated.
For instance, you may wish to authenticate with username and password (via passport-local), then also pass an OTP or CAPTCHA check in a separate strategy.
## Requirements
* Node.js 8+
* Passport strategies of your choice
## Installation
```bash
$ npm install --save passport-compose
```
## Usage
```javascript
const passportCompose = require("passport-compose");
const { compose, loginRedirect, isAuthenticated } = passportCompose({
// optional configuration
sessionStageField: "passport.stage",
sessionLoginRedirectField: "redirectTo",
successRedirect: "/",
successReturnToOrRedirect: "/",
failureRedirect: "/login"
});
// This example assumes you have set up an express application called "app",
// and have required and configured "local" and "otp" strategies for passport
// Do not set "successRedirect" or "successReturnToOrRedirect" here, or a
// passing strategy will redirect before the next one runs
const localSettings = { failureRedirect: "/login" };
const otpSettings = { failureRedirect: "/login" };
// Require successful username+password and OTP authentication, then redirect
const strategies = [
passport.authenticate("local", localSettings)),
passport.authenticate("otp", otpSettings))
];
app.post("/login", compose(strategies), loginRedirect());
// To require all strategies to pass before accessing a resource:
app.get("/protected", isAuthenticated(), (req, res) => {
res.sendStatus(204);
});
```
## License
MIT license