DEV Community

Cover image for Implementing Sign In with Google in NodeJS (without third-party libraries)

Implementing Sign In with Google in NodeJS (without third-party libraries)

Akshat Mittal on February 20, 2024

Introduction Recently I was building a project in NextJS in which I needed to implement 'Sign in with Google' without third-party librar...
Collapse
 
leye_ade_55 profile image
Faith Olubummo

Hello Akshat,

Thank you for your post. I believe there is a need to adjust the environment variable for the google client secret as shown below:
From
NEXT_PUBLIC_GOOGLE_OAUTH_CLIENT_SECRET="your oauth client secret"
To
GOOGLE_OAUTH_CLIENT_SECRET="your oauth client secret"

The "NEXT_PUBLIC_" should only be used when you intend to expose your environment variable to the user's client or browser.

Checkout the nextjs official docs for more information on this topic

Collapse
 
faisaln profile image
Faisal • Edited

Hey, amazing article! It's helping me with integrating Google Sign-in with my webapp. I had to convert some of the code to Vanilla JavaScript, but that was pretty easy!

I did find a couple of errors in your code (errors at least for me):

  1. code: req.body.code, the code supplied is not in req.body, instead it is given through req.query. It is also important to note that after successful login it redirects to the GET URL, not POST.

  2. oauthResponseData = oauthResponse.data, but oauthResponse does not have a child data. Instead, just use oauthResponse. You can then use id_token from oauthResponse later.

Despite these, I found the article very straightforward. It was very easy to use the method to create a login link and get user details, and also sticks with the idea of being dependent-free. Thanks for this one!