What is passport-local?
The passport-local npm package is a Passport strategy for authenticating with a username and password. It is designed to be simple and unopinionated, allowing developers to implement their own authentication logic.
What are passport-local's main functionalities?
Local Strategy Setup
This code sets up the LocalStrategy for Passport, which will authenticate users based on a username and password. The `User.findOne` function is a placeholder for your user lookup logic.
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
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, { message: 'Incorrect username.' }); }
if (!user.validPassword(password)) { return done(null, false, { message: 'Incorrect password.' }); }
return done(null, user);
});
}
));
Serialize and Deserialize User
These functions are used to serialize the user information into the session and deserialize it back. This is necessary for maintaining login sessions.
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
Middleware for Authentication
This code demonstrates how to use the `passport.authenticate` middleware in an Express route to handle user login. It redirects users based on the success or failure of the authentication.
app.post('/login',
passport.authenticate('local', { successRedirect: '/', failureRedirect: '/login', failureFlash: true })
);
Other packages similar to passport-local
passport-jwt
The passport-jwt package is a Passport strategy for authenticating with a JSON Web Token. It is used for stateless authentication, where the token is usually sent in the HTTP headers. Unlike passport-local, which uses sessions, passport-jwt is suitable for APIs and mobile applications.
passport-oauth2
The passport-oauth2 package is a Passport strategy for authenticating with OAuth 2.0. It is used for third-party authentication providers like Google, Facebook, and GitHub. This package is more complex than passport-local as it involves redirecting users to the provider's site for authentication.
passport-google-oauth20
The passport-google-oauth20 package is a Passport strategy for authenticating with Google using OAuth 2.0. It is specifically tailored for Google authentication and provides a streamlined way to integrate Google login into your application, unlike passport-local which is for username and password authentication.
passport-local
Passport strategy for authenticating with a username
and password.
This module lets you authenticate using a username and password in your Node.js
applications. By plugging into Passport, local authentication can be easily and
unobtrusively integrated into any application or framework that supports
Connect-style middleware, including
Express.
Install
$ npm install passport-local
Usage
Configure Strategy
The local authentication strategy authenticates users using a username and
password. The strategy requires a verify
callback, which accepts these
credentials and calls done
providing a user.
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);
});
}
));
Authenticate Requests
Use passport.authenticate()
, specifying the 'local'
strategy, to
authenticate requests.
For example, as route middleware in an Express
application:
app.post('/login',
passport.authenticate('local', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});
Examples
For complete, working examples, refer to the multiple examples included.
Tests
$ npm install
$ npm test
Credits
License
The MIT License
Copyright (c) 2011-2014 Jared Hanson <http://jaredhanson.net/>