Full-Stack Web Development with Vue.js and Node

(singke) #1
Building Authentication with passport.js Chapter 6

app.use(morgan('combined'));
app.use(bodyParser.json());
app.use(cors());
app.use(passport.initialize());

The preceding code just initialized passport.js in our application. We still need to


configure a couple of things to start using the JWT authentication mechanism.


passport.js strategies


As mentioned previously, passport.js provides a lot of strategies for easy integration.


One of the strategies that we will be working on with is the JWT strategy. We have already
added passport.js and initialized it. Now, let's add this strategy as well.


Installing the passport-jwt strategy


Just installing passport module is not sufficient for our needs. passport.js provides its


strategies in separate npm packages. For jwt authentication, we have to install the


passport-jwt module, as follows:


$ npm install passport-jwt --save

On successful installation, you should have these packages listed in the package.json file


of the application:


"nodemon": "^1.14.10",
"passport": "^0.4.0",
"passport-jwt": "^3.0.1",
"sass-loader": "^6.0.6",

Configuring the passport-jwt strategy


Now that we have all the things we need, let's jump into the configuration setting for the
JWT strategy. Add the following lines of code in server.js:


...
const morgan = require('morgan');
const fs = require('fs');
Free download pdf