Book Image

Learning Single-page Web Application Development

Book Image

Learning Single-page Web Application Development

Overview of this book

Table of Contents (15 chapters)
Learning Single-page Web Application Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Adding the Passport middleware


As mentioned earlier, we use Passport to deal with user authentication in our API. Here, we will see how to use and store sessions and encrypt a user password to maintain a secure authentication.

First of all, let's install and save the Passport middleware to the application:

  1. Open the terminal and type the following command:

    npm install passport passport-local --save
    
  2. Place the following code after the app express variable:

    // Passport configuration
    require('./server/config/passport')(passport);
  3. Now, we need to create a passport.js file and the necessary code inside the config folder. We can name this file with any name. However, to demonstrate the use of the passport module, we use the same name from the module. Create a passport.js file in the config folder and place the following code:

    // Import passport module
    var LocalStrategy = require('passport-local').Strategy;
    
    // Import the user model
    var User = require('../../server/models/user');
    
    module.exports = function...