Questions And Answers

More Tutorials

NodeJS Passport integration

Local authentication

The passport-local module is used to implement a local authentication.

This module lets you authenticate using a username and password in your Node.js applications.

Registering the user :

const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
// A named strategy is used since two local strategy are used :
// one for the registration and the other to sign-in
passport.use('localSignup', new LocalStrategy({
 // Overriding defaults expected parameters,
 // which are 'username' and 'password'
 usernameField: 'email',
 passwordField: 'password',
 passReqToCallback: true // allows us to pass back the entire request to the callback .
function(req, email, password, next) {
 // Check in database if user is already registered
 findUserByEmail(email, function(user) {
 // If email already exists, abort registration process and
 // pass 'false' to the callback
 if (user) return next(null, false);
 // Else, we create the user
 else {
 // Password must be hashed !
 let newUser = createUser(email, password); {
 // Pass the user to the callback
 return next(null, newUser);

Logging in the user :

const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
passport.use('localSignin', new LocalStrategy({
 usernameField : 'email',
 passwordField : 'password',
function(email, password, next) {
 // Find the user
 findUserByEmail(email, function(user) {
 // If user is not found, abort signing in process
 // Custom messages can be provided in the verify callback
 // to give the user more details concerning the failed authentication
 if (!user)
return next(null, false, {message: 'This e-mail address is not associated with any
 // Else, we check if password is valid
 else {
 // If password is not correct, abort signing in process
 if (!isPasswordValid(password)) return next(null, false);
 // Else, pass the user to callback
 else return next(null, user);

Creating routes :

// ...
// Sign-in route
// Passport strategies are middlewares'/login', passport.authenticate('localSignin', {
 successRedirect: '/me',
 failureRedirect: '/login'
// Sign-up route'/register', passport.authenticate('localSignup', {
 successRedirect: '/',
 failureRedirect: '/signup'
// Call req.logout() to log out
app.get('/logout', function(req, res) {


In this page (written and validated by ) you learned about NodeJS Passport integration . What's Next? If you are interested in completing NodeJS tutorial, your next topic will be learning about: NodeJS Dependency Injection.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.