<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Michael Marcoux</title>
    <description>The latest articles on DEV Community by Michael Marcoux (@mikecoux).</description>
    <link>https://dev.to/mikecoux</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1060257%2F3c2884ed-3ec3-4b37-ad70-6d006d87d1d4.jpeg</url>
      <title>DEV Community: Michael Marcoux</title>
      <link>https://dev.to/mikecoux</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mikecoux"/>
    <language>en</language>
    <item>
      <title>Data fetching with React Router</title>
      <dc:creator>Michael Marcoux</dc:creator>
      <pubDate>Tue, 02 May 2023 01:40:09 +0000</pubDate>
      <link>https://dev.to/mikecoux/data-fetching-with-react-router-2689</link>
      <guid>https://dev.to/mikecoux/data-fetching-with-react-router-2689</guid>
      <description>&lt;p&gt;This blog will cover several ways that you can fetch data without a useEffect hook using the React Router library. Following these steps will allow you to write cleaner, more intuitive code as all of your data fetching will occur in a single React Router component called a BrowserRouter.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This tutorial assumes you have a functioning React app with React Router v6.4 installed. My recommendation for beginners is to use the Vite development environment and following the  React Router tutorial &lt;a href="https://reactrouter.com/en/main/start/tutorial"&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Create your BrowserRouter
&lt;/h3&gt;

&lt;p&gt;The React Router tutorial linked above will cover the BrowserRouter component in greater depth, though I wanted to fill in the gaps that I encountered while setting up my first Vite + React Router app. &lt;em&gt;If you already have your Browser Router set up, skip this step.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After getting started with the Vite/React template, your project root directory should contain a &lt;code&gt;src&lt;/code&gt; directory with &lt;code&gt;components&lt;/code&gt; and &lt;code&gt;routes&lt;/code&gt; directories listed inside of it. Inside of the &lt;code&gt;src&lt;/code&gt; directory, you should have a &lt;code&gt;main.jsx&lt;/code&gt; file. This file represents your parent JSX file and is the only JSX file referenced in your HTML document.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;your_project/src/main.jsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the top of &lt;code&gt;main.jsx&lt;/code&gt; import the necessary components from React Router.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
import ReactDOM from 'react-dom/client'
import { createBrowserRouter, RouterProvider } from 'react-router-dom'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After all of your imports, add the BrowserRouter component using the following structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const router = createBrowserRouter([
  {
    element: &amp;lt;Root /&amp;gt;,
    id: "root",
    children: [
        {
          path: '/',
          element: &amp;lt;Home /&amp;gt;
        },
        {
          path: '/*your_route*',
          element: &amp;lt;*Your_Route_Component* /&amp;gt;
        },
        ...
    ]
  }
]}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, add the following to the bottom of your &lt;code&gt;main.jsx&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReactDOM.createRoot(document.getElementById('root')).render(
  &amp;lt;RouterProvider router={router} /&amp;gt;
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;See these &lt;a href="https://reactrouter.com/en/main/routers/create-browser-router"&gt;React Router docs&lt;/a&gt; for more support on this step.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Using the 'loader' API
&lt;/h3&gt;

&lt;p&gt;At this point, it is best to think about which routes you will need to send data to. In this example, I will go over using the &lt;code&gt;loader&lt;/code&gt; API to fetch data for the Root component for global use across my application (similar to passing state via React context) as well as fetching data on an individual route level.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adding a loader to the Root component:&lt;/strong&gt;&lt;br&gt;
As mentioned above, this technique is especially useful if there is data that you will need in multiple components across your web application. &lt;/p&gt;

&lt;p&gt;Going back to your &lt;code&gt;router&lt;/code&gt; component in the &lt;code&gt;main.jsx&lt;/code&gt; file, add the following under the &lt;code&gt;id&lt;/code&gt; key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;loader: async () =&amp;gt; {
  return fetch(`your_fetch_url`);
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your component should now look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const router = createBrowserRouter([
  {
    element: &amp;lt;Root /&amp;gt;,
    id: "root",
    loader: async () =&amp;gt; {
      return fetch(`your_fetch_url`);
    },
    children: [
    ...
    ]
  }
]}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sweet! Now your data is available in any child component when you import it with reference to its id: &lt;code&gt;root&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adding a loader to a child component&lt;/strong&gt;&lt;br&gt;
If you want to fetch data for a specific child component, the syntax is the same, but you will not need to include a value for &lt;code&gt;id&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
{
  path: '/*your_route*',
  element: &amp;lt;*Your_Route_Component* /&amp;gt;
  loader: async () =&amp;gt; {
      return fetch(`your_fetch_url`);
  },
},
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Importing the fetched data
&lt;/h3&gt;

&lt;p&gt;Now that you've set up your BrowserRouter to fetch data, you can navigate to your various components and import it. In order to import the data you fetched at your 'root' level, import the following at the top of your component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
import ReactDOM from 'react-dom/client'
import { useRouteLoaderData } from "react-router-dom";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, create a variable using the imported hook:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function YourComponent () {
  const your_data = useRouteLoaderData("root")
  ....
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how the useRouteLoader data hook takes a route &lt;code&gt;id&lt;/code&gt; as a parameter. You can find more information about this hook &lt;a href="https://reactrouter.com/en/main/hooks/use-route-loader-data"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Importing data that you fetched directly to a component is similar, except you will use the useLoaderData hook instead and not specify a route &lt;code&gt;id&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useLoaderData } from "react-router-dom"

export default function YourComponent () {
 const your_data = useLoaderData()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Amazing! You've now fetched your data for various components without a single useEffect. Your code is cleaner, and you can easily see which data is going to each of your routes in the BrowserRouter component.&lt;/p&gt;

</description>
      <category>react</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>api</category>
    </item>
    <item>
      <title>Adding 'Sign in with Google' to your site with vanilla JS</title>
      <dc:creator>Michael Marcoux</dc:creator>
      <pubDate>Thu, 06 Apr 2023 20:11:51 +0000</pubDate>
      <link>https://dev.to/mikecoux/adding-sign-in-with-google-to-your-site-with-vanilla-js-g2e</link>
      <guid>https://dev.to/mikecoux/adding-sign-in-with-google-to-your-site-with-vanilla-js-g2e</guid>
      <description>&lt;p&gt;This blog is going to be a step-by-step guide on how to quickly add a 'Sign in with Google' button to your website. You won't need to install any modules for this to work. At the end, you'll be able to sign in with your Google account and use the data returned from the Google OAuth API to run conditional JS functions on your website.&lt;/p&gt;

&lt;p&gt;I decided to put this together because I believe that a Google login is slicker and more functional than a bootstrapped HTML form, but I couldn't find a step-by-step resource oriented toward beginners. Please comment below your thoughts and any questions/issues you have while following these instructions. Happy coding!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Please note that this guide will feature macOS/Linux terminal commands. If you are running Windows, you will have to research the corresponding command prompt syntax.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A HTML &amp;amp; JS file open in your code editor of choice&lt;/li&gt;
&lt;li&gt;A Google account&lt;/li&gt;
&lt;li&gt;Python installed (&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Tools_and_setup/set_up_a_local_testing_server" rel="noopener noreferrer"&gt;installation guide&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Links you'll need:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://console.cloud.google.com/apis/credentials" rel="noopener noreferrer"&gt;Google credentials API dashboard&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developers.google.com/identity/gsi/web/guides/get-google-api-clientid" rel="noopener noreferrer"&gt;Google authentication docs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 1: Get your site running on a localhost server
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;This step with require the use of a Python command. If you do not have Python installed, follow the guide provided under 'Prerequisites'.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Navigate to the working directory in which your website lives. Make sure you have created HTML &amp;amp; JS files there. Your HTML file should contain at least one line of code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8hb9tp93f4c6mftlhyv5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8hb9tp93f4c6mftlhyv5.png" alt="working directory"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Run the following Python command in your terminal.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

python3 -m http.server


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You should see the following output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk891a9l8ib2fwydfrafb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk891a9l8ib2fwydfrafb.png" alt="terminal output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Navigate to &lt;a href="http://localhost:8000" rel="noopener noreferrer"&gt;http://localhost:8000&lt;/a&gt; in your browser. You should see your site! &lt;em&gt;When you are done working, you can use 'Ctrl + c' in the same terminal to terminate the server.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Having issues?&lt;/strong&gt; If you are getting a different output, try the following steps.&lt;/p&gt;

&lt;p&gt;Verify you have Python installed by running this command in a new terminal.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

python3 -v


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you see a version number in the output, go ahead and terminate that terminal and check to see which ports are open with this command in a new terminal.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

lsof -i -P | grep -i "listen"


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the image below, you can see that Python is running on port :8000 with the portID 30098.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqnjurpxhy0fqrmz2hbfa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqnjurpxhy0fqrmz2hbfa.png" alt="terminal output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If something else is running on port 8000, use the following command to terminate the port. Use the value found in the previous command for 'portID'.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

kill portID


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 2: Create a Google Cloud project
&lt;/h2&gt;

&lt;p&gt;Navigate to the Credentials API Dashboard and click to create a new project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7rg2b0dzql8yukk4kboe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7rg2b0dzql8yukk4kboe.png" alt="Creating a project in Google Cloud"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you have followed all of the prompts, navigate back to the dashboard if you are not automatically directed there.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 3: Create your OAuth consent screen
&lt;/h2&gt;

&lt;p&gt;Click 'OAuth consent screen' in the left-side navigation and enter an app name, user support email, and developer contact information. The rest of the fields can be left blank.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5igl8mwwa3q45aw9ver.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5igl8mwwa3q45aw9ver.png" alt="Creating OAuth consent screen"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1yjqyfbaoh9fihkhash8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1yjqyfbaoh9fihkhash8.png" alt="Creating OAuth consent screen"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Continue clicking 'SAVE AND CONTINUE' until you are at the summary page and then navigate back to the dashboard. You do not need to fill out the 'Scope' or 'Optional Information' prompts.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 4: Create your OAuth 2.0 Client ID
&lt;/h2&gt;

&lt;p&gt;Click 'Credentials' in the left-side navigation, click 'CREATE CREDENTIALS' at the top of the page, and then click 'OAuth client ID'.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0o7qtvngv55k4tngglbc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0o7qtvngv55k4tngglbc.png" alt="Creating client ID"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add a client name, and then add &lt;a href="http://localhost:8000" rel="noopener noreferrer"&gt;http://localhost:8000&lt;/a&gt; and &lt;a href="http://localhost" rel="noopener noreferrer"&gt;http://localhost&lt;/a&gt; under both the 'Authorized JavaScript origins' and 'Authorized redirect URIs' sections. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo8xp5iiyqkwam9fc373m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo8xp5iiyqkwam9fc373m.png" alt="Configuring client ID"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note that this screen is where you can find your 'Client ID'.&lt;/strong&gt; Click 'SAVE' and navigate back to the dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgoqtwaaho5l4ei7xo54n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgoqtwaaho5l4ei7xo54n.png" alt="Client ID"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If anything from steps 3 &amp;amp; 4 was confusing, checking out the &lt;a href="https://developers.google.com/identity/gsi/web/guides/get-google-api-clientid" rel="noopener noreferrer"&gt;Google authentication docs&lt;/a&gt; may be helpful.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 5: Configure your HTML headers
&lt;/h2&gt;

&lt;p&gt;Per the docs linked above, you'll need to add two headers to your HTML document so that your login works with a localhost server. &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;meta name="referrer" content="no-referrer-when-downgrade"&amp;gt;
&amp;lt;meta name="Cross-Origin-Opener-Policy" content="same-origin-allow-popups"&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;While we're here, check that your JS document is linked to your HTML document. If you choose to link it in the header as I have done, it is best practice to include the &lt;code&gt;defer&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq2vy9otr37wfhv1rqmdm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq2vy9otr37wfhv1rqmdm.png" alt="HTML header"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6: Generate your button code
&lt;/h2&gt;

&lt;p&gt;Follow this link to the &lt;a href="https://developers.google.com/identity/gsi/web/tools/configurator" rel="noopener noreferrer"&gt;HTML code generator&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Add your Client ID, choose 'Swap to JavaScript callback', and then enter a name for your callback function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6k9d5n3i31cpv6qrrp69.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6k9d5n3i31cpv6qrrp69.png" alt="Code configuration"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In this example, we have to use a JS callback rather than a Login URI because we are using a localhost server.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Toggle the 'Enable Sign in with Google button', and make sure 'In a popup' is selected under the 'Behavior' dropdown.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flyift6r1x5pj5pfplx06.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flyift6r1x5pj5pfplx06.png" alt="Code configuration"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Edit the styling options as you wish, and then click 'Get code'. Copy the code output to your clipboard.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In this example, we have not enabled OneTap as that requires additional configuration not covered in this blog.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 7: Add the button to your HTML
&lt;/h2&gt;

&lt;p&gt;Paste the code in your HTML document nested somewhere between your &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; tags. I recommend creating a parent &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; around the login button code for styling purposes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe28ihqhq6tt3eilnul1b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe28ihqhq6tt3eilnul1b.png" alt="Resulting HTML code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Hint: You can use Cmd + k then Cmd + f on a selection in VS Code to automatically format it.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 8: Add OAuth scripts to the bottom of your HTML doc
&lt;/h2&gt;

&lt;p&gt;Add the following scripts to the bottom of the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; section in your HTML document. The first script loads the Google client library and the second is the callback function that will handle the API response.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;script src="https://accounts.google.com/gsi/client" async defer&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script&amp;gt;
    function handleCredentialResponse(response) {
        decodeJwtResponse(response.credential);
    }
&amp;lt;/script&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the callback function you'll notice I invoke another function called &lt;code&gt;decodeJwtResponse&lt;/code&gt;. This function parses the JSON web token (JWT) that is returned from the Google API upon sign in. This JWT contains account information from the Google user that signed in via your button. Once parsed, you can store this information in different variables to supplement functionality on your webpage.&lt;/p&gt;

&lt;p&gt;If this step is confusing, check out Google's documentation on &lt;a href="https://developers.google.com/identity/gsi/web/guides/client-library" rel="noopener noreferrer"&gt;loading the client library&lt;/a&gt; and &lt;a href="https://developers.google.com/identity/gsi/web/guides/handle-credential-responses-js-functions" rel="noopener noreferrer"&gt;handling the credential response&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 9: Parsing the JWT in your JS file
&lt;/h2&gt;

&lt;p&gt;Parsing the JSON web token (JWT) can be accomplished several ways. In this example, I'm going to use a &lt;a href="https://stackoverflow.com/questions/38552003/how-to-decode-jwt-token-in-javascript-without-using-a-library" rel="noopener noreferrer"&gt;solution&lt;/a&gt; from Stack Overflow that doesn't require a library. If you would like to experiment with libraries, check them out &lt;a href="https://jwt.io/libraries" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Navigate to your JS file and add the following function.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

function parseJwt (token) {
    var base64Url = token.split('.')[1];
    var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
    var jsonPayload = decodeURIComponent(window.atob(base64).split('').map(function(c) {
        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
    }).join(''));

    return JSON.parse(jsonPayload);
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now pass it the JWT by adding a second function.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

function decodeJwtResponse(data){
    signIn(parseJwt(data))
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This second function is invoked in the &lt;code&gt;handleCredentialResponse&lt;/code&gt; callback and is where you can invoke other functionality related to signing in for your website. In the above example, I chose to invoke a function called &lt;code&gt;signIn&lt;/code&gt; that takes the parsed JWT from the &lt;code&gt;parseJWT&lt;/code&gt; function. If you would like to simply see the parsed JWT in your console log, replace &lt;code&gt;signIn(parseJwt(data))&lt;/code&gt; with &lt;code&gt;console.log(parseJwt(data))&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;That's all there is to this blog! Thank you for following along. Feel free to comment below with your thoughts/questions.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>javascript</category>
      <category>googlecloud</category>
    </item>
  </channel>
</rss>
