Google authentication with Node.js and Passport.js

Google authentication with Node.js and Passport.js

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.

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

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

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Hire Node.js Developers And How Much Does It Cost?

A Guide to Hire Node.js Developers who can help you create fast and efficient web applications. Also, know how much does it cost to hire Node.js Developers.

Hands on with Node.Js Streams | Examples & Approach

The practical implications of having Streams in Node.js are vast. Nodejs Streams are a great way to handle data chunks and uncomplicate development.

Node.js Performance: Node.js vs. Io.js

You may already be aware that Raygun uses Node.JS for our API nodes that receive your precious crash reporting data (we also do node.js crash reporting if you’re interested). We’ve peaked in the past at more than 110,000 requests per second coming...

Node.js Live | Node.js Docker Tutorial | Dockerizing Node.js App|Node.js Training|Edureka

🔥 Node.js Certification Training: This Edureka video on 'Node.js Docker Tutorial' will help you in learn...

Node JS Complete Course PDF | Node.js

There are some Features that choose Node.js the foremost decision of programming designers.