<?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: Sourav Mishra</title>
    <description>The latest articles on DEV Community by Sourav Mishra (@souravvmishra).</description>
    <link>https://dev.to/souravvmishra</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%2F2406309%2F204eab1b-96bc-48e2-9a18-55ca13991943.jpg</url>
      <title>DEV Community: Sourav Mishra</title>
      <link>https://dev.to/souravvmishra</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/souravvmishra"/>
    <language>en</language>
    <item>
      <title>What Does setTimeout(..., 0ms) Really Mean in JavaScript? (Event Loop Explained!)</title>
      <dc:creator>Sourav Mishra</dc:creator>
      <pubDate>Sat, 14 Dec 2024 11:57:18 +0000</pubDate>
      <link>https://dev.to/souravvmishra/what-does-settimeout-0ms-really-mean-in-javascript-event-loop-explained-1clc</link>
      <guid>https://dev.to/souravvmishra/what-does-settimeout-0ms-really-mean-in-javascript-event-loop-explained-1clc</guid>
      <description>&lt;h3&gt;
  
  
  What Does &lt;code&gt;setTimeout(..., 0ms)&lt;/code&gt; Really Mean in JavaScript? (Event Loop Explained!)
&lt;/h3&gt;

&lt;p&gt;Alright, let’s break down this whole &lt;code&gt;setTimeout&lt;/code&gt; thing with &lt;code&gt;0ms&lt;/code&gt;. At first glance, you’d think, “Bro, &lt;code&gt;0ms&lt;/code&gt; means it’ll run immediately, right?” But JavaScript has its own vibe, and &lt;code&gt;0ms&lt;/code&gt; isn’t as instant as you’d expect. Let’s figure this out together.  &lt;/p&gt;




&lt;h3&gt;
  
  
  The Code:
&lt;/h3&gt;

&lt;p&gt;Here’s the code we’re dissecting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;timerStart&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;success&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;timerEnd&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the output?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1  
2  
4  
timerStart  
success  
timerEnd  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Big Question: What’s Happening with &lt;code&gt;0ms&lt;/code&gt;?
&lt;/h3&gt;

&lt;p&gt;When you see &lt;code&gt;setTimeout(() =&amp;gt; { ... }, 0)&lt;/code&gt;, it feels like the code inside will run &lt;strong&gt;immediately&lt;/strong&gt; after &lt;code&gt;0ms&lt;/code&gt;. But nope, JavaScript is like, “Bro, I’ve got my own system, wait your turn!”  &lt;/p&gt;




&lt;h3&gt;
  
  
  JavaScript’s System: Event Loop
&lt;/h3&gt;

&lt;p&gt;JavaScript doesn’t just run code directly—it has a cool system called the &lt;strong&gt;event loop&lt;/strong&gt; to handle tasks. Think of it like this:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Main tasks (Synchronous)&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
Stuff like &lt;code&gt;console.log()&lt;/code&gt; runs first. These are the direct, in-your-face tasks.  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Microtasks (Promises)&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
Promises come next, even before timers. It’s like they’ve got a VIP ticket.  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Task queue (Timers like setTimeout)&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
Timers go to the back of the line and only run after the microtasks are done. Even if you give it &lt;code&gt;0ms&lt;/code&gt;, it waits.  &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Breaking Down the Code
&lt;/h3&gt;

&lt;p&gt;Let’s see what’s happening step by step:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Promise Created&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;console.log(1)&lt;/code&gt; runs immediately.
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setTimeout(() =&amp;gt; { ... }, 0)&lt;/code&gt; gets added to the &lt;strong&gt;task queue&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log(2)&lt;/code&gt; runs immediately.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Promise &lt;code&gt;.then()&lt;/code&gt;&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;.then()&lt;/code&gt; callback is added to the &lt;strong&gt;microtask queue&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;console.log(4)&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Runs directly since it’s part of the main script.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Microtask Runs (VIP)&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The promise resolves, so the &lt;code&gt;.then()&lt;/code&gt; callback logs &lt;code&gt;"success"&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Task Queue Runs&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;setTimeout&lt;/code&gt; callback finally gets its turn: logs &lt;code&gt;"timerStart"&lt;/code&gt; and &lt;code&gt;"timerEnd"&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Why Doesn’t &lt;code&gt;0ms&lt;/code&gt; Mean “Right Now”?
&lt;/h3&gt;

&lt;p&gt;Even though you said &lt;code&gt;0ms&lt;/code&gt;, the &lt;code&gt;setTimeout&lt;/code&gt; is &lt;strong&gt;always deferred&lt;/strong&gt;. JavaScript pushes it to the &lt;strong&gt;task queue&lt;/strong&gt;, no matter what. The system first clears all synchronous tasks and microtasks (promises) before touching the task queue.  &lt;/p&gt;

&lt;p&gt;So, in simple terms:&lt;br&gt;&lt;br&gt;
&lt;code&gt;0ms&lt;/code&gt; isn’t about the time—it’s about &lt;strong&gt;waiting for your turn&lt;/strong&gt;.  &lt;/p&gt;




&lt;h3&gt;
  
  
  Think of It Like This
&lt;/h3&gt;

&lt;p&gt;Imagine you’re in a queue for a rollercoaster:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Synchronous code: People who bought fast-track tickets—they’re first, no debate.
&lt;/li&gt;
&lt;li&gt;Promises: People with VIP passes—they’re next in line, even if they came late.
&lt;/li&gt;
&lt;li&gt;Timers (like &lt;code&gt;setTimeout&lt;/code&gt;): Regular people with no passes. Even if you tell them, “0ms wait,” they still have to stand in the regular queue.
&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Final Output Recap
&lt;/h3&gt;

&lt;p&gt;Here’s the order of events in our code:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;console.log(1)&lt;/code&gt; (Synchronous)
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log(2)&lt;/code&gt; (Synchronous)
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log(4)&lt;/code&gt; (Synchronous)
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;"success"&lt;/code&gt; from the &lt;code&gt;.then()&lt;/code&gt; (Microtask)
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log("timerStart")&lt;/code&gt; (Task queue)
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log("timerEnd")&lt;/code&gt; (Task queue)
&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Why Does This Matter?
&lt;/h3&gt;

&lt;p&gt;Understanding how &lt;code&gt;0ms&lt;/code&gt; really works helps you write better asynchronous code. It’s also a cool trick to explain to your friends when they’re debugging random delays in their JavaScript.  &lt;/p&gt;

&lt;p&gt;So next time you see &lt;code&gt;setTimeout(() =&amp;gt; { ... }, 0)&lt;/code&gt;, just remember—it’s not about the time; it’s about &lt;strong&gt;priority&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;Checkout : &lt;a href="https://www.google.com/url?source=web&amp;amp;rct=j&amp;amp;url=https://meetman.codestam.com/" rel="noopener noreferrer"&gt;Meetman - AI Powered Google Meet&lt;/a&gt;&lt;br&gt;
Portfolio : &lt;a href="https://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;rct=j&amp;amp;opi=89978449&amp;amp;url=https://souravvmishra.github.io/&amp;amp;ved=2ahUKEwjf1Nn5pKeKAxWbzTgGHffqLsAQFnoECCIQAQ&amp;amp;usg=AOvVaw3U-DrDhbIMoAx0Ks0pncXm" rel="noopener noreferrer"&gt;Sourav Mishra&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>node</category>
    </item>
    <item>
      <title>Adding Google Authentication in Next.js 14 with App Router: A Beginner-Friendly Guide</title>
      <dc:creator>Sourav Mishra</dc:creator>
      <pubDate>Mon, 11 Nov 2024 10:36:11 +0000</pubDate>
      <link>https://dev.to/souravvmishra/adding-google-authentication-in-nextjs-14-with-app-router-a-beginner-friendly-guide-3ag</link>
      <guid>https://dev.to/souravvmishra/adding-google-authentication-in-nextjs-14-with-app-router-a-beginner-friendly-guide-3ag</guid>
      <description>&lt;p&gt;Hey, you! Are you trying to add Google Authentication in a Next.js app but drowning in confusing tutorials? I feel you. As someone who has struggled with setting up authentication &lt;em&gt;from scratch&lt;/em&gt; without Firebase or NextAuth “magic” plugins, I’m here to say: &lt;strong&gt;it doesn’t have to be a nightmare&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;So, today, we’re diving into setting up Google Authentication in a Next.js 14 app. And we’ll make sure we don’t skip the basics, from setting up Next.js itself to adding Tailwind CSS for some smooth, beautiful styling. Ready? Let’s go!&lt;/p&gt;




&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;To make sure we’re all on the same page, you’ll need:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Google Cloud Console&lt;/strong&gt; account, which we’ll use to set up the OAuth credentials.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Basic JavaScript skills&lt;/strong&gt; (don’t worry, I’ll keep this beginner-friendly).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Next.js 14 installed&lt;/strong&gt; and ready to go.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s get started with the setup and then bring Google Authentication in as the cherry on top. 😎&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1: Set Up a New Next.js 14 App
&lt;/h2&gt;

&lt;p&gt;First things first, we need a Next.js project. To create one, run the following command in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx create-next-app@latest my-google-auth-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;During setup, when asked if you want to add Tailwind CSS, say &lt;strong&gt;YES&lt;/strong&gt;! If you’re working on an app these days without Tailwind, you’re missing out on some of the cleanest, most responsive styling out there. The beauty here is that Next.js will handle most of the Tailwind setup for you, so you’re up and running with minimal pain.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2: Set Up Google Cloud OAuth Credentials
&lt;/h2&gt;

&lt;p&gt;For Google Authentication, we’ll need to create some credentials on Google Cloud. Follow along to get this set up (no worries, it’s simpler than it sounds).&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to &lt;a href="https://console.cloud.google.com/" rel="noopener noreferrer"&gt;Google Cloud Console&lt;/a&gt; and start a new project if you don’t have one already. (And yes, if you’re wondering why you need to jump through these hoops just to let users sign in—&lt;em&gt;welcome to the world of cloud authentication.&lt;/em&gt; You’ll survive, I promise. Need more hand-holding through this? Just comment for a deeper dive!)&lt;/li&gt;
&lt;li&gt;Navigate to &lt;strong&gt;APIs &amp;amp; Services &amp;gt; Credentials&lt;/strong&gt; and click on &lt;strong&gt;Create Credentials&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;OAuth 2.0 Client ID&lt;/strong&gt; and set the application type to &lt;strong&gt;Web application&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;In &lt;strong&gt;Authorized redirect URIs&lt;/strong&gt;, enter &lt;code&gt;http://localhost:3000/api/auth/callback&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Google will now give you a &lt;strong&gt;Client ID&lt;/strong&gt; and &lt;strong&gt;Client Secret&lt;/strong&gt;. Keep these somewhere safe (or at least open in another tab). We’ll be needing them soon.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 3: Configure Environment Variables
&lt;/h2&gt;

&lt;p&gt;With our Google credentials in hand, we’ll store these securely using environment variables. In your Next.js project, create a &lt;code&gt;.env.local&lt;/code&gt; file and add the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
NEXTAUTH_SECRET=super-secret-key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure to replace &lt;code&gt;your-google-client-id&lt;/code&gt; and &lt;code&gt;your-google-client-secret&lt;/code&gt; with the actual values you got from Google Cloud. &lt;strong&gt;Do not&lt;/strong&gt; commit these to GitHub unless you want the whole internet freeloading on your Google project. (Seriously, don’t.)&lt;/p&gt;

&lt;h3&gt;
  
  
  About the &lt;code&gt;NEXTAUTH_SECRET&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Alright, let’s talk about the &lt;code&gt;NEXTAUTH_SECRET&lt;/code&gt;. This is like the lock on your app’s front door, keeping user sessions secure. It’s a key that NextAuth uses to sign and encrypt session cookies, which means users’ login info stays private.&lt;/p&gt;

&lt;p&gt;To create this secret key, you can run a quick command in your terminal. If you’re on macOS or Linux, here’s a one-liner to generate a strong, random key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;openssl rand &lt;span class="nt"&gt;-base64&lt;/span&gt; 32
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Take the output and add it to your &lt;code&gt;.env.local&lt;/code&gt; file under &lt;code&gt;NEXTAUTH_SECRET&lt;/code&gt;. With this key in place, your authentication is way more secure, keeping things locked up nice and tight. Here’s how it’ll look in &lt;code&gt;.env.local&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;NEXTAUTH_SECRET=your-super-secure-random-key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And yes, just like with the Google client ID and secret, &lt;strong&gt;don’t&lt;/strong&gt; push this to GitHub—leaking it would be like handing out the keys to your app’s front door!&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 4: Set Up NextAuth with Google Provider
&lt;/h2&gt;

&lt;p&gt;To handle authentication flow from scratch, let’s use &lt;code&gt;next-auth&lt;/code&gt;. Install it with this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;next-auth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, create a new file: &lt;code&gt;app/api/auth/[...nextauth]/route.js&lt;/code&gt;. This will handle the API routes for our authentication.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;NextAuth&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;next-auth&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;GoogleProvider&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;next-auth/providers/google&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;authOptions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nc"&gt;GoogleProvider&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;GOOGLE_CLIENT_ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;clientSecret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;GOOGLE_CLIENT_SECRET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NEXTAUTH_SECRET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;NextAuth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;authOptions&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;GET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;POST&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this setup, NextAuth will automatically handle all the tricky OAuth flows. You’ll be redirected to Google for login, and back to your app once authenticated.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 5: Create Sign-In and Sign-Out Functions
&lt;/h2&gt;

&lt;p&gt;Authentication is set up, but we need some buttons to actually sign in and out! Here’s where Tailwind CSS shines, letting us add beautiful buttons with minimal CSS fuss.&lt;/p&gt;

&lt;p&gt;In your &lt;code&gt;lib&lt;/code&gt; folder, create &lt;code&gt;lib/auth.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;signIn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;signOut&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;next-auth/react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleSignIn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;signIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;google&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleSignOut&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;signOut&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 6: Add Sign-In and Sign-Out Buttons
&lt;/h2&gt;

&lt;p&gt;Now, let’s add the actual UI. Go to &lt;code&gt;app/page.js&lt;/code&gt; and add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;handleSignIn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handleSignOut&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../lib/auth&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useSession&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;next-auth/react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;HomePage&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;session&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useSession&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;main&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;flex items-center justify-center min-h-screen bg-gray-100&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;session&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text-lg font-medium text-gray-700&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nx"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;
            &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleSignOut&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;px-4 py-2 mt-4 text-white bg-red-500 rounded-lg hover:bg-red-600&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
          &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nx"&gt;Sign&lt;/span&gt; &lt;span class="nx"&gt;Out&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;
          &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleSignIn&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;px-6 py-3 text-white bg-blue-600 rounded-lg hover:bg-blue-700&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="nx"&gt;Sign&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="kd"&gt;with&lt;/span&gt; &lt;span class="nx"&gt;Google&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;)}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/main&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, if the user is signed in, they see a welcome message and a &lt;strong&gt;Sign Out&lt;/strong&gt; button. If not, they see a &lt;strong&gt;Sign in with Google&lt;/strong&gt; button. Tailwind classes keep the styling easy and look neat.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 7: Test Your Google Authentication
&lt;/h2&gt;

&lt;p&gt;With everything in place, let’s run your Next.js app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go to &lt;code&gt;http://localhost:3000&lt;/code&gt;. You should see a &lt;strong&gt;Sign in with Google&lt;/strong&gt; button. Click it, and if everything’s working, you’ll see Google’s login screen. Authenticate, and Next.js will redirect you back to your app, displaying your Google account info.&lt;/p&gt;




&lt;h2&gt;
  
  
  You Did It! 🎉
&lt;/h2&gt;

&lt;p&gt;Setting up Google Authentication in a Next.js 14 app isn’t all sunshine and rainbows, but with this guide, it’s as close as we can get! You now have a solid, from-scratch setup that you can reuse and build upon.&lt;/p&gt;

&lt;p&gt;Authentication can seem scary, but once you break it down step-by-step (and throw in a little dark humor to keep things light), it’s totally manageable. So, next time someone complains about Google login being too complicated, you’ll know exactly what to do.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

&lt;p&gt;Oh, and if you want a tool that’ll keep you awake and in sync during meetings, check out my latest project: &lt;strong&gt;&lt;a href="https://meetman.codestam.com?utm_source=dev_post&amp;amp;utm_medium=guide&amp;amp;utm_campaign=google_auth_nextjs" rel="noopener noreferrer"&gt;Meetman&lt;/a&gt;&lt;/strong&gt;. It’s a real-time meeting chat and collaboration tool that uses Google Meet captions to transcribe meetings—perfect for those times when you just &lt;em&gt;might&lt;/em&gt; be zoning out. &lt;em&gt;Go on, give it a spin!&lt;/em&gt;&lt;/p&gt;

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