Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
passport-local
Advanced tools
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.
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 })
);
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.
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.
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 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.
$ npm install passport-local
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);
});
}
));
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('/');
});
For complete, working examples, refer to the multiple examples included.
$ npm install
$ npm test
Copyright (c) 2011-2014 Jared Hanson <http://jaredhanson.net/>
FAQs
Local username and password authentication strategy for Passport.
The npm package passport-local receives a total of 802,496 weekly downloads. As such, passport-local popularity was classified as popular.
We found that passport-local demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.