Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@passport-next/passport

Package Overview
Dependencies
Maintainers
3
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@passport-next/passport

Simple, unobtrusive authentication for Node.js.

  • 2.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
533
decreased by-29.59%
Maintainers
3
Weekly downloads
 
Created
Source

Passport-Next/Passport

Passport-Next/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.

Status: NPM version Build Status Coverage Status Maintainability Dependencies SAST

Differences between passport and passport-next

Passport Next was created as a fork of the Passport repositories when the upstream repositories became stale and stopped working due to changes at the various authentication providers (e.g. Facebook API deprecation, Tumblr using HTTPS etc.)

Passport Next aims to:

  • Keep the modules up to date with the various authentication providers
  • Maintain up to date dependencies
  • Address any security issues promptly
  • Ensure compatibility with the current supported versions of Node
  • Maintain the repositories in an organisation so maintaining isn't the responsibility of one person
  • Follow Semantic Versioning
  • Keep an up to date CHANGELOG.md

Passport Next does not aim to be backwards compatible with the upstream repositories. The changes required to keep up to date and functioning prohibit that so if you're migrating from the upstream modules please test your code thouroughly!

If you wish to join the team please raise an issue and one of the maintainers will assess your request.

Install

$ npm install @passport-next/passport

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.

passport.use(new LocalStrategy(
  function(username, password, done) {
    User.findOne({ username: username }, function (err, user) {
      if (err) { return done(err); }
      if (!user) { return done(null, false); }
      if (!user.verifyPassword(password)) { return done(null, false); }
      return done(null, user);
    });
  }
));

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.

passport.serializeUser(function(user, done) {
  done(null, user.id);
});

passport.deserializeUser(function(id, done) {
  User.findById(id, function (err, user) {
    done(err, user);
  });
});
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.

var app = express();
app.use(require('serve-static')(__dirname + '/../../public'));
app.use(require('cookie-parser')());
app.use(require('body-parser').urlencoded({ extended: true }));
app.use(require('express-session')({ secret: 'keyboard cat', resave: true, saveUninitialized: true }));
app.use(passport.initialize());
app.use(passport.session());
Authenticate Requests

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

app.post('/login', 
  passport.authenticate('local', { failureRedirect: '/login' }),
  function(req, res) {
    res.redirect('/');
  });
Protect Routes When Using Sessions

Passport provides an isAuthenticated() function on the request object, which is used to determine if the user has been authenticated and stored in the session.

app.post('/some/protected/route', 
  function(req, res, next) {
    if(req.isAuthenticated()){
      next();
    } else {
      next(new Error('Unauthorized'));
    }
  });

For a more complete solution to handling unauthenticated users, see connect-ensure-login, a middleware to ensure login sessions.

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:

StrategyProtocolDeveloper
LocalHTML formJared Hanson
OpenIDOpenIDJared Hanson
BrowserIDBrowserIDJared Hanson
FacebookOAuth 2.0Jared Hanson
GoogleOpenIDJared Hanson
GoogleOAuth / OAuth 2.0Jared Hanson
TwitterOAuthJared Hanson
Azure Active DirectoryOAuth 2.0 / OpenID / SAMLAzure

Examples

Tests

$ npm install
$ make test

Keywords

FAQs

Package last updated on 30 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc