<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Implement Google Login in Your Web App]]></title><description><![CDATA[Implement Google Login in Your Web App]]></description><link>https://google-sso-integration-web-app.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 28 Nov 2026 20:33:32 GMT</lastBuildDate><atom:link href="https://google-sso-integration-web-app.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Add Google Single Sign on (SSO) in your next web application. Yes, that "Sign in with Google" button.]]></title><description><![CDATA[Learn how to implement Google Single Sign-On (SSO) in your web app using OAuth 2.0. Step-by-step guide with code, setup instructions, and security tips.]]></description><link>https://google-sso-integration-web-app.hashnode.dev/add-google-single-sign-on-sso-in-your-next-web-application-yes-that-sign-in-with-google-button</link><guid isPermaLink="true">https://google-sso-integration-web-app.hashnode.dev/add-google-single-sign-on-sso-in-your-next-web-application-yes-that-sign-in-with-google-button</guid><category><![CDATA[google cloud console]]></category><category><![CDATA[Single Sign-On (SSO)]]></category><category><![CDATA[single sign on]]></category><category><![CDATA[Google]]></category><category><![CDATA[google cloud]]></category><category><![CDATA[SSO]]></category><category><![CDATA[OAuth2]]></category><category><![CDATA[Security]]></category><category><![CDATA[passwords]]></category><dc:creator><![CDATA[Akshay Vishwakarma]]></dc:creator><pubDate>Wed, 06 Aug 2025 02:33:04 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1754444844785/47c724a3-f86c-4fe4-b50a-26cbbbd7f436.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you've ever used the “Sign in with Google” button to log into apps like YouTube or Spotify you’ve already used <strong>Google Single Sign-On (SSO)</strong>.</p>
<p>In this blog, I’ll walk you through how to implement <strong>Google OAuth 2.0 login</strong> in your own web application using <strong>Node.js</strong> and <strong>React</strong>.</p>
<hr />
<h2 id="heading-what-youll-learn">What You'll Learn</h2>
<ul>
<li><p>What OAuth 2.0 and SSO is</p>
</li>
<li><p>How to set up Google Cloud Console (Developer Hub)</p>
</li>
<li><p>Backend setup (Node.js + Express)</p>
</li>
<li><p>Frontend setup (React)</p>
</li>
<li><p>Handling tokens securely</p>
</li>
</ul>
<hr />
<h2 id="heading-what-is-oauth-20">What is OAuth 2.0?</h2>
<p><strong>OAuth (Open Authorization)</strong> is a secure way to allow third party applications to access the user data without sharing passwords.</p>
<p>Why do we need OAuth?<br />- Prevent apps from storing passwords<br />- Limits what an app can do with your data<br />- Supports secure single sign on</p>
<h2 id="heading-what-is-sso">What is SSO?</h2>
<p><strong>Single Sign-On (SSO)</strong> allows users to log into multiple apps with one set of credentials—like your Google account.</p>
<p>It’s built on <strong>OAuth 2.0</strong>, a secure authorization framework where:</p>
<ul>
<li><p>Users authenticate via Google</p>
</li>
<li><p>Google sends your app a token</p>
</li>
<li><p>You use that token to trust the login</p>
</li>
</ul>
<p>This improves security, user experience and no more password headaches.</p>
<hr />
<h2 id="heading-step-1-set-up-google-oauth-credentials">Step 1: Set Up Google OAuth Credentials</h2>
<ol>
<li><p>Go to <a target="_blank" href="https://console.cloud.google.com/">Google Cloud Console</a></p>
</li>
<li><p>Create a new <strong>project</strong></p>
</li>
<li><p>Navigate to:<br /> <code>APIs &amp; Services</code> → <code>OAuth Consent Screen</code><br /> → Choose <strong>External</strong>, fill details</p>
</li>
<li><p>Go to <code>Credentials</code> → <strong>Create Credentials</strong> → <strong>OAuth Client ID</strong></p>
</li>
<li><p>Select:</p>
<ul>
<li><p><strong>Application Type:</strong> Web</p>
</li>
<li><p><strong>Authorized JavaScript origins:</strong><br />  <a target="_blank" href="http://localhost:3000"><code>http://localhost:3000</code></a></p>
</li>
<li><p><strong>Authorized redirect URIs:</strong><br />  <a target="_blank" href="http://localhost:5000/auth/google/callback"><code>http://localhost:5000/auth/google/callback</code></a></p>
</li>
</ul>
</li>
<li><p>Copy the Client ID and Client Secret</p>
</li>
</ol>
<hr />
<h2 id="heading-okay-what-happens-when-you-click-on-sign-in-with-google">Okay! What Happens When You Click on “Sign in with Google”?</h2>
<p>So, what <em>actually</em> happens behind the scenes when a user clicks on <strong>“Sign in with Google”</strong> button?</p>
<h3 id="heading-1-redirect-to-google-authorization-endpoint">1. Redirect to Google Authorization Endpoint</h3>
<p>When a user clicks the button, your app redirects them to a special <strong>Google authorization URL</strong> like:<br /><a target="_blank" href="https://accounts.google.com/o/oauth2/v2/auth"><code>https://accounts.google.com/o/oauth2/v2/auth</code></a><code>? client_id=</code><a target="_blank" href="http://123456.apps.googleusercontent.com"><code>123456.apps.googleusercontent.com</code></a> <code>&amp;redirect_uri=</code><a target="_blank" href="http://localhost:5000/auth/google/callback"><code>http://localhost:5000/auth/google/callback</code></a> <code>&amp;response_type=code &amp;scope=email profile &amp;state=RANDOM_UNIQUE_STRING</code></p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Parameter</td><td>Purpose</td></tr>
</thead>
<tbody>
<tr>
<td><code>client_id</code></td><td>Your app's public ID from Google Cloud Console</td></tr>
<tr>
<td><code>redirect_uri</code></td><td>Where Google should send the user after auth (must match your console settings)</td></tr>
<tr>
<td><code>response_type=code</code></td><td>You're requesting an <strong>authorization code</strong></td></tr>
<tr>
<td><code>scope=email profile</code></td><td>Permissions you're requesting</td></tr>
<tr>
<td><code>state=xyz123abc</code></td><td>A random string to prevent CSRF attacks</td></tr>
</tbody>
</table>
</div><h3 id="heading-2-if-everything-is-in-right-place-then-google-prompts-us-with-these">2. If Everything is in right place then Google Prompts us with these</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1754446507343/47a9103d-6d36-4b05-a8c8-d0715603d652.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-ia"> </h3>
<p>3. User Grants Permission → Google Redirects Back</p>
<p>If the user clicks “Allow”, Google redirects the user back to your app’s <code>redirect_uri</code> with an <strong>authorization code</strong>:<br /><code>http://localhost:5000/auth/google/callback?code=abc123xyz&amp;state=xyz123abc</code><br />Notice that it returns the code to us, this code is used to get the tokens (exchange tokens)<br />This <code>code</code> is short-lived (about 10 minutes) and can only be used <strong>once</strong>.</p>
<h3 id="heading-4-exchange-code-for-access-token-server-side">4. Exchange Code for Access Token (Server-Side)</h3>
<p>Now your backend server (not the browser) makes a secure POST request to Google’s token endpoint:<br />POST <code>https://oauth2.googleapis.com/token</code></p>
<p>With a payload like:<br /><code>{   "code": "abc123xyz",   "client_id": "</code><a target="_blank" href="http://123456.apps.googleusercontent.com"><code>123456.apps.googleusercontent.com</code></a><code>",   "client_secret": "YOUR_CLIENT_SECRET",   "redirect_uri": "</code><a target="_blank" href="http://localhost:5000/auth/google/callback"><code>http://localhost:5000/auth/google/callback</code></a><code>",   "grant_type": "authorization_code"   }</code></p>
<p>If all goes well, Google responds us with tokens🥳:<br /><code>{   "access_token": "ya29.a0Af...",   "expires_in": 3599,   "refresh_token": "1//0g...",   "scope": "email profile",   "token_type": "Bearer",   "id_token": "eyJhbGciOi..."   }</code></p>
<h3 id="heading-ia-1"> </h3>
<p>5. Use Access Token to Call Google APIs</p>
<p>You can now use this <code>access_token</code> to fetch user details</p>
<p><code>GET https://www.googleapis.com/oauth2/v2/userinfo   Authorization: Bearer ya29.a0Af...</code>  </p>
<p>Response:<br /><code>{   "id": "1234567890",   "email": "</code><a target="_blank" href="mailto:user@example.com"><code>user@example.com</code></a><code>",   "name": "John Doe",   "picture": "</code><a target="_blank" href="https://lh3.googleusercontent.com/"><code>https://lh3.googleusercontent.com/</code></a><code>..."   }</code>  </p>
<p>🎉 Boom! You're logged in and ready to use that user data in your app.  </p>
<h3 id="heading-ia-2"> </h3>
<p><strong>Google OAuth 2.0 Login Flow – Summary Table</strong></p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Step</td><td>Actor</td><td>Action</td><td>Description</td></tr>
</thead>
<tbody>
<tr>
<td>1</td><td><strong>User</strong></td><td>Clicks “Sign in with Google”</td><td>Initiates the login process</td></tr>
<tr>
<td>2</td><td><strong>Web App (Frontend)</strong></td><td>Redirects to Google OAuth URL</td><td>Includes <code>client_id</code>, <code>scope</code>, <code>redirect_uri</code>, <code>state</code></td></tr>
<tr>
<td>3</td><td><strong>Google Auth Server</strong></td><td>Displays consent screen</td><td>Asks the user to allow requested permissions</td></tr>
<tr>
<td>4</td><td><strong>User</strong></td><td>Grants permission</td><td>Confirms access to email, profile, etc.</td></tr>
<tr>
<td>5</td><td><strong>Google Auth Server</strong></td><td>Redirects back with auth <code>code</code></td><td>Sends temporary authorization code to your backend</td></tr>
<tr>
<td>6</td><td><strong>Web App (Backend)</strong></td><td>Exchanges <code>code</code> for <code>access_token</code></td><td>Secure server-to-server POST request using <code>client_secret</code></td></tr>
<tr>
<td>7</td><td><strong>Web App (Backend)</strong></td><td>Uses access token to call Google APIs</td><td>Retrieves user profile or email from endpoints</td></tr>
<tr>
<td>8</td><td><strong>Web App (Backend)</strong></td><td>Stores <code>refresh_token</code></td><td>Used to get new access tokens when current one expires</td></tr>
</tbody>
</table>
</div>]]></content:encoded></item></channel></rss>