passport-local
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
passport-strategy | 1.0.0 | 2.42 kB | MIT | prod |
Passport-Local is an authentication strategy for Passport.js. It allows applications to authenticate users using a username and password in Node.js applications. This strategy, as part of Passport, can be effortlessly integrated into any application or framework that supports Connect-style middleware, including Express.
Using Passport-Local involves a straightforward process. First, you need to install it via npm with the command npm install passport-local
.
Then you configure the strategy which requires a verify
callback. This callback accepts the username and password, and calls done
providing a user. Here's an example:
var passport = require('passport');
var 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); }
if (!user.verifyPassword(password)) { return done(null, false); }
return done(null, user);
});
}
));
To authenticate requests, use passport.authenticate()
, specifying the 'local'
strategy. For example:
var express = require('express');
var passport = require('passport');
var app = express();
app.post('/login',
passport.authenticate('local', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
}
);
These examples serve as a basic guide, but for comprehensive usage and implementation, refer to the multiple examples included in the package's repository.
For a detailed and thorough understanding of Passport-Local, you'll want to check out the official Passport-Local documentation, which is located in its GitHub repository (git://github.com/jaredhanson/passport-local.git). This page contains all the necessary instructions and explanations for everything you would need to about Passport-Local.