Integrate Google Oauth in your Node.Js applications

Google Oauth is an authentication API by Google which makes logging in easy for users as well as for developers for storing the user data.

Jugal Bhatt
CodeChef-VIT
7 min readJun 27, 2020

--

Ever wondered how different sites use Google authentication in order to make your signup/login process a whole lot easier. Well, Google Oauth is something that they use in order to achieve this.

We are going to go step by step about how to apply this in your Node.Js applications using Passport.Js and Express.Js

First of all, making an app.js file in your root directly and starting a server using Express.Js and mongoose to connect to your MongoDB database. In order to do this, you’ll need to install all the npm dependencies. To initialize ‘npm’ in your project use in your terminal:

Workflow

https://phppot.com

Project Initialization

and set all the options as per your wish, I usually keep them in their default state.

You should be able to see something like this in your terminal.

After you are done with this NodeJs would create a package.json file for you, where all your dependencies and their versions should be visible.

After that use

in order to install all the required dependencies to start a server and connect with MongoDB.

App.js initialization

Initialise your express server and MongoDB using the code below

Your terminal should log when you use :

Voila! You’ve successfully initialised the server and connected your database

We need to make a user model in the models folder inside your root directory who is going to login to our app using Google Oauth having code :

User model

This is the user schema that we are going to use in order to store the user in our database

Create a routers model and add user.js and auth.js files to it, your directory should now look like :

After you’re done creating the model, it is time to explore the Google Oauth API

Go to

console.developers.google.com

Google API setup

Creating project and enabling API

console.google.developers.com

Click on select a project and create a new project :

console.developers.google.com

Enter your project name and organisation name if any :

console.developers.google.com

Now enable API and Services

console.developers.google.com

Now search for Google+ and select the Google+ API and Enable it

console.developers.google.com

You should be brought to a page like this and now click on create credentials

console.developers.google.com

select Google+ API on the form and save the access key somewhere with you .

Configuring and generating Credentials

You would then be redirected to a page like this and click on configure the consent screen

Select External if you want all the Google users to be able to signup and fill the next page as shown by changing your project name

console.developers.google.com
console.developers.google.com

After you save it successfully, go to credentials page and click on create credentials and select OAuth client ID

console.developers.google.com

Now this Form should open select your application type and enter your application name, now enter the URLs as I’ve done and you are good to go

console.developers.google.com
console.developers.google.com

A dialog box like this should occur with your credentials, make sure you save the credentials somewhere

console.developers.google.com

You are now all set on the Google developer console and now going back to VScode to setup our Passport.Js

Create a config folder in the root directory and add the passport-setup.js file to it.

Google-passport-setup

Now inside the passport-setup.js file add this snippet of code

Now explaining the snippet above, we first import all the necessary packages and file that we are going to use in the code. Enter your clientID and clientSecret that you received from Google developers console.

What is callbackURL — It is the URL on which Google will redirect your users to once they have successfully signedup/logged in. The access token is a token generated by Google which is automatically entered into the callback function in order to authenticate the user that just signed in, refreshTokens are used for offline apps, the profile is something that contains the user’s entire data which is returned from Google, I only took the data which my user model requires which is googleId, name and email but if you

you will get the entire user data that Google allows you to have.

The functions serializeUser and deserializeUser are used in order to generate cookies and actually get the user data when the user signs in so that he/she can be authenticated and the data could be further processed

Routes and final setup

Auth Routes

Now in /routers/auth.js add this code :

The scope used in router.get(“/Google”…..) is to define which data fields we actually require from Google according to the profile in the callback function as we recall

It will automatically redirect you to /auth/Google/redirect as we added that to the callbackURL in the GoogleStrategy function in passport-setup.js

App.js Final additions

Now in app.js add this snippet of code before calling the mongoose.connect() function:

app.use(“/auth”,…..) because the URL to redirect to Google signup/login using http://localhost:3000/auth/Google

Testing and checks

you now run the app using node app.js you should still see the same console.log() which you saw in the beginning. Now open your browser and go to http://localhost:3000/auth/Google, It should redirect you to the Google sign-in page

accounts.google.com

Once you select any account it should redirect you to router.get(‘/Google/redirect’,…..) in which we sent the data of the user as a response, you can redirect directly from there using res.redirect too.

but what we have kept should display an output page like

which has already been saved to the database you created.

Congrats you’ve successfully integrated the Google Oauth API in your nodejs application.

Github repository link : https://github.com/jugaldb/Google-Oauth-Nodejs

--

--

Jugal Bhatt
CodeChef-VIT

Software Developer || Student at VIT university.