In this blog, we’ll be implementing authentication via Google in a Node.js web application. For this, we’ll be using Passport.js, an authentication package for Node.js.

Before You Get Started

This tutorial assumes you have:

  • Basic knowledge of HTML/CSS
  • A good understanding of JavaScript and Node.js
  • Latest Node.js version installed on your system

Step 1: Create a Google client ID and client secret

We can create a client ID and client secret using its Google API Console. You need to follow below steps once you open Google API Console

  • From the project drop-down, select an existing project, or create a new one by selecting Create a new project
  • In the sidebar under “APIs & Services”, select Credentials
  • In the Credentials tab, select the Create credentials drop-down list, and choose OAuth client ID.
  • Under Application type, select Web application.
  • In Authorized redirect URI use http://localhost:3000/auth/google/callback
  • Press the Create button and copy the generated client ID and client secret

Note: If Google doesn’t support http://localhost:3000, then use http://127.0.0.1:3000

Step 2: Initialize a node.js project with all the dependencies

First in an empty folder run the below command

npm init

It essentially just creates the package.json file with all the basic information you will provide. after that, we will install all the dependencies needed in our project

  • express: Node.js framework to create a server and accept requests
  • ejs: To render HTML pages for login and profile
  • express-session: To save information from google in session and use it on the success page
  • passport: Social Authentication package for Node.js
  • passport-google-oauth: Google authentication module by Passport.js
npm install express ejs express-session passport passport-google-oauth --save

#node.js #passport.js

Google authentication with Node.js and Passport.js
17.25 GEEK