Sitemap

JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Google API Authentication using OAuth2.0 and Passport.js in Node.js

5 min readApr 8, 2020

--

Press enter or click to view image in full size
Let’s buzzzz!

What is OAuth?

OAuth is an authorization protocol and it stands for Open Authorization. Many sites including Medium use it to directly log into another third-party application without filling a separate form. Now, users don’t need to remember passwords separately for different applications. With the help of OAuth, the user can log in to multiple sites using the account details of the primary account. Like here we will be using a Google account.

What is Passport.js and why do we need it?

Passport is authentication middleware for Node. It is required to handle the authentication of requests. It is an authentication module used with the Express framework and helps in web development.

Prerequisites

  1. Install Node.js:

Run these commands in cmd/terminal.

sudo apt-get install curlcurl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -sudo apt-get install -y nodejs

2. Install npm:

If you have the latest version of Nodejs you don’t need to install npm separately, otherwise to install the latest version of npm globally run this command in cmd/terminal.

sudo npm install npm@latest -g

3. Install express:

If your npm is of the latest version you don’t need to mention — save.

npm install ejs express — save

4. Install passport and passport strategy for authenticating with Google API:

npm install passport passport-google-oauth20

Code snippet:

Passport-setup.js
Passport-setup.js

Below mentioned are the steps to be followed:

  • The clientID and clientSecret used above are to be taken from the https://console.developers.google.com.
  • Remember these keys are confidential, hence I have put them in a separate file.
  • Make sure you already have a Google account. Log in with that.
  • This is the Google Developers Console’s Dashboard.
Press enter or click to view image in full size
Google Developer console’s Dashboard
  • If you want to create a new project, click on the arrow beside the Oauthtest.
Press enter or click to view image in full size
Adding a new project
  • Click on the NEW PROJECT.
Adding Project Name
  • After this click on the CREATE button.
Press enter or click to view image in full size
Dashboard
  • Now, you will be seeing the Dashboard. You need to Enable the APIS*
  • Click on the ENABLE APIS AND SERVICES.
Press enter or click to view image in full size
Searching for the API
  • Search for Google+ API.
Press enter or click to view image in full size
Selecting API
  • Select the second one as per my list i.e. Google+ API.
Press enter or click to view image in full size
Enabling API
  • Click on the ENABLE button to enable the Google+ API.
  • Select Credentials from the left pane.
Press enter or click to view image in full size
Creating Credentials
  • Click on the CREATE CREDENTIALS.
Creating Credentials
  • Choose OAuth client ID.
Press enter or click to view image in full size
Creating OAuth client ID
  • Click on Configure Consent Screen.
Press enter or click to view image in full size
Creating OAuth client ID
  • Choose External and click on CREATE button.
Press enter or click to view image in full size
Creating OAuth client ID
  • Enter the application name:
  • Upload the image for the logo.
  • Support email will be your Gmail account entered by default.
  • In Authorized domains, enter your website, you can enter anything I’ll be entering google.com.
  • In Application Homepage link, enter http://www.google.com
  • In Application Privacy Policy link, enter http://www.google.com/Policy
  • In Application Terms of Service link, enter http://www.google.com/Service
  • Click on the Save button.
  • Again go to Credentials>Create Credentials>OAuth client ID.
Creating OAuth client ID
  • Choose your required Application type. We are working on a web app so I’ll go with Web Application.
  • Fill the Name and restrictions as shown in the figure above.
  • Then click on the Create button.

Authorized Javascript Origins is the the URL where your application is running so that Google can know where the application is going to run.

Authorized Redirect URIs is the link where the application will be redirected after the user grants the permission to access the information from the Google Account.

Press enter or click to view image in full size
OAuth Client ID created
  • Click on the Web client 1 to access the ClientID and ClientSecret keys.
Press enter or click to view image in full size
OAuth Client ID created
  • Copy these Client ID and Client Secret keys into your code.

This makes OAuth2.0 applicable in your application.

Conclusion:

This is my first blog and I hope you liked this.

To know more, you can read from https://console.developers.google.com. You can also read from Nodejs Official Documentation, Express Official Documentation and Passport Official Documentation to enhance your learning.

For any suggestions or improvements, you can write in the response section below. You can also connect with me on LinkedIn, Twitter and GitHub. Thank you for taking the time to read this. Good luck!

More content at plainenglish.io

--

--

JavaScript in Plain English
JavaScript in Plain English

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Ishu Khanchi
Ishu Khanchi

Written by Ishu Khanchi

“The things that make me different are the things that make me me.”- Piglet

No responses yet

Write a response