<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jack Scott on Medium]]></title>
        <description><![CDATA[Stories by Jack Scott on Medium]]></description>
        <link>https://medium.com/@jackrobertscott?source=rss-f4316fefd8c4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*uRyXUtlTVIwg3z96fmO1fA.png</url>
            <title>Stories by Jack Scott on Medium</title>
            <link>https://medium.com/@jackrobertscott?source=rss-f4316fefd8c4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 01:30:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jackrobertscott/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Avoid TS-Node, Use ESBuild Instead]]></title>
            <link>https://medium.com/@jackrobertscott/use-esbuild-not-ts-node-6f763ad9e98a?source=rss-f4316fefd8c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/6f763ad9e98a</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[esbuild]]></category>
            <dc:creator><![CDATA[Jack Scott]]></dc:creator>
            <pubDate>Fri, 14 Jun 2024 04:02:06 GMT</pubDate>
            <atom:updated>2024-06-22T12:29:56.762Z</atom:updated>
            <content:encoded><![CDATA[<p>Are you tired of waiting for your TypeScript projects to start up? Do you dread the thought of using ts-node for your development environment? Fear not, my friend! There&#39;s a better way: esbuild! 🎉</p><h3>The Pitfalls of ts-node 😩</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OMaFs9dpMUIcQLQbZNyDKQ.png" /></figure><p>While ts-node has been a popular choice for running TypeScript code directly, it comes with some major drawbacks:</p><ol><li><strong>ESM Support</strong>: ts-node fails to implement ESM (ECMAScript Modules) on versions of Node.js older than version 18. This means you&#39;re stuck with the old CommonJS syntax and missing out on the benefits of modern JavaScript. 😞</li><li><strong>Slow Startup Times</strong>: Starting a ts-node program is like watching paint dry. It&#39;s much slower compared to the time it takes to completely rebuild your project using esbuild and start the Node.js version of the program. ⏰</li></ol><h3>Embrace the Power of esbuild 💪</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z-FMLSLGn3E2vEA4C9Xvzg.jpeg" /><figcaption>esbuild</figcaption></figure><p>esbuild is here to save the day! It&#39;s a fast and efficient bundler that supports ESM out of the box. Here&#39;s why you should switch to esbuild:</p><ol><li><strong>ESM Support</strong>: With esbuild, you can use all the modern JavaScript packages and enjoy the cool benefits of ESM over CommonJS. Say goodbye to require and hello to import! 🙌</li><li><strong>Lightning-Fast Builds</strong>: esbuild is known for its blazing-fast build times. It can completely rebuild your project in a fraction of the time it takes ts-node to start up. Get ready to be amazed! ⚡</li></ol><h3>Setting Up a New Repo with esbuild and nodemon 🛠️</h3><p>Let’s create a new repository and set up esbuild with nodemon for a seamless development experience:</p><ol><li>Create a new directory for your project and navigate to it in your terminal.</li><li>Initialize a new Node.js project by running npm init -y.</li><li>Install the necessary dependencies:</li></ol><pre>npm install --save-dev esbuild nodemon</pre><p>4. Create an esbuild.config.js file in the root of your project with the following content:</p><pre>import { build, context } from &#39;esbuild&#39;;<br>import { readFileSync } from &#39;fs&#39;;<br>import { resolve } from &#39;path&#39;;<br><br>const isWatching = !!process.argv.includes(&#39;--watch&#39;)<br>const nodePackage = JSON.parse(readFileSync(resolve(process.cwd(), &#39;package.json&#39;), &#39;utf8&#39;));<br><br>const buildOptions = {<br>  entryPoints: [resolve(process.cwd(), &#39;src&#39;, &#39;index.server.ts&#39;)],<br>  outfile: resolve(process.cwd(), &#39;dist&#39;, &quot;server&quot;, &#39;index.server.js&#39;),<br>  bundle: true,<br>  platform: &#39;node&#39;,<br>  format: &#39;esm&#39;,<br>  external: [<br>    Object.keys(nodePackage.dependencies ?? {}),<br>    Object.keys(nodePackage.peerDependencies ?? {}),<br>    Object.keys(nodePackage.devDependencies ?? {}),<br>  ].flat(),<br>};<br><br>if (isWatching) {<br>  context(buildOptions).then(ctx =&gt; {<br>    if (isWatching) {<br>      ctx.watch();<br>    } else {<br>      ctx.rebuild();<br>    }<br>  });<br>} else {<br>  build(buildOptions)<br>}<br><br></pre><p>This configuration file tells esbuild how to build your project and enables watching for changes.</p><p>5. Create a src/index.ts file which will be the entry point of your TypeScript node program.</p><p>6. Update your package.json file with the following scripts:</p><pre>&quot;scripts&quot;: {<br>  &quot;build&quot;: &quot;node esbuild.config.js&quot;,<br>  &quot;start&quot;: &quot;node dist/index.js&quot;,<br>  &quot;dev&quot;: &quot;node esbuild.config.js --watch &amp; nodemon dist/index.js&quot;<br>}</pre><p>These scripts allow you to build, start, and watch your project with ease.</p><p>7. Run npm run dev to start the development server and watch for changes. 🎊</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_V2Fh15mVfF0g60giXWqaA.png" /></figure><p>Congratulations! You now have a blazing-fast TypeScript project set up with esbuild and nodemon. Enjoy the smooth development experience and lightning-fast startup times! 😎</p><p>Remember, esbuild is your friend when it comes to building and bundling TypeScript projects. Say goodbye to the limitations of ts-node and embrace the power of esbuild! 🚀</p><p>Happy coding! 💻✨</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6f763ad9e98a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Easy Facebook Auth with Node.js]]></title>
            <link>https://medium.com/@jackrobertscott/facebook-auth-with-node-js-c4bb90d03fc0?source=rss-f4316fefd8c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/c4bb90d03fc0</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[oauth]]></category>
            <category><![CDATA[facebook]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[authentication]]></category>
            <dc:creator><![CDATA[Jack Scott]]></dc:creator>
            <pubDate>Wed, 11 Dec 2019 12:24:45 GMT</pubDate>
            <atom:updated>2022-05-17T05:12:00.326Z</atom:updated>
            <content:encoded><![CDATA[<p>5 Steps to install Facebook user login with Node.js</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7VbJtNQQlRPMNySt_mPDAQ.png" /><figcaption>Facebook Login</figcaption></figure><p>So you want to add Facebook login to your app or website, hey? Maybe you’ve had problems with <a href="http://www.passportjs.org/">Passport.js</a> and want a more straight forward solution. Or maybe you’re just here for fun…</p><p>Well this is the article for you!</p><p>This article contains a step-by-step tutorial on how to code a Facebook login on your app using <a href="https://oauth.net/2/">OAuth 2</a>. I’ve broken the process as best I could to help you understand exactly what’s going on.</p><h3>Before We Start ☝️</h3><p>In this article, we are assuming you:</p><ul><li>Already know how to write code — preferably JavaScript.</li></ul><p>Yep, that’s it. If you don’t know how to code or want a quicker way to add OAuth logins to your app or website, check out <a href="https://authpack.io/?ref=medium-facebook-oauth-nodejs-1">Authpack</a>.</p><h3>Step 1 of 5 🔒</h3><p>To start, you’ll need to register your app on Facebook.</p><ul><li>Go to the Facebook App page: <a href="https://developers.facebook.com/apps/">https://developers.facebook.com/apps</a></li><li>Add a new app and call it what ever you want to.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0KyZOcVbvuPLqRqLjTHLqg.png" /><figcaption><a href="https://developers.facebook.com/apps/">developers.facebook.com</a></figcaption></figure><p>Now that your app is created, we will need to get it’s ID value which we will use later on in the code.</p><ul><li>Navigate to the app’s basic settings page — it’s in the left sidebar and is shown in the photo bellow.</li><li>Copy your App ID.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y02NrCN7P944SUxYDmD4bA.png" /><figcaption><a href="https://developers.facebook.com/apps/">developers.facebook.com</a></figcaption></figure><h3>Step 2 of 5 🔌</h3><p>When a user is logged in with Facebook, they will then be redirected back to your app. To do this, we need to give Facebook the url that points to your app. You may need to create this app route if you haven’t already.</p><p>You can also test Facebook login on your localhost servers. Facebook automatically approves all localhost addresses while the app is in development mode, so you don’t have to add the url if it’s on localhost.</p><ul><li>Create a route in your app such as: <a href="https://www.example.com/authenticate/facebook/">https://www.example.com/authenticate/facebook/</a></li><li>Facebook will redirect users to this route after they login.</li><li>Add this route to your Facebook App’s settings under Valid OAuth Redirect URIs.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*knri1yMe-FA46nNsjKi4sA.png" /><figcaption><a href="https://developers.facebook.com/apps/">developers.facebook.com</a></figcaption></figure><h3>Step 3 of 5 👉</h3><p>Now it’s time for the good bit, we are going to start logging in your users with Facebook. All we need to do here is append a few parameters to the Facebook login url. When our users click a button or link, we will send them to this url. To make things easier, we are going to use the simple query-string library to append parameters to this url.</p><ul><li>Install the popular query-string library on your app: <a href="https://www.npmjs.com/package/query-string">https://www.npmjs.com/package/query-string</a></li><li>Create the Facebook login url.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c5507f2e9d16beb7c88ccc49a6f7f993/href">https://medium.com/media/c5507f2e9d16beb7c88ccc49a6f7f993/href</a></iframe><ul><li>Make sure your scopes match up to the data that you wish to access later on in your code. For example, you will need to add the “email” scope to access the user’s email address.</li><li>You can see all the url parameter options here: <a href="https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow#login">https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow#login</a></li></ul><p>Now that you’ve created the login url, add it to your app or website. Here is a simple React example.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10a61ae42e3196808fe76fe67d32261b/href">https://medium.com/media/10a61ae42e3196808fe76fe67d32261b/href</a></iframe><h3>Step 4 of 5 🔐</h3><p>As mentioned before, once your users login, they will be redirected back to your app. The url they are redirect to will contain a special <strong>code</strong>. For example:</p><pre>https://www.example.com/authenticate/facebook?code=CODE_IS_HERE</pre><p>We will use to create an access token. An access token is required to authenticate any future requests we send to Facebook such as getting the user’s name or email address. To get the code and create the access token, we will use the same query-string library that we used before. Let’s go!</p><ul><li>Get the code from the url.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0aad3b078d47873ab2334d3beec519ca/href">https://medium.com/media/0aad3b078d47873ab2334d3beec519ca/href</a></iframe><ul><li>Send the code to your server to be processed — not applicable when already on the server i.e. when using express.</li><li>Create the access token from your code.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c9c530e71558a90f8361c4a1705319f/href">https://medium.com/media/8c9c530e71558a90f8361c4a1705319f/href</a></iframe><ul><li>See url parameter options here: <a href="https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow#confirm">https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow#confirm</a></li><li>Remember to store secret variables in process.env which is easy to do with the dotenv library: <a href="https://www.npmjs.com/package/dotenv">https://www.npmjs.com/package/dotenv</a></li></ul><p><strong>Troubleshooting:</strong> should Facebook say your redirect URI is incorrect, put a “/” (slash) at the end or the redirect URI.</p><h3>Step 5 of 5 👩‍💻</h3><p>Now that you got the access token, we can use it to get data from the Facebook API. We’ve gone ahead and created an example request which you can use to get some basic user details.</p><ul><li>Create a HTTP request with the access token to get the user’s data.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5836f3ff11e77f3ed9b76c62929d1110/href">https://medium.com/media/5836f3ff11e77f3ed9b76c62929d1110/href</a></iframe><ul><li>See the Facebook API Explorer here: <a href="https://developers.facebook.com/tools/explorer">https://developers.facebook.com/tools/explorer</a></li></ul><h3>Yahooo! 🎉</h3><p>If you enjoyed this article, please give it a <strong>few claps </strong>— <em>you can leave up to 50</em> — or you can comment if you have any questions, I’ll do my best to answer them!</p><p>Liked this tutorial? ❤️ Here are some <strong>more:</strong></p><ul><li><a href="https://medium.com/authpack/easy-google-auth-with-node-js-99ac40b97f4c">Easy Google Auth with Node.js</a></li><li><a href="https://medium.com/authpack/easy-github-auth-with-node-js-502d3d8f8e62">Easy GitHub Auth with Node.js</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c4bb90d03fc0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Easy GitHub Auth with Node.js]]></title>
            <link>https://medium.com/@jackrobertscott/easy-github-auth-with-node-js-502d3d8f8e62?source=rss-f4316fefd8c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/502d3d8f8e62</guid>
            <category><![CDATA[github]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[oauth2]]></category>
            <category><![CDATA[oauth]]></category>
            <dc:creator><![CDATA[Jack Scott]]></dc:creator>
            <pubDate>Wed, 11 Dec 2019 12:21:22 GMT</pubDate>
            <atom:updated>2022-05-17T05:12:45.627Z</atom:updated>
            <content:encoded><![CDATA[<p>5 Steps to install GitHub user login with Node.js</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u-JAb6KdBOIly-KSPIH_qw.png" /></figure><p>So you want to add GitHub login to your app or website, hey? Maybe you’ve had problems with <a href="http://www.passportjs.org/">Passport.js</a> and want a more straight forward solution. Or maybe you’re just here for fun…</p><p>Well this is the article for you!</p><p>This article contains a step-by-step tutorial on how to code a GitHub login on your app using <a href="https://oauth.net/2/">OAuth 2</a>. I’ve broken the process as best I could to help you understand exactly what’s going on.</p><h3>Before We Start ☝️</h3><p>In this article, we are assuming you:</p><ul><li>Already know how to write code — preferably JavaScript.</li></ul><p>Yep, that’s it. If you don’t know how to code or want a quicker way to add OAuth logins to your app or website, check out <a href="https://authpack.io/?ref=medium-github-oauth-nodejs-1">Authpack</a>.</p><h3>Step 1 of 5 🔒</h3><p>To start, you’ll need to register your app on GitHub</p><ul><li>Go to the OAuth Apps section of the GitHub developer settings: <a href="https://github.com/settings/developers">https://github.com/settings/developers</a></li><li>Click the New OAuth App button and create an app.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5kbgF2R7kYjmurHb_pdwOw.png" /></figure><ul><li>Copy the Client ID and Client Secret as we will use them later.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xBC8WMfG7kEHAwpactLzlw.png" /><figcaption><a href="https://github.com/settings/developers">github.com/settings/developers</a></figcaption></figure><h3>Step 2 of 5 🔌</h3><p>When a user is logged in with GitHub, they will then be redirected back to your app. To do this, we need to give GitHub the url that points to your app. You may need to create this app route if you haven’t already.</p><p>You can also test GitHub login on localhost, just provide the localhost address instead of the domain address.</p><ul><li>Create a route in your app such as <a href="https://www.example.com/authenticate/github">https://www.example.com/authenticate/github</a></li><li>GitHub will redirect users to this route after they login.</li><li>Add the route to your GitHub App’s settings under Authorization callback URL.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/933/1*ZuIOlbV5DctOoxI8y_h2Dw.png" /></figure><h3>Step 3 of 5 👉</h3><p>Now it’s time for the good bit, we are going to start logging in your users with GitHub. All we need to do here is append a few parameters to the GitHub login url. When our users click a button or link, we will send them to this url. To make things easier, we are going to use the simple query-string library to append parameters to this url.</p><ul><li>Install the popular query-string library on your app: <a href="https://www.npmjs.com/package/query-string">https://www.npmjs.com/package/query-string</a></li><li>Create the GitHub login url.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e00dcf029e3dd45242f2870f2262595/href">https://medium.com/media/9e00dcf029e3dd45242f2870f2262595/href</a></iframe><ul><li>Make sure your scopes match up to the data that you wish to access later on in your code. For example, you will need to add the “user:email” scope to access the user’s email address.</li><li>See url parameter options here: <a href="https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/#1-request-a-users-github-identity">https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/#1-request-a-users-github-identity</a></li><li>See scope options here: <a href="https://developer.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/">https://developer.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/</a></li></ul><p>Now that you’ve created the login url, add it to your app or website. Here is a simple React example.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51847e5ebcfb94557a7de28768d4279e/href">https://medium.com/media/51847e5ebcfb94557a7de28768d4279e/href</a></iframe><h3>Step 4 of 5 🔐</h3><p>As mentioned before, once your users login, they will be redirected back to your app. The url they are redirect to will contain a special <strong>code</strong>. For example:</p><pre><a href="https://www.example.com/authenticate/google?code=CODE_IS_HERE">https://www.example.com/authenticate/github?code=CODE_IS_HERE</a></pre><p>We will use to create an access token. An access token is required to authenticate any future requests we send to GitHub such as getting the user’s name or email address. To get the code and create the access token, we will use the same query-string library that we used before. Let’s go!</p><ul><li>Get the code from the url.</li><li>The code returned by GitHub will expire after 10 minutes.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97bc6f38d61908034519499fe423f259/href">https://medium.com/media/97bc6f38d61908034519499fe423f259/href</a></iframe><ul><li>Send the code to your server to be processed — not applicable when already on the server i.e. when using express.</li><li>Create an access token from your code.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a79bd4d069f17f3cf87b3420656a24e/href">https://medium.com/media/9a79bd4d069f17f3cf87b3420656a24e/href</a></iframe><ul><li>See url parameter options here: <a href="https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/#2-users-are-redirected-back-to-your-site-by-github">https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/#2-users-are-redirected-back-to-your-site-by-github</a></li><li>Remember to store secret variables in process.env which is easy to do with the dotenv library: <a href="https://www.npmjs.com/package/dotenv">https://www.npmjs.com/package/dotenv</a></li></ul><h3>Step 5 of 5 👩‍💻</h3><p>Now that you got the access token, we can use it to get data from the GitHub API. We’ve gone ahead and created an example request which you can use to get some basic user details.</p><ul><li>You can now use the access token to get data from the GitHub API.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab710a0088b31d33b4e7e900ea5da6da/href">https://medium.com/media/ab710a0088b31d33b4e7e900ea5da6da/href</a></iframe><ul><li>See the GitHub API options here: <a href="https://developer.github.com/v3/">https://developer.github.com/v3/</a></li></ul><h3>Yahooo! 🎉</h3><p>If you enjoyed this article, please give it a <strong>few claps </strong>— <em>you can leave up to 50</em> — or you can comment if you have any questions, I’ll do my best to answer them!</p><p>Liked this tutorial? ❤️ Here are some <strong>more:</strong></p><ul><li><a href="https://medium.com/authpack/easy-google-auth-with-node-js-99ac40b97f4c">Easy Google Auth with Node.js</a></li><li><a href="https://medium.com/authpack/facebook-auth-with-node-js-c4bb90d03fc0">Easy Facebook Auth with Node.js</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=502d3d8f8e62" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Easy Google Auth with Node.js]]></title>
            <link>https://medium.com/@jackrobertscott/easy-google-auth-with-node-js-99ac40b97f4c?source=rss-f4316fefd8c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/99ac40b97f4c</guid>
            <category><![CDATA[oauth]]></category>
            <category><![CDATA[login]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[google]]></category>
            <category><![CDATA[oauth2]]></category>
            <dc:creator><![CDATA[Jack Scott]]></dc:creator>
            <pubDate>Wed, 11 Dec 2019 12:18:05 GMT</pubDate>
            <atom:updated>2022-05-17T05:12:31.033Z</atom:updated>
            <content:encoded><![CDATA[<p>5 Steps to install Google user login with Node.js</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZBKNuev2I-9ht0jdqkF4xQ.png" /><figcaption>Google Login</figcaption></figure><p>So you want to add Google login to your app or website, hey? Maybe you’ve had problems with <a href="http://www.passportjs.org/">Passport.js</a> and want a more straight forward solution. Or maybe you’re just here for fun…</p><p>Well this is the article for you!</p><p>This article contains a step-by-step tutorial on how to code a Google login on your app using <a href="https://oauth.net/2/">OAuth 2</a>. I’ve broken the process as best I could to help you understand exactly what’s going on. This is an updated version of the original article which you can see <a href="https://medium.com/@jackrobertscott/how-to-use-google-auth-api-with-node-js-888304f7e3a0">here</a> — currently at 4K+ claps.</p><h3>Before We Start ☝️</h3><p>In this article, we are assuming you:</p><ul><li>Already know how to write code — preferably JavaScript.</li></ul><p>Yep, that’s it. If you don’t know how to code or want a quicker way to add OAuth logins to your app or website, check out <a href="https://authpack.io/?ref=medium-google-oauth-nodejs-1">Authpack</a>.</p><h3>Step 1 of 5 🔒</h3><p>To start, you’ll need to create a new Google project.</p><ul><li>Go to the Google developer console: <a href="https://console.developers.google.com/apis/credentials">https://console.developers.google.com/apis/credentials</a></li><li>Select or create a Google project.</li><li>Navigate to the credentials page — in the left sidebar — and create an OAuth client ID.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2qbRc9tI23L-cyNFw6XqIA.png" /><figcaption><a href="https://console.developers.google.com/apis/credentials">console.developers.google.com/apis/credentials</a></figcaption></figure><ul><li>Copy the client id and client secret to be used later.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vL-Ygk5MB_REkw27Uj1BTw.png" /><figcaption><a href="https://console.developers.google.com/apis/credentials">console.developers.google.com/apis/credentials</a></figcaption></figure><h3>Step 2 of 5 🔌</h3><p>When a user is logged in with Google, they will then be redirected back to your app. To do this, we need to give Google the url that points to your app. You may need to create this app route if you haven’t already.</p><p>You can also test Google login on localhost, just provide the localhost address instead of the domain address.</p><ul><li>Create a route in your app such as <a href="https://www.example.com/authenticate/google">https://www.example.com/authenticate/google</a></li><li>Google will redirect users to this route after they login.</li><li>Navigate to the OAuth consent screen panel of the credentials area.</li><li>Add your domain to the Authorized domains section.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zcP9eT4-aS2d6KduB87tMA.png" /><figcaption><a href="https://console.developers.google.com/apis/credentials">console.developers.google.com/apis/credentials</a></figcaption></figure><h3>Step 3 of 5 👉</h3><p>Now it’s time for the good bit, we are going to start logging in your users with Google. All we need to do here is append a few parameters to the Google login url. When our users click a button or link, we will send them to this url. To make things easier, we are going to use the simple query-string library to append parameters to this url.</p><ul><li>Install the popular query-string library on your app: <a href="https://www.npmjs.com/package/query-string">https://www.npmjs.com/package/query-string</a></li><li>Create the Google login url.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/469c82496e7ca6d3063caed5bd91a9ef/href">https://medium.com/media/469c82496e7ca6d3063caed5bd91a9ef/href</a></iframe><ul><li>Make sure your scopes match up to the data that you wish to access later on in your code. For example, you will need to add the “https://www.googleapis.com/auth/userinfo.email” scope to access the user’s email address.</li><li>See url parameter options here: <a href="https://developers.google.com/identity/protocols/OAuth2WebServer#obtainingaccesstokens">https://developers.google.com/identity/protocols/OAuth2WebServer#obtainingaccesstokens</a></li></ul><p>Now that you’ve created the login url, add it to your app or website. Here is a simple React example.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bcf28655e61b994fdbefbae3ed2ff247/href">https://medium.com/media/bcf28655e61b994fdbefbae3ed2ff247/href</a></iframe><h3>Step 4 of 5 🔐</h3><p>As mentioned before, once your users login, they will be redirected back to your app. The url they are redirect to will contain a special <strong>code</strong>. For example:</p><pre>https://www.example.com/authenticate/google?code=CODE_IS_HERE</pre><p>We will use to create an access token. An access token is required to authenticate any future requests we send to Google such as getting the user’s name or email address. To get the code and create the access token, we will use the same query-string library that we used before. Let’s go!</p><ul><li>Get the code from the url.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cec6758e27a62a4b5abfe366b55772e6/href">https://medium.com/media/cec6758e27a62a4b5abfe366b55772e6/href</a></iframe><ul><li>Send the code to your server to be processed — not applicable when already on the server i.e. when using express.</li><li>Create an access token from your code.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/194d52471126dfa4add554a736e3a018/href">https://medium.com/media/194d52471126dfa4add554a736e3a018/href</a></iframe><ul><li>See url parameter options here: <a href="https://developers.google.com/identity/protocols/OAuth2WebServer#exchange-authorization-code">https://developers.google.com/identity/protocols/OAuth2WebServer#exchange-authorization-code</a></li><li>Remember to store secret variables in process.env which is easy to do with the dotenv library: <a href="https://www.npmjs.com/package/dotenv">https://www.npmjs.com/package/dotenv</a></li></ul><h3>Step 5 of 5 👩‍💻</h3><p>Now that you got the access token, we can use it to get data from the Google API. We’ve gone ahead and created an example request which you can use to get some basic user details.</p><ul><li>Create a HTTP request with the access token to get the user’s data.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd2c4064297fb1198be213834332821a/href">https://medium.com/media/bd2c4064297fb1198be213834332821a/href</a></iframe><ul><li>See the Google API Explorer here: <a href="https://developers.google.com/apis-explorer/#p/">https://developers.google.com/apis-explorer/#p/</a></li></ul><h3>Yahooo! 🎉</h3><p>If you enjoyed this article, please give it a <strong>few claps </strong>— <em>you can leave up to 50</em> — or you can comment if you have any questions, I’ll do my best to answer them!</p><p>Liked this tutorial? ❤️ Here are some <strong>more:</strong></p><ul><li><a href="https://medium.com/authpack/facebook-auth-with-node-js-c4bb90d03fc0">Easy Facebook Auth with Node.js</a></li><li><a href="https://medium.com/authpack/easy-github-auth-with-node-js-502d3d8f8e62">Easy GitHub Auth with Node.js</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=99ac40b97f4c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>