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:
Open the terminal and type the following command:
npm install passport passport-local --save
Place the following code after the
app
express variable:// Passport configuration require('./server/config/passport')(passport);
Now, we need to create a
passport.js
file and the necessary code inside theconfig
folder. We can name this file with any name. However, to demonstrate the use of thepassport
module, we use the same name from the module. Create apassport.js
file in theconfig
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...