<?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: Denil</title>
    <description>The latest articles on DEV Community by Denil (@denilany).</description>
    <link>https://dev.to/denilany</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%2F1809740%2Fc8a60f50-5abb-4197-a108-ba3f4dd47708.jpeg</url>
      <title>DEV Community: Denil</title>
      <link>https://dev.to/denilany</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/denilany"/>
    <language>en</language>
    <item>
      <title>Using Context in Next.js – A Beginner-Friendly Guide</title>
      <dc:creator>Denil</dc:creator>
      <pubDate>Mon, 21 Jul 2025 18:35:55 +0000</pubDate>
      <link>https://dev.to/denilany/using-context-in-nextjs-a-beginner-friendly-guide-3fo0</link>
      <guid>https://dev.to/denilany/using-context-in-nextjs-a-beginner-friendly-guide-3fo0</guid>
      <description>&lt;p&gt;Managing state in a modern web app can get tricky, especially as your app grows. If you’ve ever found yourself passing props down multiple layers of components in your Next.js project, it’s time to meet your new best friend: &lt;strong&gt;React Context&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this post, we'll explore how &lt;strong&gt;Context&lt;/strong&gt; works in a Next.js app, when to use it, and how to set it up step by step—with simple examples for beginners.&lt;/p&gt;




&lt;h2&gt;
  
  
  🤔 What Is Context?
&lt;/h2&gt;

&lt;p&gt;In plain English, &lt;strong&gt;Context&lt;/strong&gt; is a way to share data between components &lt;strong&gt;without passing props manually at every level&lt;/strong&gt;. Think of it like a global store that's accessible to any component in your app, as long as it's wrapped in a Context Provider.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Use Context in Next.js?
&lt;/h2&gt;

&lt;p&gt;Next.js is built on top of React, so it supports the React Context API out of the box. Context becomes useful when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple components need access to the same data (e.g. user info, theme settings).&lt;/li&gt;
&lt;li&gt;You want to avoid &lt;strong&gt;prop drilling&lt;/strong&gt;—passing props down through components that don’t need them.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Common Use Cases
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Authentication (user logged in or not)&lt;/li&gt;
&lt;li&gt;Theme toggles (light/dark mode)&lt;/li&gt;
&lt;li&gt;Language preferences&lt;/li&gt;
&lt;li&gt;Cart state in e-commerce apps&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step-by-Step: Using Context in Next.js
&lt;/h2&gt;

&lt;p&gt;Let’s walk through setting up a simple &lt;strong&gt;ThemeContext&lt;/strong&gt; that toggles between light and dark mode.&lt;/p&gt;




&lt;h3&gt;
  
  
  1. Create the Context
&lt;/h3&gt;

&lt;p&gt;First, create a new file: &lt;code&gt;context/ThemeContext.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;createContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useContext&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;react&lt;/span&gt;&lt;span class="dl"&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;ThemeContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createContext&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;function&lt;/span&gt; &lt;span class="nf"&gt;ThemeProvider&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&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="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTheme&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;light&lt;/span&gt;&lt;span class="dl"&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;toggleTheme&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;setTheme&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;prev&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;prev&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;light&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dark&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;light&lt;/span&gt;&lt;span class="dl"&gt;'&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;ThemeContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;toggleTheme&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;children&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;/ThemeContext.Provider&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="c1"&gt;// Custom hook&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;useTheme&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;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ThemeContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  2. Wrap Your App in &lt;code&gt;_app.js&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;In your &lt;code&gt;pages/_app.js&lt;/code&gt;, wrap your entire app with the &lt;code&gt;ThemeProvider&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;ThemeProvider&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;../context/ThemeContext&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyApp&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pageProps&lt;/span&gt; &lt;span class="p"&gt;})&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;ThemeProvider&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;Component&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;pageProps&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&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;/ThemeProvider&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;MyApp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now every page and component in your app has access to the theme context.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Consume the Context in a Component
&lt;/h3&gt;

&lt;p&gt;Create a component to toggle the theme:&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="c1"&gt;// components/ThemeToggle.js&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;useTheme&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;../context/ThemeContext&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;ThemeToggle&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="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;toggleTheme&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useTheme&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;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;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Current&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;theme&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;/p&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;toggleTheme&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Toggle&lt;/span&gt; &lt;span class="nx"&gt;Theme&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use this component in any page:&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="c1"&gt;// pages/index.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ThemeToggle&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;../components/ThemeToggle&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;Home&lt;/span&gt;&lt;span class="p"&gt;()&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;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;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Context&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;Next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&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;ThemeToggle&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="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  When to Use Context
&lt;/h2&gt;

&lt;p&gt;Use context when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need &lt;strong&gt;shared state&lt;/strong&gt; between many components.&lt;/li&gt;
&lt;li&gt;Props become hard to manage.&lt;/li&gt;
&lt;li&gt;You’re handling &lt;strong&gt;global settings&lt;/strong&gt; (like dark mode or user auth).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But remember:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Don’t use context for &lt;strong&gt;frequently changing values&lt;/strong&gt; (like form inputs or live data). For those cases, use local state or libraries like Zustand, Redux, or Recoil.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Bonus: Persist Theme with LocalStorage
&lt;/h2&gt;

&lt;p&gt;Want to remember the user's theme setting?&lt;/p&gt;

&lt;p&gt;Update &lt;code&gt;ThemeProvider&lt;/code&gt; with this:&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;useEffect&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;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;useEffect&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;saved&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;theme&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;saved&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;setTheme&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;saved&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="nf"&gt;useEffect&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;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;theme&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;theme&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="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the selected theme sticks even after the user refreshes the page!&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Here’s a quick recap:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;What it Does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;createContext()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Creates a new context&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;&amp;lt;Provider&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Wraps your app to provide state&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;useContext()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Lets you use that state anywhere&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;useEffect()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Helps persist data like theme preferences&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Using Context in Next.js is just like in React—but with Next.js’s file-based routing and SSR capabilities, it shines even more. It simplifies your state management for global values like themes, authentication, and preferences, especially when used with clean component architecture.&lt;/p&gt;

&lt;p&gt;If you're just starting out, it's a great tool to learn before jumping into more advanced state libraries.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>nextjs</category>
    </item>
    <item>
      <title>Understanding Private and Public Keys</title>
      <dc:creator>Denil</dc:creator>
      <pubDate>Mon, 21 Apr 2025 20:53:01 +0000</pubDate>
      <link>https://dev.to/denilany/understanding-private-and-public-keys-h2f</link>
      <guid>https://dev.to/denilany/understanding-private-and-public-keys-h2f</guid>
      <description>&lt;p&gt;When you hear about cryptocurrencies like Bitcoin or Ethereum, you often come across the terms &lt;strong&gt;private key&lt;/strong&gt; and &lt;strong&gt;public key&lt;/strong&gt;. These are essential to how crypto works — they're what keep your coins safe and allow you to send and receive digital money securely.&lt;/p&gt;

&lt;p&gt;Let’s break it down in a way that’s easy to understand, even if you’re not super tech-savvy.&lt;/p&gt;




&lt;h2&gt;
  
  
  Imagine a Mailbox
&lt;/h2&gt;

&lt;p&gt;Think of a &lt;strong&gt;public key&lt;/strong&gt; like your &lt;strong&gt;email address&lt;/strong&gt; or &lt;strong&gt;home mailbox&lt;/strong&gt;. Anyone can see it and send you a message (or crypto) using it.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;private key&lt;/strong&gt; is like the &lt;strong&gt;key to unlock your mailbox&lt;/strong&gt;. It’s secret. Only you should have it. It lets you access and control the messages (or coins) inside.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔑 What Are Keys, Technically?
&lt;/h2&gt;

&lt;p&gt;In crypto, keys are really just &lt;strong&gt;long strings of numbers and letters&lt;/strong&gt; generated by cryptographic algorithms.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;public key&lt;/strong&gt; is derived from the private key using math.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;private key&lt;/strong&gt; is randomly generated and should never be shared.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They work together as a &lt;strong&gt;key pair&lt;/strong&gt; using something called &lt;strong&gt;asymmetric encryption&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  How They Work Together
&lt;/h2&gt;

&lt;p&gt;Let’s say Alice wants to send Bob 1 Bitcoin.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Bob shares his &lt;strong&gt;public key&lt;/strong&gt; with Alice.&lt;/li&gt;
&lt;li&gt;Alice sends 1 BTC to that public key.&lt;/li&gt;
&lt;li&gt;Bob uses his &lt;strong&gt;private key&lt;/strong&gt; to access the funds.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;No one else can spend the BTC Alice sent, because only Bob has the &lt;strong&gt;private key&lt;/strong&gt; that “unlocks” that crypto.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Is This So Secure?
&lt;/h2&gt;

&lt;p&gt;It’s all based on &lt;strong&gt;math that’s easy to do one way, but practically impossible to reverse&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's easy to generate a public key from a private key.&lt;/li&gt;
&lt;li&gt;It's nearly impossible to figure out the private key just from knowing the public key.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is why even if someone knows your public key (which everyone can see), they &lt;strong&gt;can’t steal your crypto&lt;/strong&gt; — unless they get your private key.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real-Life Example: A Bitcoin Wallet
&lt;/h2&gt;

&lt;p&gt;When you create a Bitcoin wallet:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It generates a &lt;strong&gt;private key&lt;/strong&gt; and a &lt;strong&gt;public key&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The public key is hashed to make your &lt;strong&gt;Bitcoin address&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;You give people your &lt;strong&gt;address&lt;/strong&gt; (public key) to receive Bitcoin.&lt;/li&gt;
&lt;li&gt;You use your &lt;strong&gt;private key&lt;/strong&gt; to send Bitcoin.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Keep Your Private Key Safe!
&lt;/h2&gt;

&lt;p&gt;If someone gets your private key, they &lt;strong&gt;can steal all your crypto&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here’s how to stay safe:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;✅ Do&lt;/th&gt;
&lt;th&gt;❌ Don’t&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Store your private key offline&lt;/td&gt;
&lt;td&gt;Share your private key with anyone&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Use a hardware wallet&lt;/td&gt;
&lt;td&gt;Save your key in plain text on your computer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Write it down and keep it safe&lt;/td&gt;
&lt;td&gt;Email your private key&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Quick Recap
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Term&lt;/th&gt;
&lt;th&gt;What it is&lt;/th&gt;
&lt;th&gt;Who can see it?&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Public Key&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Your receiving address&lt;/td&gt;
&lt;td&gt;Everyone&lt;/td&gt;
&lt;td&gt;Lets people send you crypto&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Private Key&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Your secret password&lt;/td&gt;
&lt;td&gt;Only you&lt;/td&gt;
&lt;td&gt;Lets you spend your crypto&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Understanding private and public keys is &lt;strong&gt;key&lt;/strong&gt; (pun intended!) to staying safe in the crypto world. You don’t need to be a tech wizard — just remember:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Public key&lt;/strong&gt; = share to receive.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Private key&lt;/strong&gt; = guard with your life.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you protect your private key, you protect your digital money.&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>web3</category>
      <category>ethereum</category>
      <category>bitcoin</category>
    </item>
    <item>
      <title>OAuth2 Explained Simply — For Developers Who Hate Overcomplicated Docs</title>
      <dc:creator>Denil</dc:creator>
      <pubDate>Sun, 06 Apr 2025 19:45:55 +0000</pubDate>
      <link>https://dev.to/denilany/oauth2-explained-simply-for-developers-who-hate-overcomplicated-docs-42j5</link>
      <guid>https://dev.to/denilany/oauth2-explained-simply-for-developers-who-hate-overcomplicated-docs-42j5</guid>
      <description>&lt;h2&gt;
  
  
  🧠 Introduction — OAuth2, Demystified
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;You've probably clicked &lt;strong&gt;"Login with Google"&lt;/strong&gt; or &lt;strong&gt;"Sign in with GitHub"&lt;/strong&gt; more times than you can count. But have you ever stopped and wondered:&lt;br&gt;&lt;br&gt;
&lt;strong&gt;What &lt;em&gt;actually&lt;/em&gt; happens under the hood when I do that?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That slick one-click login experience is powered by &lt;strong&gt;OAuth2&lt;/strong&gt;, a protocol designed to let applications access user data &lt;em&gt;without&lt;/em&gt; asking for passwords. It’s one of the most critical yet misunderstood pieces of modern web security.&lt;/p&gt;

&lt;p&gt;Whether you’re building a web app, a mobile app, or an API-driven service, there's a good chance you'll need to integrate OAuth2 at some point. But here’s the catch:&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Most OAuth2 documentation reads like a textbook written by robots for robots.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this article, I’ll walk you through OAuth2 in &lt;em&gt;plain English&lt;/em&gt;—what it is, why it exists, and how it works—step by step.&lt;/p&gt;

&lt;p&gt;By the end, you'll:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Understand the core flow of OAuth2 (without memorizing jargon)&lt;/li&gt;
&lt;li&gt;Know what each piece of the protocol does and why&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  🔐 What is OAuth2 (and Why Should You Care?)
&lt;/h2&gt;

&lt;p&gt;OAuth2 is a protocol that lets apps &lt;strong&gt;access user data on another service—securely and with permission&lt;/strong&gt;—without ever seeing the user’s password.&lt;/p&gt;

&lt;p&gt;Think of it like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;OAuth2 is like handing a valet a special key that only starts your car—but doesn’t open your glove box, trunk, or access your phone via Bluetooth.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
The valet (a third-party app) can drive your car (perform limited tasks) without ever having full control over your belongings (like your personal data).&lt;/p&gt;
&lt;/blockquote&gt;


&lt;h3&gt;
  
  
  🤔 Why Not Just Share the Password?
&lt;/h3&gt;

&lt;p&gt;Before OAuth2, some apps &lt;em&gt;literally&lt;/em&gt; asked users for their credentials to access third-party services (like email or cloud storage). Sketchy, right?&lt;/p&gt;

&lt;p&gt;OAuth2 solves this problem by letting users &lt;strong&gt;authorize access&lt;/strong&gt; through a secure process handled by the service they trust (like Google or GitHub). The user logs in there, not in your app, and your app receives a special &lt;strong&gt;token&lt;/strong&gt; that allows access to certain parts of their data.&lt;/p&gt;


&lt;h3&gt;
  
  
  🧩 Core Use Cases of OAuth2
&lt;/h3&gt;

&lt;p&gt;You’ve probably already interacted with OAuth2 if you’ve done any of these:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Signed into a website using Google, GitHub, Facebook, or Twitter&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Connected your GitHub repo to a CI/CD service like Netlify or CircleCI&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Granted a mobile app access to your Google Calendar or Contacts&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Authorized Postman to use your Spotify account data for testing&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Behind the scenes, OAuth2 makes all of that possible—&lt;strong&gt;without ever giving away your actual login credentials.&lt;/strong&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  👨‍💻 Why It Matters to Developers
&lt;/h3&gt;

&lt;p&gt;If you’re building an app that needs to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Let users log in with a third-party account&lt;/li&gt;
&lt;li&gt;Access APIs like GitHub, Google Calendar, Dropbox, Spotify, etc.&lt;/li&gt;
&lt;li&gt;Allow integrations with services on behalf of the user&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;...then OAuth2 is your new best friend.&lt;/p&gt;

&lt;p&gt;And while it &lt;em&gt;can&lt;/em&gt; be complex, understanding the fundamentals will help you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Avoid confusing errors (like that dreaded &lt;code&gt;CORS&lt;/code&gt; or &lt;code&gt;invalid_redirect_uri&lt;/code&gt; stuff)&lt;/li&gt;
&lt;li&gt;Secure your app against token misuse and CSRF attacks&lt;/li&gt;
&lt;li&gt;Build user trust with a seamless and safe login experience&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Next, let’s break down the roles involved in an OAuth2 flow—like who’s handing the valet key and who’s driving the car. 🧑‍🔧&lt;/p&gt;


&lt;h2&gt;
  
  
  🧱 The Key Players in OAuth2 (Who's Who in the Zoo)
&lt;/h2&gt;

&lt;p&gt;OAuth2 can feel like a lot, but once you understand who’s playing what role, it all starts to click.&lt;/p&gt;

&lt;p&gt;Let’s break down the four main characters in this security drama—using simple language and real-world metaphors.&lt;/p&gt;
&lt;h3&gt;
  
  
  👤 1. &lt;strong&gt;Resource Owner&lt;/strong&gt; — &lt;em&gt;The User&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;This is the person who owns the data and grants permission.  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Think of them as the &lt;strong&gt;car owner&lt;/strong&gt; who decides whether or not to give someone the valet key.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;They’re the ones who log into Google or GitHub and approve your app's request.&lt;/p&gt;
&lt;h3&gt;
  
  
  📱 2. &lt;strong&gt;Client&lt;/strong&gt; — &lt;em&gt;Your Application&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;This is your app that wants access to the user’s data.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Your app is the &lt;strong&gt;valet&lt;/strong&gt; asking for permission to drive the car—but it needs to go through the proper process to prove it's trustworthy.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The client never sees the user’s credentials (like their password). Instead, it gets a &lt;strong&gt;token&lt;/strong&gt; after the user grants permission.&lt;/p&gt;
&lt;h3&gt;
  
  
  🔐 3. &lt;strong&gt;Authorization Server&lt;/strong&gt; — &lt;em&gt;The Gatekeeper&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;This is the service that authenticates the user and issues tokens.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Think of it like the &lt;strong&gt;front desk at a hotel&lt;/strong&gt; that verifies the user’s identity and gives out digital keycards with limited access.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Examples:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Google’s OAuth2 server
&lt;/li&gt;
&lt;li&gt;GitHub’s OAuth app system
&lt;/li&gt;
&lt;li&gt;Facebook’s Login service&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the place users are redirected to when they click “Login with GitHub”.&lt;/p&gt;
&lt;h3&gt;
  
  
  🗂 4. &lt;strong&gt;Resource Server&lt;/strong&gt; — &lt;em&gt;The Data Vault&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;This is the server that holds the user’s protected data—like email, profile info, repositories, playlists, etc.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It’s the &lt;strong&gt;garage where the car is parked&lt;/strong&gt;, and only lets people in who have the right kind of valet key (a valid access token).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In many cases, the Authorization Server and Resource Server are the same (e.g., in Google’s system), but they &lt;em&gt;don’t have to be&lt;/em&gt;.&lt;/p&gt;


&lt;h3&gt;
  
  
  🎯 Real Example: Logging in With GitHub
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Role&lt;/th&gt;
&lt;th&gt;Who It Is&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Resource Owner&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;You (the user)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Client&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Your web app&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Authorization Server&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;GitHub’s OAuth server&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Resource Server&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;GitHub’s API (e.g., &lt;code&gt;/user&lt;/code&gt;, &lt;code&gt;/repos&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;


&lt;h2&gt;
  
  
  🔄 The OAuth2 Flow — Step by Step (Authorization Code Grant)
&lt;/h2&gt;

&lt;p&gt;Now that we know the key players, let’s walk through &lt;strong&gt;how they actually interact&lt;/strong&gt; in the most commonly used OAuth2 flow: the &lt;strong&gt;Authorization Code Grant&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is the flow typically used for &lt;strong&gt;web apps&lt;/strong&gt; where you want to authenticate users securely.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;TL;DR: Your app asks for permission, the user approves it, and your app gets a temporary "hall pass" (access token) to call APIs on their behalf.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s break it down step by step 👇&lt;/p&gt;


&lt;h3&gt;
  
  
  🧨 Step 1: The User Clicks “Login with GitHub”
&lt;/h3&gt;

&lt;p&gt;Your app presents a button:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://github.com/login/oauth/authorize?client_id=YOUR_CLIENT_ID&amp;amp;redirect_uri=http://localhost:8080/callback&amp;amp;scope=user"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  Login with GitHub
&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the user clicks it, they’re redirected to the &lt;strong&gt;Authorization Server&lt;/strong&gt; (in this case, GitHub).&lt;/p&gt;

&lt;h3&gt;
  
  
  🔐 Step 2: Redirect to Authorization Server
&lt;/h3&gt;

&lt;p&gt;The browser navigates to a URL like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="c"&gt;//github.com/login/oauth/authorize&lt;/span&gt;
  &lt;span class="err"&gt;?&lt;/span&gt;&lt;span class="n"&gt;client_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;abc123&lt;/span&gt;
  &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;redirect_uri&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="c"&gt;//localhost:8080/callback&lt;/span&gt;
  &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;
  &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;randomstring&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here’s what each query parameter means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;client_id&lt;/code&gt;: Your app’s public identifier on GitHub&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;redirect_uri&lt;/code&gt;: Where GitHub should send the user afterward&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;scope&lt;/code&gt;: What your app is requesting (e.g., &lt;code&gt;user&lt;/code&gt;, &lt;code&gt;repo&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;state&lt;/code&gt;: A random string to protect against CSRF attacks&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  👤 Step 3: User Logs In and Approves Access
&lt;/h3&gt;

&lt;p&gt;GitHub shows a prompt:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Do you want to allow &lt;code&gt;Your App&lt;/code&gt; to access your profile?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Once the user logs in and approves, GitHub redirects them back to your app via the &lt;code&gt;redirect_uri&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  🧾 Step 4: Authorization Code is Sent Back
&lt;/h3&gt;

&lt;p&gt;Your app receives a GET request at the callback endpoint:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET /callback?code=123456&amp;amp;state=randomstring
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;code&lt;/code&gt;: A temporary &lt;strong&gt;authorization code&lt;/strong&gt; you’ll now exchange for a token.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;state&lt;/code&gt;: You verify this matches what you originally sent (CSRF protection).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🔄 Step 5: App Exchanges Code for Access Token
&lt;/h3&gt;

&lt;p&gt;Your backend makes a &lt;strong&gt;POST request&lt;/strong&gt; to GitHub’s token endpoint:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;POST https://github.com/login/oauth/access_token
Content-Type: application/json

{
  "client_id": "abc123",
  "client_secret": "your-client-secret",
  "code": "123456",
  "redirect_uri": "http://localhost:8080/callback"
}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;🧩 &lt;strong&gt;Where do you get your &lt;code&gt;client_id&lt;/code&gt; and &lt;code&gt;client_secret&lt;/code&gt;?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
To get these, you need to &lt;a href="https://github.com/settings/developers" rel="noopener noreferrer"&gt;register a new OAuth application on GitHub&lt;/a&gt;.&lt;br&gt;&lt;br&gt;
Once registered, GitHub will give you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;Client ID&lt;/strong&gt; (public identifier for your app)&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;Client Secret&lt;/strong&gt; (keep this safe—used for secure token exchanges)&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;If everything checks out, GitHub responds:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"access_token"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"gho_abcdef123456"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scope"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"token_type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"bearer"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You now have the token—&lt;strong&gt;but not user info yet.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  🌐 Step 6: App Uses Access Token to Access User Data
&lt;/h3&gt;

&lt;p&gt;You can now call GitHub’s API on behalf of the user:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;GET https://api.github.com/user
Authorization: Bearer gho_abcdef123456
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;GitHub responds with the user’s profile info:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"login"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"yourusername"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Denil"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"avatar_url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"https://..."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Mission accomplished! 🎯 You’ve successfully authenticated a user using OAuth2 and can now personalize the app based on their profile.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🔧 &lt;strong&gt;Coming Up in Part 2&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
In the next part of this series, we’ll roll up our sleeves and &lt;strong&gt;implement the entire OAuth2 flow in Go&lt;/strong&gt;, using the &lt;a href="https://pkg.go.dev/golang.org/x/oauth2" rel="noopener noreferrer"&gt;&lt;code&gt;golang.org/x/oauth2&lt;/code&gt;&lt;/a&gt; package with &lt;strong&gt;GitHub&lt;/strong&gt; as our OAuth provider.&lt;br&gt;&lt;br&gt;
You’ll learn how to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Register an OAuth app on GitHub to get your &lt;code&gt;client_id&lt;/code&gt; and &lt;code&gt;client_secret&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Set up a simple Go web server to handle login, callback, and token exchange&lt;/li&gt;
&lt;li&gt;Use the access token to fetch real user data from the GitHub API&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Stay tuned—Part 2 will turn everything we’ve learned into real, working code. 💻🔥&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>oauth</category>
      <category>authentication</category>
      <category>security</category>
    </item>
  </channel>
</rss>
