Full-Stack Web Development with Vue.js and Node

(singke) #1
Building OAuth Strategies with passport.js Chapter 7

Passport's Google strategy


The next strategy is the Passport's Google Strategy. Let's start with the installation of this
strategy.


Installing Passport's Google strategy


Run the following command to install Passport's Google strategy:


$ npm install passport-google-oauth20 --save

The preceding command should add the package to your package.json file:


"node-sass": "^4.7.2",
"nodemon": "^1.14.10",
"passport": "^0.4.0",
"passport-google-oauth20": "^1.0.0",

Configuring Passport's Google strategy


The configuration for all the strategies is somewhat similar. For the Google strategy, the
following are the steps we have to follow for configuration:



  1. Creating and registering an application on Google. This will provide us with a
    consumer key (API key) and a consumer secret (API secret).

  2. Adding a button to our login page that allows our users to log in via Google.

  3. Adding the necessary routes.

  4. Adding a middleware method to check authentication.

  5. Redirecting the user to the home page and displaying the logged-in user's email
    in the top bar.


Let's dive into the details for each of the preceding steps.

Free download pdf