Passport

Simple, unobtrusive authentication for Node.js.

README

passport banner

Passport


Passport is Express-compatible authentication
middleware for Node.js.

Passport's sole purpose is to authenticate requests, which it does through an
extensible set of plugins known as _strategies_.  Passport does not mount
routes or assume any particular database schema, which maximizes flexibility and
allows application-level decisions to be made by the developer.  The API is
simple: you provide Passport a request to authenticate, and Passport provides
hooks for controlling what occurs when authentication succeeds or fails.


Sponsors

Your app, enterprise-ready.
Start selling to enterprise customers with just a few lines of code. Add Single Sign-On (and more) in minutes instead of months.




Status: Build Coverage Dependencies


Install


  1. ```
  2. $ npm install passport
  3. ```

Usage


Strategies


Passport uses the concept of strategies to authenticate requests.  Strategies
can range from verifying username and password credentials, delegated
authentication using OAuth (for example, via Facebook
or Twitter), or federated authentication using OpenID.

Before authenticating requests, the strategy (or strategies) used by an
application must be configured.

  1. ``` js
  2. passport.use(new LocalStrategy(
  3.   function(username, password, done) {
  4.     User.findOne({ username: username }, function (err, user) {
  5.       if (err) { return done(err); }
  6.       if (!user) { return done(null, false); }
  7.       if (!user.verifyPassword(password)) { return done(null, false); }
  8.       return done(null, user);
  9.     });
  10.   }
  11. ));
  12. ```

There are 480+ strategies. Find the ones you want at: passportjs.org

Sessions


Passport will maintain persistent login sessions.  In order for persistent
sessions to work, the authenticated user must be serialized to the session, and
deserialized when subsequent requests are made.

Passport does not impose any restrictions on how your user records are stored.
Instead, you provide functions to Passport which implements the necessary
serialization and deserialization logic.  In a typical application, this will be
as simple as serializing the user ID, and finding the user by ID when
deserializing.

  1. ``` js
  2. passport.serializeUser(function(user, done) {
  3.   done(null, user.id);
  4. });

  5. passport.deserializeUser(function(id, done) {
  6.   User.findById(id, function (err, user) {
  7.     done(err, user);
  8.   });
  9. });
  10. ```

Middleware


To use Passport in an Express or
Connect-based application, configure it
with the required passport.initialize() middleware.  If your application uses
persistent login sessions (recommended, but not required), passport.session()
middleware must also be used.

  1. ``` js
  2. var app = express();
  3. app.use(require('serve-static')(__dirname + '/../../public'));
  4. app.use(require('cookie-parser')());
  5. app.use(require('body-parser').urlencoded({ extended: true }));
  6. app.use(require('express-session')({ secret: 'keyboard cat', resave: true, saveUninitialized: true }));
  7. app.use(passport.initialize());
  8. app.use(passport.session());
  9. ```

Authenticate Requests


Passport provides an authenticate() function, which is used as route
middleware to authenticate requests.

  1. ``` js
  2. app.post('/login',
  3.   passport.authenticate('local', { failureRedirect: '/login' }),
  4.   function(req, res) {
  5.     res.redirect('/');
  6.   });
  7. ```

Strategies


Passport has a comprehensive set of over 480 authentication strategies
covering social networking, enterprise integration, API services, and more.

Search all strategies


There is a Strategy Search at passportjs.org

The following table lists commonly used strategies:

|StrategyProtocol|Developer
|---------------------------------------------------------------|--------------------------|------------------------------------------------|
|[Local](https://github.com/jaredhanson/passport-local)HTML|[Jared
|[OpenID](https://github.com/jaredhanson/passport-openid)OpenID|[Jared
|[BrowserID](https://github.com/jaredhanson/passport-browserid)BrowserID|[Jared
|[Facebook](https://github.com/jaredhanson/passport-facebook)OAuth|[Jared
|[Google](https://github.com/jaredhanson/passport-google)OpenID|[Jared
|[Google](https://github.com/jaredhanson/passport-google-oauth)OAuth|[Jared
|[Twitter](https://github.com/jaredhanson/passport-twitter)OAuth|[Jared
|[AzureOAuth|[Azure](https://github.com/azuread)

Examples


- For a complete, working example, refer to the example
that uses passport-local.
- Local Strategy: Refer to the following tutorials for setting up user authentication via LocalStrategy (passport-local):
    - Mongo
      - Express v3x - Tutorial / working example
      - Express v4x - Tutorial / working example
    - Postgres
      - Tutorial / working example
- Social Authentication: Refer to the following tutorials for setting up various social authentication strategies:
    - Express v3x - Tutorial / working example
    - Express v4x - Tutorial / working example

Related Modules


- Locomotive — Powerful MVC web framework
- OAuthorize — OAuth service provider toolkit
- OAuth2orize — OAuth 2.0 authorization server toolkit
- connect-ensure-login  — middleware to ensure login sessions

The modules page on the
wiki lists other useful modules
that build upon or integrate with Passport.

License



Copyright (c) 2011-2021 Jared Hanson <[https://www.jaredhanson.me/](https://www.jaredhanson.me/)>