Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Node.js authentication with passport local

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 684
    Comment on it

    In node.js authentication and login could be a difficult task but there are some modules available in node that makes it easier for a node developer. One of them is Passport.
    Passport is a middleware that allows us to authenticate using username and password called as local strategy. Apart from local strategy, passport has a set of strategies for authentication like facebook, twitter and so on, based on our requirement we can use any of them.
    In this particular blog i am going to use local authentication.
    First of all i am going to create an application using express generator:

    express passportAuthApp
    cd passportAuthApp
    npm install

    Now we will install passport and passport-local Node modules using the below commands:

    npm install passport
    npm install passport-local

    Once installation is done we create a login form, so create a file view/login.html:

        <form action="/login" method="post">
            <input type="text" name="username" />
            <input type="password" name="password" />
            <input type="submit" value="Submit" />

    Inside app.js, add the following route:

    app.get('/', function(req, res) {

    Now open the browser and type http://localhost:3000, you will see a login page.
    To handle authentication we need to include passport in our app.js:

    var passport = require('passport');
    var localStrategy = require('passport-local').Strategy;

    Now we need to add routes for login page that we have created.'/login',
      passport.authenticate('local', {
        successRedirect: '/home',
        failureRedirect: '/'
    app.get('/home', function(req, res, next) {
      res.send('Successfull authentication');

    We also needs to serialize and deserialize the user instance:

    passport.serializeUser(function(user, done) {
      done(null, user);

    passport.deserializeUser(function(user, done) {
      done(null, user);

    As we are going to use local database for authentication, we have to connect to Mongo:
    for that first we install mongoose with the command below:

    npm install mongoose

    Now add Mongoose to app.js using the following code.

    var mongoose = require('mongoose');

    In order to access data in mongo, we need to have schema and model.

    var schema = mongoose.Schema;
    var userSchema = new schema({
          username: String,
          password: String
    var User = mongoose.model('User', userSchema);

    Finally we include our logic to authenticate the user using User model.

    passport.use(new localStrategy(function(username, password, done) {    
        process.nextTick(function() {
          'username': username,
        }, function(err, user) {
          if (err) {
              return done(err);
          if (!user) {
              return done(null, false);
          if (user.password != password) {
              return done(null, false);
          return done(null, user);

    Now fill the username and password and submit the login form.User will be authenticated and redirected to the corresponding page.

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: