passport-local Documentation

repository·master·Indexed 25 days ago

https://github.com/jaredhanson/passport-local

A Passport.js strategy for authenticating users via a local username and password. It integrates with Connect-style middleware like Express and provides a LocalStrategy constructor with configurable options for usernameField, passwordField, session support, and request object passing.

Tokens
823
Snippets
4
Records
5
Agent score
33%

What's inside passport-local

  1. Configure LocalStrategy options

    master

    The LocalStrategy constructor accepts an optional configuration object before the verify callback. Use these options to customize how credentials are extracted from the request body and how the callback behaves.

    Available options:

    • usernameField: The name of the username property in the POST body (defaults to 'username').
    • passwordField: The name of the password property in the POST body (defaults to 'password').
    • session: Set to false to disable session support.
    • passReqToCallback: Set to true to include the request object as the first argument in the verify callback.
    passport.use(new LocalStrategy({
        usernameField: 'email',
        passwordField: 'passwd',
        passReqToCallback: true,
        session: false
      },
      function(req, username, password, done) {
        // request object is now first argument
        // ...
      }
    ));
  2. Authenticate requests using passport.authenticate()

    master

    To trigger the authentication process, use passport.authenticate() middleware in your routes, specifying 'local' as the strategy name. You can provide options like failureRedirect to specify where to send the user if authentication fails.

    app.post('/login', 
      passport.authenticate('local', { failureRedirect: '/login' }),
      function(req, res) {
        res.redirect('/');
      });
  3. Configure the LocalStrategy

    master

    To use the local authentication strategy, instantiate LocalStrategy and pass it to passport.use(). You must provide a verify callback function that receives the credentials and calls done(err, user) to signal the result of the authentication attempt.

    By default, the callback arguments are (username, password, done). If you set the passReqToCallback option to true, the request object will be passed as the first argument: (req, username, password, done).

    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);
        });
      }
    ));
  4. Import the passport-local Strategy

    master
    The passport-local module exports the Strategy constructor directly as the main module export, and also as a named property Strategy. You can use either approach to import the strategy for username and password authentication in Passport.js.