<?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>Forem: Shreelaxmi Hegde</title>
    <description>The latest articles on Forem by Shreelaxmi Hegde (@shreelaxmihegde).</description>
    <link>https://forem.com/shreelaxmihegde</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%2F3378285%2F60884750-931e-4fe1-abc7-ae25e61c90ba.jpg</url>
      <title>Forem: Shreelaxmi Hegde</title>
      <link>https://forem.com/shreelaxmihegde</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/shreelaxmihegde"/>
    <language>en</language>
    <item>
      <title>Auth Series #5: Authorization implementation with Passport.js</title>
      <dc:creator>Shreelaxmi Hegde</dc:creator>
      <pubDate>Sat, 25 Oct 2025 05:16:33 +0000</pubDate>
      <link>https://forem.com/shreelaxmihegde/auth-series-5-authorization-implementation-with-passportjs-362</link>
      <guid>https://forem.com/shreelaxmihegde/auth-series-5-authorization-implementation-with-passportjs-362</guid>
      <description>&lt;p&gt;We have covered &lt;a href="https://dev.to/shreelaxmihegde/auth-series-4-understanding-cookies-and-sessions-1ajg"&gt;Cookies and Sessions&lt;/a&gt; in the previous part.&lt;br&gt;
Now, it’s time to use them in our authorization flow.&lt;/p&gt;

&lt;p&gt;As discussed &lt;a href="https://dev.to/shreelaxmihegde/auth-series-1-understanding-authentication-and-authorization-17le"&gt;earlier&lt;/a&gt;, authorization means allowing access to specific parts or actions that a user is permitted to perform.&lt;br&gt;
Before we authorize a user, we first need to authenticate them (verify their identity).&lt;/p&gt;

&lt;p&gt;We’ve already &lt;a href="https://dev.to/shreelaxmihegde/auth-series-3-authentication-implementation-using-passportjs-416j"&gt;handled authentication&lt;/a&gt;.&lt;br&gt;
Now, our next goal is to remember the authenticated user across sessions. And that’s where cookies and sessions come into play.&lt;/p&gt;



&lt;p&gt;⚙️ &lt;strong&gt;&lt;u&gt;Session Setup&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After installing and importing &lt;code&gt;express-session&lt;/code&gt;, the next step is to configure our session options:&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;sessionOptions&lt;/span&gt; &lt;span class="o"&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;secretecode&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;resave&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;saveUninitialized&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;cookie&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;httpOnly&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// prevents client-side JavaScript access for better security&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;session&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sessionOptions&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;//setup express-session middleware&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;📌 &lt;strong&gt;&lt;u&gt;What does this code snippet do?&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
⤷ &lt;code&gt;secret&lt;/code&gt;: Used to sign the session ID cookie to prevent tampering.&lt;/p&gt;

&lt;p&gt;⤷ &lt;code&gt;resave: false&lt;/code&gt;: Ensures the session isn’t saved back to the store if it hasn’t been modified.&lt;/p&gt;

&lt;p&gt;⤷ &lt;code&gt;saveUninitialized: true&lt;/code&gt;: Saves new sessions that haven’t been modified yet.&lt;/p&gt;

&lt;p&gt;⤷ &lt;code&gt;cookie.httpOnly&lt;/code&gt;: Helps protect against XSS attacks by making cookies inaccessible via client-side JavaScript.&lt;/p&gt;

&lt;p&gt;In short, this middleware automatically attaches a session object to every request &lt;code&gt;(req.session)&lt;/code&gt;, allowing us to store and retrieve user-specific data securely on the server. These are the common parameters defined while creating session.&lt;/p&gt;



&lt;p&gt;🔐 &lt;strong&gt;&lt;u&gt;Passport Setup&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once the session is configured, we can connect it with &lt;em&gt;Passport.js&lt;/em&gt;, which handles authentication seamlessly.&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="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;passport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;session&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Connect Passport with session&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line ensures that Passport can store and access user data within the existing session.&lt;/p&gt;

&lt;p&gt;📌 &lt;strong&gt;&lt;u&gt;Why do we connecting Passport to session?&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
When a user logs in successfully, Passport authenticates them only for that single HTTP request.&lt;br&gt;
Since HTTP is a stateless protocol, the server doesn’t automatically remember the user between different requests.&lt;/p&gt;

&lt;p&gt;By connecting Passport to the session, we enable it to store the user’s identity in a session (on the server) and keep them logged in across multiple pages or browser refreshes without needing to log in again each time.&lt;/p&gt;



&lt;p&gt;We have successfully connected Passport with express-session. But there are two more crucial steps to complete the flow: &lt;code&gt;serializeUser()&lt;/code&gt; and &lt;code&gt;deserializeUser()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;📌 &lt;strong&gt;&lt;u&gt;Why do we need them?&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When a user logs in, a session is created and attached to all future requests from the browser.&lt;/p&gt;

&lt;p&gt;⤷ However, when a request reaches the server, it only contains the session ID. This means the server sees some &lt;strong&gt;“unknown ID”&lt;/strong&gt; but doesn’t yet know which user it belongs to.&lt;/p&gt;

&lt;p&gt;⤷ &lt;code&gt;serializeUser()&lt;/code&gt; solves this problem by telling Passport what user information to store in the session (usually the user ID), effectively linking the session ID to a specific user.&lt;/p&gt;

&lt;p&gt;⤷ On every subsequent request after login, &lt;code&gt;deserializeUser()&lt;/code&gt; runs. It fetches the full user information from the database using the ID stored in the session.&lt;/p&gt;

&lt;p&gt;⤷ This ensures that &lt;code&gt;req.user&lt;/code&gt; is populated, so you can access the authenticated user’s details in any route or middleware.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage :&lt;/strong&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="nx"&gt;passport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;serializeUser&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="nf"&gt;serializeUser&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="nx"&gt;passport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deserializeUser&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="nf"&gt;deserializeUser&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;📌 &lt;strong&gt;&lt;u&gt;Passport Session Flow: Serialize &amp;amp; Deserialize&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User logs in
     │
     ▼
Passport authenticates credentials
     │
     ▼
serializeUser() runs
     │
     │  Stores only the user ID in the session
     ▼
Session created and sessionID sent to browser as cookie
     │
     ▼
Browser makes another request
     │
     ▼
Session middleware reads sessionID from cookie
     │
     ▼
deserializeUser() runs
     │
     │  Uses stored user ID to fetch full user info from DB
     ▼
req.user is populated → available in all route handlers

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

&lt;/div&gt;






&lt;p&gt;These are common practices to implement an Authorization system.&lt;br&gt;
Now, let’s see how we can use them in our routes to authorize actions like edit, delete, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example Scenario: Blog Application&lt;/strong&gt;&lt;br&gt;
In our blog app, users can create, edit, delete, or update their posts.&lt;/p&gt;

&lt;p&gt;We have already completed the authentication steps.&lt;br&gt;
Now, our goal is to allow users to perform actions only on their own posts. for example, the author of a post can edit or delete it, but others cannot.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Step 1: Define Middleware&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
We first create a middleware function to check the user’s role or ownership:&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;function&lt;/span&gt; &lt;span class="nf"&gt;isAuthor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&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="nx"&gt;next&lt;/span&gt;&lt;span class="p"&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;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;req&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;role&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;author&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="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&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="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;403&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Access Denied&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;Step 2: Apply Authorization in Routes&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
Use the middleware in your route definitions:&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="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/posts/:id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isAuthor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;deletePost&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;How it works internally:&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
⤷ When a user tries to delete a post, the request hits the route handler.&lt;br&gt;
⤷ The isAuthor middleware runs first.&lt;br&gt;
⤷ req.user (fetched via deserializeUser()) is checked against the required role.&lt;br&gt;
⤷ If authorized, the request proceeds to deletePost. Otherwise, access is denied.&lt;/p&gt;

&lt;p&gt;You can implement similar middleware for all other routes that require authorization.&lt;/p&gt;




&lt;p&gt;This completes our blog series on Authentication and Authorization in Express.js.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed this series and gained a deeper understanding of the under-the-hood workflow behind authentication and authorization.&lt;/p&gt;

&lt;p&gt;Thank you for reading! 🎉&lt;/p&gt;

</description>
      <category>authseries</category>
      <category>javascript</category>
      <category>express</category>
      <category>passport</category>
    </item>
    <item>
      <title>Auth Series #4: Understanding Cookies and Sessions.</title>
      <dc:creator>Shreelaxmi Hegde</dc:creator>
      <pubDate>Sat, 25 Oct 2025 05:15:29 +0000</pubDate>
      <link>https://forem.com/shreelaxmihegde/auth-series-4-understanding-cookies-and-sessions-1ajg</link>
      <guid>https://forem.com/shreelaxmihegde/auth-series-4-understanding-cookies-and-sessions-1ajg</guid>
      <description>&lt;p&gt;After implementing the Authentication mechanism in the &lt;a href="https://dev.to/shreelaxmihegde/auth-series-3-authentication-implementation-using-passportjs-416j"&gt;part 3&lt;/a&gt;, we are now going to learn another interesting thing : &lt;strong&gt;Cookies and Sessions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Cookies and sessions play a crucial role in the authentication and authorization process. &lt;br&gt;
Most &lt;strong&gt;familiar&lt;/strong&gt; scenarios with behind the scene cookies are:&lt;/p&gt;

&lt;p&gt;❖ Keeping you logged in, even when the same website is opened in different tabs.&lt;br&gt;
❖ Ensuring you don’t have to log in every time when you visit the website.&lt;br&gt;
❖ Remembering what items the you added to the cart.&lt;br&gt;
❖ Recommending related posts, videos, or items based on your activity.&lt;br&gt;
❖ You don't have to toggle the theme(dark mode) for each pages.&lt;/p&gt;

&lt;p&gt;Most of us have experienced it. These are just a few of the jobs cookies handle. Let’s explore them in more detail.&lt;/p&gt;



&lt;p&gt;📌 &lt;strong&gt;&lt;u&gt;What are cookies?&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Cookies are &lt;strong&gt;small pieces of data&lt;/strong&gt; that a website stores in the user's browser. They are also called HTTP cookies, web cookies, or browser cookies. Cookies help websites remember information about the user’s activity, preferences, or session.&lt;/p&gt;

&lt;p&gt;There are different types of cookies based on their usage:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tracking Cookies [Track user activity to provide personalized recommendations based on browsing behavior.]&lt;/li&gt;
&lt;li&gt;Session Cookies [Temporary cookies that are deleted when the browser is closed, used to keep users logged in during a session.]&lt;/li&gt;
&lt;li&gt;Persistent Cookies [Stored on the browser for a longer time, can remember login info or user preferences across visits.]
etc.&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;📌 &lt;strong&gt;&lt;u&gt;How Cookies work?&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;⤷ Server sends a cookie to the browser in http response header (&lt;code&gt;Set-Cookie&lt;/code&gt;).&lt;br&gt;
⤷ Browser stores the cookie.&lt;br&gt;
⤷ For every subsequent request, browser sends it back to the server  in request header.&lt;br&gt;
⤷ Server reads the cookie and performs the logic defined.&lt;/p&gt;



&lt;p&gt;📌 &lt;strong&gt;&lt;u&gt;Need for Cookies in Authentication and Authorization :&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are many types of cookies, but in the authentication and authorization process, we use them primarily to remember users and keep them logged in as they navigate across different pages of a website.&lt;/p&gt;

&lt;p&gt;Without cookies, users would have to log in &lt;strong&gt;every time&lt;/strong&gt; they visit a page or refresh the website, which can be &lt;strong&gt;frustrating&lt;/strong&gt;. This happens because the server does not inherently know who the user is. So, it renders the login form each time authentication is required.&lt;/p&gt;

&lt;p&gt;To provide a smooth and personalized browsing experience, the server needs a way to &lt;strong&gt;“remember”&lt;/strong&gt; the user. Cookies serve this purpose by storing user specific information and sending it back to the server with every request, allowing the server to identify and authorize the user seamlessly.&lt;/p&gt;

&lt;p&gt;Let’s see how we can create cookies in an Express.js application.&lt;/p&gt;



&lt;p&gt;📌 &lt;strong&gt;&lt;u&gt;Creating Cookies in Express.js :&lt;/u&gt;&lt;/strong&gt; 🍪 &lt;/p&gt;

&lt;p&gt;In Express, we can create a cookie like 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="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/set-cookie&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="nx"&gt;req&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cookie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;username&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;shree&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//creates and sends cookie.&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;res.cookie(...)&lt;/code&gt; is a method in Express.js used to create and store cookies.&lt;br&gt;
It can take up to three parameters: &lt;code&gt;res.cookie(name, value, options)&lt;/code&gt;.&lt;br&gt;
So, while creating a cookie, we always send the cookie name and its value, and optionally some options (like expiry time, security flags, etc.). &lt;/p&gt;



&lt;p&gt;📌 &lt;strong&gt;&lt;u&gt;What the &lt;code&gt;res.cookie(...)&lt;/code&gt; doing under the hood?&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;⤷ The server defines a cookie named "username" with the value "shree".&lt;br&gt;
⤷ This cookie information is sent to the browser in the HTTP response header to be stored locally.&lt;br&gt;
⤷ Each time the user navigates to a new page, the browser automatically attaches the cookie in the HTTP request header.&lt;br&gt;
⤷ The server reads and validates it before responding again with a new HTTP response.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;But there’s a small challenge...&lt;/strong&gt;&lt;br&gt;
When the browser sends cookies back, the server receives them as raw string data like 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="nx"&gt;Cookie&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;shree&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, our Express server can’t directly read these Strings, it prefers data in JavaScript object format.&lt;br&gt;
We need a stream which can parse the cookies header data. So, we use &lt;code&gt;cookie-parser&lt;/code&gt; a npm package to parse the cookies header.&lt;/p&gt;

&lt;p&gt;You can install it like this :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i cookie-parser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use it like 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="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cookieParser&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;📌 &lt;strong&gt;&lt;u&gt;What this cookie-parser do?&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
⤷ Looks for cookies wrapped in the HTTP headers&lt;br&gt;
⤷ Parses them into a JavaScript object format&lt;br&gt;
⤷ Makes them accessible via req.cookies in Express&lt;/p&gt;



&lt;p&gt;📌 &lt;strong&gt;&lt;u&gt;Signed Cookies&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Cookies defined in the normal way (like we did before) are easily prone to tampering, since they are sent as &lt;strong&gt;plain text&lt;/strong&gt; in the browser.&lt;br&gt;
To secure them, we add another option: the signed parameter (while defining cookies), along with a &lt;strong&gt;secret key&lt;/strong&gt;, like 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="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cookieParser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;secretKey&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// add secret key&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cookie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;username&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="s2"&gt;shree&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="na"&gt;signed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// while creating cookie&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These Cookies now called &lt;strong&gt;Signed Cookies&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;📌 &lt;strong&gt;&lt;u&gt;How do Signed Cookies work?&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;⤷ When you set &lt;code&gt;{ signed: true }&lt;/code&gt;, Express uses the secret key you provided to cryptographically sign the cookie value.&lt;br&gt;
⤷ If anyone tampers with the cookie, the signature won’t match, and Express immediately detects it.&lt;/p&gt;

&lt;p&gt;In this way, signed cookies protect cookie data from tampering.  &lt;/p&gt;



&lt;p&gt;📌 &lt;strong&gt;&lt;u&gt;Stateless and Stateful protocols&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The difference between stateless and stateful protocols lies in their ability to remember browser data (like headers or sessions).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Stateful protocols&lt;/strong&gt;: Remember data across multiple page navigations.&lt;br&gt;
Example: FTP (File Transfer Protocol)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Stateless protocols&lt;/strong&gt;: Do not store or remember any data between requests.&lt;br&gt;
Example: HTTP (HyperText Transfer Protocol)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What is the reason we are discussing about it?&lt;/strong&gt;&lt;br&gt;
By default, our Express server communicates with browsers using the HTTP protocol, which is stateless.&lt;br&gt;
This means every time a user navigates to a new page, the server forgets who the user is unless we add a mechanism to &lt;strong&gt;“remember”&lt;/strong&gt; them.&lt;/p&gt;

&lt;p&gt;Our goal is to keep users logged in even after page refreshes or navigation.&lt;br&gt;
That’s where sessions come in.&lt;/p&gt;



&lt;p&gt;📌 &lt;strong&gt;&lt;u&gt;What are sessions?&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sessions allow the server to store user-specific data temporarily, making HTTP stateful.&lt;/p&gt;

&lt;p&gt;When we use sessions in Express, the server:&lt;br&gt;
⤷ Creates a unique session ID for each user&lt;br&gt;
⤷ Stores session data on the server-side (in memory or a database)&lt;br&gt;
⤷ Sends that session ID to the client in a cookie&lt;br&gt;
⤷ On future requests, the browser automatically sends that session ID cookie back&lt;br&gt;
⤷ The server then looks up the stored session data using that ID&lt;/p&gt;

&lt;p&gt;This is how users stay logged in even across multiple tabs or page refreshes.&lt;/p&gt;



&lt;p&gt;📌 &lt;strong&gt;&lt;u&gt;Cookies and Sessions Integration :&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We can enable sessions in Express like 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="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;session&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;secretkey&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage :&lt;/strong&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="nx"&gt;req&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="c1"&gt;// to access the session&lt;/span&gt;
&lt;span class="nx"&gt;req&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;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shree&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// to attach data&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;📌 &lt;strong&gt;&lt;u&gt;What does the &lt;code&gt;express-session&lt;/code&gt; middleware do?&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
⤷ Creates a unique session ID&lt;br&gt;
⤷ Stores session data on the server&lt;br&gt;
⤷ Sends that session ID to the client in a cookie&lt;br&gt;
⤷ Automatically validates the session on each request&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 Since &lt;code&gt;express-session&lt;/code&gt; already handles cookie parsing internally, we no longer need the &lt;code&gt;cookie-parser&lt;/code&gt; middleware.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;We have covered all the topics about cookies and sessions that are essential to understand and implement Authorization workflow.&lt;/p&gt;

&lt;p&gt;Now let's move to the &lt;strong&gt;last part&lt;/strong&gt; : &lt;a href="https://dev.to/shreelaxmihegde/auth-series-5-authorization-implementation-with-passportjs-362"&gt;Auth series #5: Authorization Implementation&lt;/a&gt;&lt;/p&gt;

</description>
      <category>authseries</category>
      <category>javascript</category>
      <category>authentication</category>
      <category>cookies</category>
    </item>
    <item>
      <title>Auth Series #3: Authentication implementation using Passport.js</title>
      <dc:creator>Shreelaxmi Hegde</dc:creator>
      <pubDate>Sat, 25 Oct 2025 05:12:30 +0000</pubDate>
      <link>https://forem.com/shreelaxmihegde/auth-series-3-authentication-implementation-using-passportjs-416j</link>
      <guid>https://forem.com/shreelaxmihegde/auth-series-3-authentication-implementation-using-passportjs-416j</guid>
      <description>&lt;p&gt;Previous : &lt;a href="https://dev.to/shreelaxmihegde/auth-series-2-authentication-implementation-setup-1df1"&gt;Auth Series #2: Authentication Implementation with Passport.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are two ways to implement authentication system:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Build a custom authentication system from scratch, or&lt;/li&gt;
&lt;li&gt;Use existing, battle-tested tools that simplify the process.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Among the many options available, &lt;strong&gt;Passport.js&lt;/strong&gt; stands out as one of the most popular and widely used libraries for &lt;em&gt;Node.js&lt;/em&gt; applications. It blends easily with &lt;em&gt;MongoDB&lt;/em&gt; (via Mongoose) and offers a modular structure that supports both local and third-party authentication (like Google, GitHub, or Facebook).&lt;/p&gt;




&lt;p&gt;📌 &lt;strong&gt;&lt;u&gt;Why Passport.js?&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Passport.js&lt;/em&gt; provides a clean, flexible API and powerful features that make authentication smooth and reliable:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;▹ Built-in middlewares and methods speed up development.&lt;br&gt;
▹ Uses strong &lt;strong&gt;salting&lt;/strong&gt; and &lt;strong&gt;hashing&lt;/strong&gt; techniques to protect passwords.&lt;br&gt;
▹ Works seamlessly with &lt;em&gt;MongoDB&lt;/em&gt; + &lt;em&gt;Mongoose&lt;/em&gt;.&lt;br&gt;
▹ Supports multiple authentication strategies (local, OAuth, etc.).&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;strong&gt;&lt;u&gt;3.1 Setting Up Passport.js :&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
First, install the necessary packages from npm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install passport
npm install passport-local         # Local strategy
npm install passport-local-mongoose # Mongoose plugin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The local strategy is used for authentication systems that rely on standard login fields like username, email, and password instead of third-party logins.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;3.2 Initialize Passport in your Express app:&lt;/u&gt;&lt;/strong&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="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;passport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Sets up Passport middleware&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;&lt;u&gt;3.3 Defining the User Schema :&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We’ll use &lt;em&gt;Mongoose&lt;/em&gt; to define our user schema. The &lt;code&gt;passport-local-mongoose&lt;/code&gt; plugin automatically adds username and password fields, and handles the salting and hashing of passwords behind the scenes so we don’t need to code that manually.&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;passportLocalMongoose&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;passport-local-mongoose&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;userSchema&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;Schema&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; 
   &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// user schema definitions&lt;/span&gt;
&lt;span class="nx"&gt;userSchema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;plugin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;passportLocalMongoose&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Connects schema with Passport’s helper methods&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;&lt;u&gt;3.4 Registering a New User Using Passport.js through the &lt;code&gt;/signup&lt;/code&gt; Route&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We’ve already discussed that when a user submits the signup form, the browser sends a &lt;code&gt;POST&lt;/code&gt; request to the &lt;code&gt;/signup&lt;/code&gt; route, where the server stores their credentials in the database.&lt;/p&gt;

&lt;p&gt;Here’s what the route looks like :&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="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/signup&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&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="kd"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&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;newUser&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;User&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;username&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;registeredUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;password&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;&lt;strong&gt;&lt;u&gt;What does this code do?&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
⤷ It takes all the credentials submitted by the user through the signup form.&lt;br&gt;
⤷ It creates a new user using the Mongoose User schema, which stores the &lt;code&gt;email&lt;/code&gt; and &lt;code&gt;username&lt;/code&gt;.&lt;br&gt;
⤷ The &lt;code&gt;User.register()&lt;/code&gt; method then handles password hashing, salting, and saving the final user document to the database&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;More clarification on &lt;code&gt;User.register()&lt;/code&gt; method&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;.register()&lt;/code&gt; method is provided by the &lt;code&gt;passport-local-mongoose&lt;/code&gt; plugin, which we attached while defining our User schema.&lt;/li&gt;
&lt;li&gt;It automates most of the registration process by handling the following steps internally :

&lt;ul&gt;
&lt;li&gt;Generates salt&lt;/li&gt;
&lt;li&gt;Hashes the password using that salt.&lt;/li&gt;
&lt;li&gt;stores both salt and hashed password in the User document.&lt;/li&gt;
&lt;li&gt;Saves the user in the database.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;p&gt;In this way we have successfully built an Authentication system.🥳&lt;/p&gt;

&lt;p&gt;It's time to move to another interesting and most important part : &lt;a href="https://dev.to/shreelaxmihegde/auth-series-4-understanding-cookies-and-sessions-1ajg"&gt;Cookies and Sessions in Express.js&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>authseries</category>
      <category>javascript</category>
      <category>passport</category>
      <category>express</category>
    </item>
    <item>
      <title>Auth Series #2: Authentication Implementation Setup</title>
      <dc:creator>Shreelaxmi Hegde</dc:creator>
      <pubDate>Sat, 25 Oct 2025 05:10:44 +0000</pubDate>
      <link>https://forem.com/shreelaxmihegde/auth-series-2-authentication-implementation-setup-1df1</link>
      <guid>https://forem.com/shreelaxmihegde/auth-series-2-authentication-implementation-setup-1df1</guid>
      <description>&lt;p&gt;We have covered the basic understanding of Authentication and Authorization in the &lt;a href="https://dev.to/shreelaxmihegde/auth-series-1-understanding-authentication-and-authorization-17le"&gt;Auth Series #1: Understanding Authentication and Authorization&lt;/a&gt;. Now, implement the basic setup to build Authentication system in Express.js.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;&lt;u&gt;2.1 SignUp Form&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
Our first step in building authentication is to &lt;strong&gt;register&lt;/strong&gt; a new user into our database so that the platform can provide them with their own personalized space.&lt;/p&gt;

&lt;p&gt;Let’s start with a basic signup form 👇&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;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"/signup"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"POST"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;    &lt;span class="c"&gt;&amp;lt;!-- username --&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"username"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Username : &lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"username"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"username"&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;    &lt;span class="c"&gt;&amp;lt;!-- email --&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Email : &lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

     &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;   &lt;span class="c"&gt;&amp;lt;!-- password --&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Password : &lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;button&amp;gt;&lt;/span&gt;SignUp&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*Its a simple HTML form. You can always add your own styling later.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Behind the scenes of the Form :&lt;/strong&gt;&lt;br&gt;
⤷  User enters their credentials(username, email, password).&lt;br&gt;
⤷  Clicks the "&lt;em&gt;signUp&lt;/em&gt;" button.&lt;br&gt;
⤷  The browser sends a &lt;code&gt;POST&lt;/code&gt; request to the &lt;code&gt;/signup&lt;/code&gt; route.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;2.2 Securing User Data Before Storing 🔐&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
Once we receive the user’s information from the signup form, the next step is to store it securely in the database (MongoDB).&lt;br&gt;
But since passwords are &lt;strong&gt;sensitive&lt;/strong&gt; data, we should never store them in plain text.&lt;/p&gt;

&lt;p&gt;In real-world applications, developers add 2 extra layers of protection before saving user credentials:&lt;br&gt;
▹ Salting&lt;br&gt;
▹ Hashing&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The workflow looks like this&lt;/strong&gt; 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sensitive data → Salting → Hashing → Store in database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;📌 &lt;strong&gt;&lt;u&gt;What is Salting?&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
Salting adds the first layer of security to your user’s password.&lt;br&gt;
It involves adding a unique, &lt;strong&gt;secret String&lt;/strong&gt; called a &lt;strong&gt;&lt;em&gt;salt&lt;/em&gt;&lt;/strong&gt; to the password before hashing it.&lt;br&gt;
This salt is stored securely by the server and ensures that even two users with the same password will have different hashed outputs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example:
Password: abc123
Salt: x9@#p!
Combined: abc123x9@#p!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;📌 &lt;strong&gt;&lt;u&gt;What is Hashing?&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
Hashing is a &lt;strong&gt;one-way&lt;/strong&gt; encryption process that converts readable data into a scrambled form using a mathematical algorithm.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example:
data = 'abc'
hashed = 'lajhfld#k2432blk34w3&amp;amp;alsjd'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s &lt;strong&gt;impossible&lt;/strong&gt; to reverse a hash and retrieve the original password which is what makes it secure.&lt;/p&gt;




&lt;p&gt;📌 &lt;strong&gt;&lt;u&gt;Why Both Salting and Hashing?&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
Some users choose weak passwords like 1234 or password.&lt;br&gt;
If a hacker guesses or obtains the hashing algorithm, those weak passwords become easy targets.&lt;/p&gt;

&lt;p&gt;By adding a salt before hashing, you make the final encrypted output &lt;strong&gt;unique&lt;/strong&gt; and &lt;strong&gt;unpredictable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So the final process looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Server receives password
→ Adds salt  
→ Hashes the combined string  
→ Stores the hashed result in the database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Now some questions arise&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How do we implement this system in Express.js?&lt;br&gt;
How to write the Hashing algorithm?&lt;br&gt;
How long our salt should be?&lt;br&gt;
etc. etc.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;Lets discuss about them in the Next Part : &lt;a href="https://dev.to/shreelaxmihegde/auth-series-3-authentication-implementation-using-passportjs-416j"&gt;Auth Series #3: Authentication implementation using Passport.js&lt;/a&gt;&lt;/p&gt;

</description>
      <category>authseries</category>
      <category>javascript</category>
      <category>express</category>
      <category>authentication</category>
    </item>
    <item>
      <title>Auth Series #1: Understanding Authentication and Authorization</title>
      <dc:creator>Shreelaxmi Hegde</dc:creator>
      <pubDate>Sat, 25 Oct 2025 05:09:44 +0000</pubDate>
      <link>https://forem.com/shreelaxmihegde/auth-series-1-understanding-authentication-and-authorization-17le</link>
      <guid>https://forem.com/shreelaxmihegde/auth-series-1-understanding-authentication-and-authorization-17le</guid>
      <description>&lt;p&gt;&lt;em&gt;Authentication&lt;/em&gt; and &lt;em&gt;Authorization&lt;/em&gt; are two of the most essential features in modern web applications. &lt;br&gt;
&lt;em&gt;Signup&lt;/em&gt; and &lt;em&gt;login&lt;/em&gt; systems have become common in most real-world apps from small personal projects to production-ready enterprise solutions. These mechanisms ensure that users enjoy a &lt;strong&gt;secure&lt;/strong&gt; and &lt;strong&gt;personalized&lt;/strong&gt; experience on the platform.&lt;/p&gt;

&lt;h4&gt;
  
  
  From the USER'S Perspective :
&lt;/h4&gt;

&lt;p&gt;❖ You get your own secure space within the application.&lt;br&gt;
❖ You have control over your own data and actions. No one else can access or modify them.&lt;/p&gt;

&lt;h4&gt;
  
  
  From the Developer / PLATFORM'S Perspective :
&lt;/h4&gt;

&lt;p&gt;❖ You offer users a smooth, personalized experience that builds trust.&lt;br&gt;
❖ You protect your platform's data from &lt;em&gt;unauthorized&lt;/em&gt; access or &lt;em&gt;malicious&lt;/em&gt; activity.&lt;/p&gt;

&lt;p&gt;These are the reasons that makes the Authentication and Authorization a &lt;em&gt;powerful&lt;/em&gt; feature that a web application can have.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Understanding Authentication and Authorization
&lt;/h2&gt;

&lt;h3&gt;
  
  
  📌 What is Authentication?
&lt;/h3&gt;

&lt;p&gt;Authentication is the process of &lt;em&gt;verifying the identity&lt;/em&gt; of a user. It usually involves collecting user credentials such as email, username or passwords and validating them against stored records in database.&lt;/p&gt;

&lt;p&gt;In simple terms, it's the process that answers: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Who are you?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This step is often implemented through actions like Sign Up(Register) and Sign In(Login).&lt;/p&gt;




&lt;h3&gt;
  
  
  📌 What is Authorization?
&lt;/h3&gt;

&lt;p&gt;Authorization, on the other hand, determines WHAT a user is allowed to do after the have been authenticated.&lt;/p&gt;

&lt;p&gt;It answers:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"What are you allowed to access?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For example an author of a blog post can modify or delete the post, while a regular user can only view the post.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;&lt;u&gt;How they work together in a real-world flow :&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
⤷ The user enters credentials (Sign Up/Sign in)&lt;br&gt;
⤷ The system authenticates them (verifies identity)&lt;br&gt;
⤷ The system authorizes their actions (decides what they can access or modify)&lt;/p&gt;

&lt;p&gt;Now we have the basic theoretical understanding of these two terms. &lt;br&gt;
Let's discuss about &lt;a href="https://dev.to/shreelaxmihegde/auth-series-2-authentication-implementation-setup-1df1"&gt;how it can be implemented in Express.js&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Previous : &lt;a href="https://dev.to/shreelaxmihegde/auth-series-index-building-authentication-and-authorization-in-expressjs-5hfm"&gt;Auth Series Index: Building Authentication and Authorization in Express.js&lt;/a&gt; &lt;br&gt;
Next : &lt;a href="https://dev.to/shreelaxmihegde/auth-series-2-authentication-implementation-setup-1df1"&gt;Auth Series #2: Authentication Implementation with Passport.js&lt;/a&gt;&lt;/p&gt;

</description>
      <category>authseries</category>
      <category>express</category>
      <category>javascript</category>
      <category>backend</category>
    </item>
    <item>
      <title>Auth Series Index: Building Authentication and Authorization in Express.js</title>
      <dc:creator>Shreelaxmi Hegde</dc:creator>
      <pubDate>Sat, 25 Oct 2025 05:09:26 +0000</pubDate>
      <link>https://forem.com/shreelaxmihegde/auth-series-index-building-authentication-and-authorization-in-expressjs-5hfm</link>
      <guid>https://forem.com/shreelaxmihegde/auth-series-index-building-authentication-and-authorization-in-expressjs-5hfm</guid>
      <description>&lt;p&gt;Welcome to my &lt;strong&gt;Auth Series&lt;/strong&gt;!  &lt;/p&gt;

&lt;p&gt;In this series, we explore the core concepts and practical implementation of Authentication and Authorization in &lt;strong&gt;Express.js&lt;/strong&gt;, covering everything from user login to session management and access control.&lt;/p&gt;

&lt;p&gt;We focus especially on the &lt;strong&gt;behind-the-scenes&lt;/strong&gt; workflows and the reasoning behind every step, giving you a deep understanding &lt;strong&gt;beyond just the code&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;What we'll explore:&lt;/strong&gt;&lt;br&gt;
✧ How Authentication and Authorization work in MERN based applications&lt;br&gt;
✧ Related topics that are essential to understand before implementing them (cookies, sessions, salting, hashing etc)&lt;br&gt;
✧ The tools and libraries that simplify the process&lt;br&gt;
✧ The WHY and HOW behind each step and concept.&lt;/p&gt;




&lt;h2&gt;
  
  
  We'll be breaking down the process in 5 parts.
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://dev.to/shreelaxmihegde/auth-series-1-understanding-authentication-and-authorization-17le"&gt;Auth Series #1: Understanding Authentication and Authorization&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/shreelaxmihegde/auth-series-2-authentication-implementation-setup-1df1"&gt;Auth Series #2: Authentication Implementation Setup&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/shreelaxmihegde/auth-series-3-authentication-implementation-using-passportjs-416j"&gt;Auth Series #3: Authentication implementation using Passport.js&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/shreelaxmihegde/auth-series-4-understanding-cookies-and-sessions-1ajg"&gt;Auth Series #4: Understanding Cookies and Sessions&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/shreelaxmihegde/auth-series-5-authorization-implementation-with-passportjs-362"&gt;Auth Series #5: Authorization Implementation with Passport.js&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Follow the links in order to go through the series smoothly, or jump directly to the topic you want to learn.&lt;/p&gt;

&lt;p&gt;Next : &lt;a href="https://dev.to/shreelaxmihegde/auth-series-1-understanding-authentication-and-authorization-17le"&gt;Auth Series #1: Understanding Authentication and Authorization&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;☁️⋆ ˚｡⋆ --- Happy Coding 😇 --- ⋆｡˚⋆☁️&lt;br&gt;
🌿⋆✧˚ --- Keep Learning 🌈 --- ˚✧⋆🌿&lt;/p&gt;

</description>
      <category>authseries</category>
      <category>javascript</category>
      <category>express</category>
      <category>authentication</category>
    </item>
    <item>
      <title>Cascade Deletion: What Happens to Comments When You Delete Your Post MongoDB</title>
      <dc:creator>Shreelaxmi Hegde</dc:creator>
      <pubDate>Sat, 11 Oct 2025 05:42:46 +0000</pubDate>
      <link>https://forem.com/shreelaxmihegde/cascade-deletion-what-happens-to-comments-when-you-delete-your-post-mongodb-51f5</link>
      <guid>https://forem.com/shreelaxmihegde/cascade-deletion-what-happens-to-comments-when-you-delete-your-post-mongodb-51f5</guid>
      <description>&lt;p&gt;When you delete your account or post, everything linked to it(comments, likes, followers) disappears too.&lt;/p&gt;

&lt;p&gt;But have you ever wondered what really happens behind the scenes when this deletion occurs?&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is Cascade Deletion?
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Cascade deletion&lt;/em&gt; is a common database pattern used to maintain &lt;strong&gt;data integrity&lt;/strong&gt; and &lt;strong&gt;avoid storing unnecessary data&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It ensures that when a record (like a user or post) is deleted, all associated entities (comments, likes, etc.) are also removed automatically.&lt;/p&gt;




&lt;h2&gt;
  
  
  Typical Workflow :
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;The user clicks Delete Account or Delete Post.&lt;/li&gt;
&lt;li&gt;The app shows a confirmation message: once deleted, data cannot be recovered.&lt;/li&gt;
&lt;li&gt;On confirmation, the main record (user/post) is deleted.&lt;/li&gt;
&lt;li&gt;Then, all related data connected through references are also deleted. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is the &lt;em&gt;Cascade deletion&lt;/em&gt; at work.&lt;/p&gt;




&lt;h2&gt;
  
  
  Behind the Scenes (in MongoDB)
&lt;/h2&gt;

&lt;p&gt;In databases like MongoDB, data is often spread across multiple collections that are linked together through references rather than direct relationships. Where deleting interconnected data in one go is not possible.&lt;/p&gt;

&lt;p&gt;To handle such cases, developers use a concept known as &lt;strong&gt;Cascade Deletion&lt;/strong&gt; (a logic that ensures all dependent records are removed when the parent record is deleted). &lt;br&gt;
Let’s understand this through an example:&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Example setup:&lt;/u&gt;&lt;br&gt;
Let’s say we’re building a platform where users can create posts.&lt;br&gt;
We can have 2 collections:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;users → stores user info like name, followers, etc.&lt;/li&gt;
&lt;li&gt;posts → stores post info like content, comments, etc.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, to associate users with their posts, we connect the two collections using reference IDs via Mongoose schemas:&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;userSchema&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;Schema&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
  &lt;span class="na"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Types&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Post&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// referencing the "Post" model&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 MongoDB, collections are linked by &lt;strong&gt;reference IDs&lt;/strong&gt;, not by embedding actual data.&lt;br&gt;
So, when a user is deleted, their posts don’t automatically get removed from the page or the database. They still exist, pointing to a user who no longer exists.&lt;/p&gt;

&lt;p&gt;To handle this properly, we define a &lt;em&gt;post middleware&lt;/em&gt; in Mongoose that runs after a user is deleted.&lt;br&gt;
This middleware deletes all the posts associated with that user.&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="nx"&gt;userSchema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;findOneAndDelete&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &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="o"&gt;=&amp;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;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deleteMany&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$in&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;posts&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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This middleware ensures that when a user is deleted, all of their posts are also removed from the database.&lt;/p&gt;

&lt;p&gt;This process is known as &lt;strong&gt;Cascade Deletion&lt;/strong&gt; removing all dependent records once the parent record is deleted, keeping the database clean and consistent.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why It Matters?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Keeps the database clean and efficient.&lt;/li&gt;
&lt;li&gt;Prevents orphaned records (data with no valid reference).&lt;/li&gt;
&lt;li&gt;Ensures a consistent and reliable user experience.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;In short, &lt;em&gt;Cascade deletion&lt;/em&gt; is the silent cleaner that ensures the database doesn’t hold on to what no longer exists.&lt;/p&gt;

&lt;p&gt;Thanks for reading! &lt;/p&gt;

&lt;p&gt;I’m currently building Accommate, a full-stack web app for student accommodation. It is a part of learning while I was building Rating and Comment feature to a specific housing.&lt;/p&gt;

&lt;p&gt;I'll be sharing everything I learn as I build it.&lt;/p&gt;

&lt;p&gt;If you are building something like this, I'd love to connect and share ideas.&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>javascript</category>
      <category>database</category>
    </item>
    <item>
      <title>The Magic of Client &amp; Server Validation : How Your App Knows You Typed Your Email Wrong</title>
      <dc:creator>Shreelaxmi Hegde</dc:creator>
      <pubDate>Thu, 09 Oct 2025 07:39:04 +0000</pubDate>
      <link>https://forem.com/shreelaxmihegde/the-magic-of-client-server-validation-how-your-app-knows-you-typed-your-email-wrong-2a1p</link>
      <guid>https://forem.com/shreelaxmihegde/the-magic-of-client-server-validation-how-your-app-knows-you-typed-your-email-wrong-2a1p</guid>
      <description>&lt;p&gt;Have you ever tried filling out a form? maybe logging in, signing up, or entering an OTP, and suddenly got a reminder saying, “&lt;em&gt;Please fill out this field&lt;/em&gt;” or "&lt;em&gt;Please enter valid ...&lt;/em&gt;"?&lt;/p&gt;

&lt;p&gt;It’s a small moment we experience almost every day.&lt;br&gt;
But have you ever wondered what makes your app instantly catch that missing or incorrect input?&lt;/p&gt;

&lt;p&gt;Let’s uncover what’s happening behind the scenes step by step 👇&lt;/p&gt;


&lt;h2&gt;
  
  
  &lt;u&gt;What Really Happens When You Submit a Form?&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The Workflow :&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User Form → Client Validation → Server Validation → Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  1. Client-Side Validation
&lt;/h2&gt;

&lt;p&gt;This is the first line of defense. The validation that happens right inside your browser while you’re typing or before you hit &lt;strong&gt;Submit&lt;/strong&gt;.&lt;br&gt;
Think of it like a friendly supervisor ensuring you’re entering valid details before the data leaves your screen.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can’t type random characters into an email or URL field as they follow strict patterns.&lt;/li&gt;
&lt;li&gt;You can’t enter negative numbers in a price or age field.&lt;/li&gt;
&lt;li&gt;You can’t skip required fields and the browser instantly reminds you.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of this happens instantly, without reloading the page.&lt;br&gt;
It ensures smoother user experience and directs users to fix errors immediately before data is even sent to the server.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Server-Side Validation 🛡️
&lt;/h2&gt;

&lt;p&gt;Once you click submit, the data reaches your backend and here’s where server-side validation steps in.&lt;/p&gt;

&lt;p&gt;Even though the browser already validated your input, the server can never blindly trust incoming data.&lt;br&gt;
Why? &lt;br&gt;
Because anyone can manipulate HTML or disable JavaScript using DevTools or tools like Postman.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Attackers can:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inject malicious SQL code&lt;/li&gt;
&lt;li&gt;Send malformed or unexpected data&lt;/li&gt;
&lt;li&gt;Try to upload harmful files&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To protect the system, the server revalidates and sanitizes the incoming data before storing it in the database.&lt;/p&gt;

&lt;p&gt;Libraries like &lt;strong&gt;Joi&lt;/strong&gt;(for Node.js) help define strict validation rules (the practice known as &lt;strong&gt;Schema Validation&lt;/strong&gt; in technical terms).&lt;/p&gt;

&lt;p&gt;In short, the server acts as a gatekeeper, ensuring only clean, trusted data enters your system.&lt;/p&gt;




&lt;h2&gt;
  
  
  In Summary :
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Client-side&lt;/strong&gt; validation improves user experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Server-side&lt;/strong&gt; validation ensures security and data integrity&lt;br&gt;
Together, they create a &lt;strong&gt;robust&lt;/strong&gt; and &lt;strong&gt;reliable application&lt;/strong&gt; that something every real-world system relies on, whether it’s web or mobile, regardless of the tech stack.&lt;/p&gt;




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

&lt;p&gt;This workflow may look simple, but it’s one of the core pillars that keep our apps safe and user-friendly.&lt;br&gt;
It’s fascinating how these small mechanisms silently guard every modern digital product.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;br&gt;
I hope this post helps you understand &lt;strong&gt;something new&lt;/strong&gt; today!&lt;br&gt;
If you’ve worked with validation in your own projects or have different approaches, &lt;strong&gt;I’d love to hear your thoughts&lt;/strong&gt; 👇&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>fullstack</category>
      <category>javascript</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>I Recreated Pinterest UI with Bootstrap.</title>
      <dc:creator>Shreelaxmi Hegde</dc:creator>
      <pubDate>Tue, 12 Aug 2025 14:58:32 +0000</pubDate>
      <link>https://forem.com/shreelaxmihegde/i-recreated-pinterest-ui-with-bootstrap-4i1i</link>
      <guid>https://forem.com/shreelaxmihegde/i-recreated-pinterest-ui-with-bootstrap-4i1i</guid>
      <description>&lt;p&gt;As soon as I learned &lt;em&gt;Bootstrap&lt;/em&gt;, I was drawn to its built-in classes for responsiveness, grid layout, flex utilities, cards, and navbar components. And these all features mapped naturally to the Pinterest dashboard. I chose &lt;em&gt;Pinterest&lt;/em&gt; as my target to deepen my understanding of CSS, responsiveness, and layout designs.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;&lt;u&gt;Project overview&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
Stack: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HTML &lt;/li&gt;
&lt;li&gt;CSS &lt;/li&gt;
&lt;li&gt;Bootstrap 5&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I built a responsive &lt;em&gt;Pinterest&lt;/em&gt;-like UI with a grid of images, cards, hover effects, sidebar and a simple search bar.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Screenshot preview:&lt;/strong&gt; &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyysgrp76eoxr8xq7gau5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyysgrp76eoxr8xq7gau5.png" alt="dashboard screenshot" width="800" height="425"&gt;&lt;/a&gt;&lt;/p&gt;



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



&lt;p&gt;&lt;strong&gt;&lt;u&gt;Planning the layout:&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
I started by sketching the main sections:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sidebar + Search bar&lt;/li&gt;
&lt;li&gt;A fixed Sidebar&lt;/li&gt;
&lt;li&gt;Main image grid with card components&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My plan was to use &lt;em&gt;Bootstrap&lt;/em&gt; for the overall structure (grid and breakpoints) and custom CSS for fine details (hover effects, spacing, overflow, etc).&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;1. Sidebar &amp;amp; Search bar&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
First I started with the smaller section, the &lt;em&gt;Sidebar&lt;/em&gt;.&lt;br&gt;
Used navigation icons and aligned them with built in classes. &lt;br&gt;
Used &lt;code&gt;list-unstyled&lt;/code&gt; class for to remove text decorations etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="nav-items m-3 p-3"&amp;gt;
      &amp;lt;ul class="list-unstyled"&amp;gt;
           &amp;lt;li class="nav-item p-2"&amp;gt;
               &amp;lt;a href="#"&amp;gt;&amp;lt;i class=""&amp;gt;&amp;lt;/i&amp;gt;&amp;lt;/a&amp;gt;
            &amp;lt;/li&amp;gt;
            &amp;lt;!-- list of navigation tabs--&amp;gt;
       &amp;lt;/ul&amp;gt; 
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the same way I built the horizontal &lt;em&gt;Search bar&lt;/em&gt; with user profile which occupies whole width except the vertical &lt;em&gt;Sidebar&lt;/em&gt;.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;&lt;u&gt;2. Scrolling &amp;amp; overflow issues:&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
Even though I gave &lt;code&gt;sticky-top&lt;/code&gt; property to the &lt;em&gt;Search bar&lt;/em&gt;, the page was stretching when scrolled. To fix this annoying issue I used &lt;code&gt;overflow: hidden&lt;/code&gt; style to the body tag and applied &lt;code&gt;overflow: auto&lt;/code&gt; only for the image container section.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body { 
  overflow-x: hidden; /* prevent horizontal stretch */
}     

.image-grid-container { 
  overflow: auto;    /* scroll only the grid */
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;&lt;u&gt;3. Grid &amp;amp; image layout&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Pinterest&lt;/em&gt; uses a &lt;em&gt;masonry layout&lt;/em&gt; which arranges different sized images and uses &lt;em&gt;Javascript&lt;/em&gt; for accurate calculation.&lt;br&gt;
I experimented building it with CSS grid for more simpler arrangement and chose same-sized image cards to keep layout simple and responsive. The consistent column wise image alignment made the page look more friendly.&lt;/p&gt;

&lt;p&gt;Example &lt;em&gt;Bootstrap&lt;/em&gt; column pattern:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="row g-3"&amp;gt;
  &amp;lt;div class="col-6 col-sm-4 col-md-3"&amp;gt;
    &amp;lt;!-- card with image --&amp;gt;
  &amp;lt;/div&amp;gt;
  &amp;lt;!-- repeat --&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make the Image layout responsive across all screen sizes, I used the breakpoints defined in bootstrap and adjusted columns accordingly.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;code&gt;row-cols-xl-5&lt;/code&gt; 5 image columns for extra large screens&lt;br&gt;
&lt;code&gt;row-cols-lg-4&lt;/code&gt; 4 image columns for large screens&lt;br&gt;
&lt;code&gt;row-cols-sm-2&lt;/code&gt; 2 image columns for small screens&lt;br&gt;
etc.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;4. Cards &amp;amp; hover effects&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I used Bootstrap cards for structure and then added CSS for hover effect:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.card {
  transition: transform 0.4s ease, box-shadow 0.4s ease;
}
.card:hover {
  cursor: pointer;
  transform: scale(1.05);
  box-shadow: 0 20px 40px rgba(0,0,0,0.3);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes images look like they’re popping out of the page.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;&lt;u&gt;Key Learnings&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Practical ways to combine Bootstrap utilities and custom CSS.&lt;/li&gt;
&lt;li&gt;How to deal with responsive &lt;em&gt;breakpoints&lt;/em&gt; with Bootstrap.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Debugging&lt;/em&gt; real UI issues (overflow, alignment, spacing) is invaluable.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Don’t memorize classes instead understand &lt;em&gt;layout logic&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Bootstrap vs. custom CSS styling:&lt;/strong&gt;&lt;br&gt;
Before starting the project I thought &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;If Bootstrap have ready-made classes and utilities along with responsiveness, then why do we need raw CSS? Things made easier and faster.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But after the project realized the importance of vanilla CSS. Why we still need it.&lt;/p&gt;

&lt;p&gt;I learned how to blend Bootstrap with CSS. &lt;br&gt;
While using these classes no surprise we feel lost. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How much to use Bootstrap? &lt;br&gt;
When to use CSS?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After going through this project I can suggest, &lt;br&gt;
apply only necessary Bootstrap classes first, then add custom CSS for unhandled cases. Doing the reverse will lead to conflicts.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Documentation reading&lt;/strong&gt;
Learning to find the right utility classes quickly was a challenging part. 
As Bootstrap comes with a ton of built in classes, sometimes it feels overwhelming and time consuming.
Deciding what fits the best for our layout is challenging.
I read examples and inspect Bootstrap source components. And tried to match the best one.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;&lt;u&gt;Links&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
Code: &lt;a href="https://github.com/ShreelaxmiHegde/pinterest-ui-clone" rel="noopener noreferrer"&gt;GitHub repo&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Thanks for reading.&lt;br&gt;
If you enjoyed this, feel free to &lt;strong&gt;reach out&lt;/strong&gt; and give &lt;strong&gt;valuable feedback&lt;/strong&gt;. &lt;br&gt;
Happy to &lt;strong&gt;connect&lt;/strong&gt; with &lt;em&gt;fellow devs&lt;/em&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>bootstrap</category>
      <category>beginners</category>
      <category>devjournal</category>
    </item>
    <item>
      <title>Cloning a website taught me more than tutorials did.</title>
      <dc:creator>Shreelaxmi Hegde</dc:creator>
      <pubDate>Sat, 09 Aug 2025 04:02:39 +0000</pubDate>
      <link>https://forem.com/shreelaxmihegde/cloning-a-website-taught-me-more-than-tutorials-did-3bgg</link>
      <guid>https://forem.com/shreelaxmihegde/cloning-a-website-taught-me-more-than-tutorials-did-3bgg</guid>
      <description>&lt;p&gt;I recently completed a CSS tutorial. As a part of the tutorial i built this project with the guidance of a wonderful teacher.&lt;br&gt;
I applied what I learned by building a clone of a well-known &lt;strong&gt;music app Spotify&lt;/strong&gt;. I focused on &lt;strong&gt;recreating its dashboard&lt;/strong&gt; page using HTML and pure CSS.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm80p7jp6h2mi2fvum1x5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm80p7jp6h2mi2fvum1x5.png" alt="Spotify clone project preview" width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even though the project doesn't have advanced functionality or interactive features yet, the journey taught me more than I expected. It was a valuable experience that made me realize something important:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I should stop only watching tutorials and blindly copying code. Instead, I need to apply what I learn to real-world problems.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Yes,&lt;br&gt;
&lt;em&gt;It hit me hard.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Whenever you learn something from a tutorial, take a moment to ask yourself:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Where have I seen this concept used before?&lt;br&gt;
How can I apply it to something I'm building or interested in?&lt;/p&gt;
&lt;/blockquote&gt;




&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;In web development, there are no hidden secrets.&lt;/strong&gt;
You can see everything on a website if you inspect it carefully. Most great websites start with the basics: HTML and CSS.
So, the next time you see a well-designed website, pause and think:&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;How was this layout built?&lt;br&gt;
Is this using Flexbox? Maybe this square element has a 270-degree rotation with a transition.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This type of thinking builds &lt;strong&gt;creativity&lt;/strong&gt;. Great designs are often a combination of many small and thoughtful details.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Don’t blindly code.&lt;/strong&gt; 
Break down the entire layout into smaller parts. Complete each task. One step at a time.&lt;/li&gt;
&lt;/ul&gt;




&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use browser DevTools effectively.&lt;/strong&gt; 
They're very helpful for &lt;em&gt;testing&lt;/em&gt;, &lt;em&gt;debugging&lt;/em&gt;, and &lt;em&gt;previewing&lt;/em&gt; changes. Avoid switching tabs multiple times for simple things. Use devtools instead.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, in this clone, I found that a CSS property wasn’t applying as expected. Using DevTools, I discovered another rule was overriding it and quickly fixed the issue.&lt;br&gt;
Testing flex box logics, sizing elements, animations, effects etc. become smooth with this.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Building real projects always unlocks new insights, tips, and tricks.&lt;/strong&gt;
You get to know what it feels like to be a real developer. When you run into bugs don’t run away. Face them. 
When you want to add a feature you haven’t learned yet, go learn it and build it. Never be lazy to explore new things. 
That’s how strong foundation built.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Final Thought:&lt;/strong&gt; &lt;br&gt;
Learning through real-world practice creates a deep understanding that no tutorial alone can provide. Keep building. Keep exploring. That’s the path to becoming a great developer.&lt;br&gt;
You are building a new thing. Give it a life. Put your curiosity, passion, whatever it takes. &lt;br&gt;
Stressed out? take a small break, continue.&lt;br&gt;
Embrace all the obstacles you face. &lt;br&gt;
Learn from every mistake. Enjoy the process. &lt;br&gt;
Don't forget why you started the journey. &lt;br&gt;
Happy Coding.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Let me know what you think.&lt;/strong&gt;&lt;br&gt;
Happy to hear a valuable &lt;strong&gt;feedback&lt;/strong&gt; from you.&lt;br&gt;
If you have a nice feature to add on or want to &lt;strong&gt;collaborate&lt;/strong&gt; feel free to &lt;strong&gt;reach out&lt;/strong&gt;. You are always &lt;strong&gt;welcomed&lt;/strong&gt;.&lt;br&gt;
Or you want me to &lt;strong&gt;contribute&lt;/strong&gt; to your one of the works &lt;strong&gt;I'm open&lt;/strong&gt; 🙌.&lt;/p&gt;

&lt;p&gt;&lt;a href="//github.com/ShreelaxmiHegde/spotify-ui-clone"&gt;Checkout the project&lt;/a&gt; on github.&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>webdev</category>
      <category>uidesign</category>
      <category>devjournal</category>
    </item>
    <item>
      <title>The confusing Pointers and Arrays in C++</title>
      <dc:creator>Shreelaxmi Hegde</dc:creator>
      <pubDate>Sat, 02 Aug 2025 10:07:08 +0000</pubDate>
      <link>https://forem.com/shreelaxmihegde/the-truth-about-pointers-and-arrays-in-c-that-no-one-told-me-3e4k</link>
      <guid>https://forem.com/shreelaxmihegde/the-truth-about-pointers-and-arrays-in-c-that-no-one-told-me-3e4k</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br&gt;
When I started learning C++, pointers felt like black magic especially when working with arrays. Array and pointer concepts and their flow felt confusing.&lt;/p&gt;

&lt;p&gt;While solving an array-based question, I ran into this bug:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;❗ 'sizeof' on array function parameter 'arr' will return size of 'int*' &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So I paused and went back to revise pointers and boom, things started clicking magically! Suddenly, &lt;em&gt;the flow of logic made much more sense.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Here’s everything I learned, explained in the &lt;strong&gt;simplest&lt;/strong&gt; possible way.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;What Is a Pointer?&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
A pointer is a variable that stores the memory address of another variable.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int a = 10;
int* ptr = &amp;amp;a;  // pointer variable storing address of a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;&amp;amp;a&lt;/code&gt; → gets the address of variable a&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ptr&lt;/code&gt; → stores that address&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;*ptr&lt;/code&gt; → gives the value stored at that address (dereferencing)&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;strong&gt;&lt;u&gt;Two Uses of the Asterisk *&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The symbol * is used in two ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Declaring a pointer: &lt;code&gt;int* ptr;&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Dereferencing a pointer: &lt;code&gt;cout &amp;lt;&amp;lt; *ptr;&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;strong&gt;&lt;u&gt;NULL Pointer&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A null pointer is a pointer that doesn’t point to any memory location.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;int* ptr = NULL;  // or int* ptr = 0;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NULL points to address 0x0&lt;/li&gt;
&lt;li&gt;You can’t dereference a null pointer. Doing &lt;code&gt;cout &amp;lt;&amp;lt; *ptr;&lt;/code&gt; will
result in a runtime crash.&lt;/li&gt;
&lt;li&gt;Note: Just declaring int* ptr; without initialization gives a garbage address.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;&lt;u&gt;Array as a Pointer&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;int arr[] = {1, 2, 3};&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;arr&lt;/code&gt; is actually a constant pointer to the first element (&lt;code&gt;&amp;amp;arr[0]&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;*(arr + 1)&lt;/code&gt; is the same as &lt;code&gt;arr[1]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;But remember: You cannot do &lt;code&gt;arr = &amp;amp;x;&lt;/code&gt; because array names are non-modifiable &lt;em&gt;lvalues&lt;/em&gt; (fixed memory pointers).&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;u&gt;&lt;strong&gt;Passing Arrays to Functions&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;When you pass an array to a function, it acts as a pointer. That means only the address of the first element is passed, not the whole array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void print(int arr[]) {
    // arr is actually int* inside the function
}
So sizeof(arr) will give size of int*, not the full array.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;&lt;u&gt;How to Handle It?&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Always pass the array size separately to functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void print(int arr[], int size) {
    for (int i = 0; i &amp;lt; size; i++)
        cout &amp;lt;&amp;lt; arr[i] &amp;lt;&amp;lt; " ";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;&lt;u&gt;Key Insights Where Most Beginners Get Confused:&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int a = 10;
int* ptr = &amp;amp;a;
cout &amp;lt;&amp;lt; &amp;amp;a &amp;lt;&amp;lt; " = " &amp;lt;&amp;lt; ptr; → Both print the memory address of a

cout &amp;lt;&amp;lt; a &amp;lt;&amp;lt; " = " &amp;lt;&amp;lt; *ptr; → Both print the value stored at that address → 10

*ptr = 20; → Modifies the value of a to 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;Reference Variables&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A reference is just another name for an existing variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int a = 10;
int&amp;amp; b = a;

cout &amp;lt;&amp;lt; a &amp;lt;&amp;lt; " = " &amp;lt;&amp;lt; b;  // Both print 10
a and b refer to the same memory location.
//Any change to one affects the other.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;&lt;u&gt;Pass by Reference&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can pass variables by reference to a function so that any changes affect the original value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void func(int&amp;amp; a) {
    a = 50;  // changes the actual argument
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This avoids copying the variable&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;int* ptr = &amp;amp;a;&lt;/code&gt; → pointer stores address&lt;br&gt;
&lt;code&gt;*ptr&lt;/code&gt; → gets value at that address&lt;br&gt;
&lt;code&gt;arr&lt;/code&gt; is a constant pointer to the first element&lt;br&gt;
Arrays decay into pointers in function calls&lt;br&gt;
Always pass size with arrays&lt;br&gt;
Use &lt;code&gt;NULL&lt;/code&gt; to safely initialize unused pointers&lt;br&gt;
Reference variables (&lt;code&gt;int &amp;amp;ref = var;&lt;/code&gt;) refer to the same memory&lt;br&gt;
Pass by reference reflects changes in the original argument&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Final Thoughts&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Pointers and arrays are scary at first, but once you understand what’s really happening under the hood (just memory addresses and values), it becomes easier to work with them.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you're just starting out in C++, I hope this helped you get one step closer to mastering them.&lt;/p&gt;

&lt;p&gt;Have questions or got stuck somewhere similar? Drop a comment — let’s grow together!&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>pointers</category>
      <category>beginners</category>
    </item>
    <item>
      <title>CSS Basics : Box Model, Display Property, Inline vs Block!</title>
      <dc:creator>Shreelaxmi Hegde</dc:creator>
      <pubDate>Sun, 27 Jul 2025 14:29:01 +0000</pubDate>
      <link>https://forem.com/shreelaxmihegde/css-basics-you-must-master-box-model-display-property-inline-vs-block-and-making-circles-ml8</link>
      <guid>https://forem.com/shreelaxmihegde/css-basics-you-must-master-box-model-display-property-inline-vs-block-and-making-circles-ml8</guid>
      <description>&lt;p&gt;CSS can feel tricky when you're starting out, especially when things just “&lt;strong&gt;don’t align right&lt;/strong&gt;” or “&lt;strong&gt;spacing feels off&lt;/strong&gt;.” But once you master the Box Model, understand display properties, and learn how to work with inline vs block elements, everything starts making sense.&lt;/p&gt;

&lt;p&gt;Let me walk you through the most important foundational concepts — written in beginner-friendly language and backed with practical code examples.&lt;/p&gt;




&lt;p&gt;&lt;u&gt;What is the CSS Box Model?&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;Before applying styles to a container (an element), you need to understand how it’s structured behind the scenes.&lt;/p&gt;

&lt;p&gt;Think of every HTML element as a box. This box has:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Content (like text or an image)&lt;/li&gt;
&lt;li&gt;Padding (space around the content)&lt;/li&gt;
&lt;li&gt;Border (the box edge)&lt;/li&gt;
&lt;li&gt;Margin (space between
this box and others)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s Understand With an Example:&lt;/p&gt;

&lt;p&gt;Take this simple heading:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
&amp;lt;h1&amp;gt;Hello, world!&amp;lt;/h1&amp;gt;
&amp;lt;p&amp;gt;This is a paragraph.&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even without styling, you’ll see space between &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt;, thanks to the default margin.&lt;/p&gt;

&lt;p&gt;This is because the browser treats elements like boxes with spacing already included.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Height &amp;amp; Width&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The content area has width and height.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div {
  height: 200px;
  width: 300px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In browser dev tools, this area is usually highlighted in blue.&lt;/p&gt;




&lt;p&gt;🔲&lt;strong&gt;Border&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The border wraps the content and padding — defining the container’s edge.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div {
  border: 2px solid black;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s a shorthand for:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;border-width: 2px;
border-style: solid;
border-color: black;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Padding&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Padding is the space between the content and the border.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;padding: 10px;                /* all sides */
padding: 10px 20px;           /* top-bottom | left-right */
padding: 10px 20px 30px;      /* top | left-right | bottom */
padding: 10px 20px 30px 40px; /* top | right | bottom | left */
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Margin&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Margin is the space between elements (outer spacing).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;margin: 10px;                /* all sides */
margin: 10px 20px;           /* top-bottom | left-right */
margin: 10px 20px 30px;      /* top | left-right | bottom */
margin: 10px 20px 30px 40px; /* top | right | bottom | left */
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;u&gt;Block vs Inline Elements in CSS&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;Understanding how elements behave by default is key.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Inline Elements&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Occupy only the space their content needs&lt;/li&gt;
&lt;li&gt;Can’t be given height or width&lt;/li&gt;
&lt;li&gt;Margins work horizontally only&lt;/li&gt;
&lt;li&gt;Padding may cause overlap&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Examples: span, img, button, a, input&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Block Elements&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Always start from a new line&lt;/li&gt;
&lt;li&gt;Take full horizontal width&lt;/li&gt;
&lt;li&gt;Accept all styling properties like width, height, padding, margin&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Examples: div, p, h1 to h6&lt;/p&gt;




&lt;p&gt;&lt;u&gt;The Display Property in CSS&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;This property changes how an element behaves visually.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;display: block;&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Makes an inline element act like a block element (starts on a new line).&lt;/p&gt;

&lt;p&gt;2.&lt;code&gt;display: inline;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Makes a block element behave like inline — but you lose ability to apply height, width, etc.&lt;/p&gt;

&lt;p&gt;3.&lt;code&gt;display: inline-block;&lt;/code&gt;&lt;br&gt;
Best of both worlds.&lt;br&gt;
You retain block-like properties (height, width, margin), but it behaves inline.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;&lt;u&gt;Final Takeaways&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Understanding the box model unlocks real control over layout.&lt;/p&gt;

&lt;p&gt;Inline vs block behavior affects everything from alignment to spacing.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;display: inline-block&lt;/code&gt; is your best friend when you want styling flexibility.&lt;/p&gt;

&lt;p&gt;Making shapes like circles in CSS is simpler than you think.&lt;/p&gt;




&lt;p&gt;If you're just starting with web development, mastering these fundamentals will set you apart when working on real-world projects, internships, or job interviews.&lt;/p&gt;

&lt;p&gt;Let me know what you think — and if this helped you, consider sharing it with someone who’s also learning CSS!&lt;/p&gt;




&lt;p&gt;🔗 Follow me for more beginner-friendly dev tips and real project breakdowns.&lt;/p&gt;

</description>
      <category>css</category>
      <category>frontend</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
