Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Chapter 3: Authentication using node.js(passport.js) and mongoDb

    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 864
    Comment on it

    As we all know authentication plays an important role in every site. Here In this blog I am explaining how we will authenticate our site in node.js as a backend and mongoDb as a database. To authenticate I am using passport.js which will help to login with facebook,twitter and google etc.

    In this article I am explaining local authentication with a mongoDb backend.

    Prerequisites:

    Install node.js,express.js and mongoDb .For installing them please read my previous blog.

    when all installation are completed then we will create a new project using express.

    express AuthApp
    cd AuthApp
    npm install

    Next we will install passport module by using below command.

    npm install passport
    npm install passport-local

    After completing this we will run our server.

    node app.js

    then our server will start.

    Implementing Local Authentication:Now we will do authentication first we will create the view. Let give the name as login.html.Below is the code for that:

    <html>
      <body>
        <form action="/login" method="post">
          <div>
            <label>Username:</label>
            <input type="text" name="username" />
            <br/>
          </div>
          <div>
            <label>Password:</label>
            <input type="password" name="password" />
          </div>
          <div>
            <input type="submit" value="Submit" />
          </div>
        </form>
      </body>
    </html>
    

    then we will define the route in app.js

    app.get('/login', function(req, res) {
      res.sendfile('views/login.html');
    });

    Now we will create login post method to authenticate. For this we will require passport module.

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

    We also need to add the Passport middleware for authentication to work. Before the line that reads:

    app.use(app.router);
    app.use(passport.initialize());
    app.use(passport.session());

    Now we will define the login handler routes which are shown below.

    app.post('/login',
      passport.authenticate('local', {
        successRedirect: '/loginSuccess',
        failureRedirect: '/loginFailure'
      })
    );
    
    app.get('/loginFailure', function(req, res, next) {
      res.send('Failed to authenticate');
    });
    
    app.get('/loginSuccess', function(req, res, next) {
      res.send('Successfully authenticated');
    });
    

    Passport also needs to serialize and deserialize the user instance, so add the following code.

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

     

    By this way we have completed all the step of performing authentication in node js. Now we will
    run our mongoDb server and connect our Db with node js which I have explained in my previous chapter.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
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: