<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Raviraj Jariwala</title>
    <description>The latest articles on DEV Community by Raviraj Jariwala (@raviraj_jariwala).</description>
    <link>https://dev.to/raviraj_jariwala</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F912161%2Fdcc2e50c-b5ff-4429-9183-1504f00a5d4f.jpg</url>
      <title>DEV Community: Raviraj Jariwala</title>
      <link>https://dev.to/raviraj_jariwala</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/raviraj_jariwala"/>
    <language>en</language>
    <item>
      <title>What We Learned While Rebuilding RBAC for a B2B SaaS</title>
      <dc:creator>Raviraj Jariwala</dc:creator>
      <pubDate>Tue, 07 Jul 2026 08:12:37 +0000</pubDate>
      <link>https://dev.to/raviraj_jariwala/what-we-learned-while-rebuilding-rbac-for-a-b2b-saas-1ome</link>
      <guid>https://dev.to/raviraj_jariwala/what-we-learned-while-rebuilding-rbac-for-a-b2b-saas-1ome</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; A multi-instance B2B platform had 700 lines of hardcoded navigation with inline permission checks, no route-level security, and Cognito group changes requiring code deploys. We separated instance config, group-based RBAC, and per-user overrides into three distinct layers: moved navigation to the database, built a flat group-to-policy mapping, and added a two-gate route enforcement system. Access changes went from "deploy a PR" to "update a DB row."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Nobody designs a broken permission system on purpose. Ours wasn't a mistake. It was the right call, made correctly, several times in a row.&lt;/p&gt;

&lt;p&gt;With one client and a dozen users, hiding a sidebar item behind a flag isn't a shortcut. It's the correct amount of engineering. Building a permissions layer at that scale would have been solving a problem we didn't have yet.&lt;/p&gt;

&lt;p&gt;A few clients later, another feature ships with its own access rule. Another flag goes in next to the last one. Still the right call. A real RBAC system still costs more than the problem it would solve.&lt;/p&gt;

&lt;p&gt;Then a client asks for a custom navigation just for their instance. There's no layer for "per-client" anywhere in the system, so it goes in the same place everything else did. That's not laziness. It's consistency with how the codebase already works.&lt;/p&gt;

&lt;p&gt;Fast forward to a dozen clients and a hundred menu items, and the same decision, repeated at ten times the scale, isn't cheap anymore. It's a 700-line file where every item carries its own inline permission check, and shipping one more feature means finding the right spot in it, wiring up another &lt;code&gt;hasAccess()&lt;/code&gt; call, and testing everything around it before you deploy.&lt;/p&gt;

&lt;p&gt;Nothing here was ever broken by a bad decision. The system just outgrew the scale it was built for.&lt;/p&gt;

&lt;p&gt;That was us.&lt;/p&gt;




&lt;h2&gt;
  
  
  What We Were Building
&lt;/h2&gt;

&lt;p&gt;We were building a multi-instance B2B SaaS platform. Each client gets their own isolated instance. Their employees use it daily: Sales, Marketing, Content teams, Executives. Same app, completely different access needs.&lt;/p&gt;

&lt;p&gt;The product had grown to multiple modules. Each module had sub-features. Each sub-feature had its own access requirements. And underneath all of that, each client had a different subscription: some had all modules, some had three, some had two.&lt;/p&gt;

&lt;p&gt;On paper, not a complicated problem. In practice, we had made it one.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Real Problem
&lt;/h2&gt;

&lt;p&gt;When a client asked us to restrict the Reports section for a specific employee, the answer was: &lt;em&gt;"We'll need to create a new user group in Cognito, redeploy, and it'll be live in a few hours."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That's not a permission system. That's a deployment pipeline masquerading as one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The navigation file looked like this:&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;menuItems&lt;/span&gt; &lt;span class="o"&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;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Customers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;link&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/app/customers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;icon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;CustomersIcon&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;hidden&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;hasCustomerAccess&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Reports&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;link&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/app/reports&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;icon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ReportingIcon&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;hidden&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;hasReportingAccess&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Budget&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;link&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/app/budget&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;icon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;CoinsIcon&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;hidden&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;hasAccess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;budgetRoles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;BUDGET_FULL_ACCESS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
      &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;hasAccess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fullAdminRoles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FULL_ADMIN&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="c1"&gt;// ... 60 more items like this&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every item. Inline. In code. Adding a new feature meant opening this file, finding the right place, wiring up a new &lt;code&gt;hasAccess()&lt;/code&gt; call, and shipping a deploy.&lt;/p&gt;

&lt;p&gt;There were three things happening that shouldn't have been:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Access changes required code deploys.&lt;/strong&gt; Every time a group needed different access, a developer had to touch the navigation file and ship it. For a platform serving multiple clients simultaneously, this was a constant low-grade tax on engineering time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Route protection stopped at the group level.&lt;/strong&gt; Routes did call &lt;code&gt;hasAccess()&lt;/code&gt;, the same Cognito group check the sidebar used, before rendering a component for a given path. But that was the only gate. There was no way to block one specific person without pulling their entire group's access, so a one-off restriction only ever lived in the sidebar. If someone hid from the menu still knew the URL, the route let them straight in.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One-off exceptions were impossible.&lt;/strong&gt; If a client wanted their Sales Manager to see everything except the Budget module, there was no clean way to do it. Every exception became a new Cognito group, which accumulated over time into a list nobody fully understood.&lt;/p&gt;




&lt;h2&gt;
  
  
  Separating Three Things We Had Mixed Together
&lt;/h2&gt;

&lt;p&gt;The first useful thing I did was stop thinking of this as one problem.&lt;/p&gt;

&lt;p&gt;What looked like a permission problem was actually three separate concerns we had conflated into one system:&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fa7jamdiycu8e8gr865di.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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fa7jamdiycu8e8gr865di.png" alt="Three separated concerns: instance config, group-based RBAC, per-user overrides" width="800" height="487"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once I separated these three, each one had an obvious solution. The mess came from trying to solve all three in the same place.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Changed
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. The Navigation Moved to the Database
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Before: navigation hardcoded in JS&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;src/
  components/
    Sidebar/
      helpers.js       ← 700+ lines, every menu item hardcoded
      permissions.js   ← hasCustomerAccess, hasCorporateAccess...
      Icons/           ← 30+ icon files
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;After: navigation lives in the DB, code just renders it&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;src/
  core/
    ui/
      Sidebar/
        SideNav.jsx        ← fetches menu, builds tree, renders
        helpers.js         ← menuArrayToTreeWithFilter()
        components/
          StaticNav.js     ← product icons strip
          NavDrawer.jsx    ← feature list flyout
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every menu item is now a database row:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ui_config_menu
┌─────────────────┬──────────────┬─────────────────────────────────┬─────────────────┐
│ menu_identifier │ url          │ access_policies                 │ product_id      │
├─────────────────┼──────────────┼─────────────────────────────────┼─────────────────┤
│ customers_list  │ /app/cust... │ ["customer_full_access",        │ 2               │
│                 │              │  "customer_restricted_view"]    │                 │
├─────────────────┼──────────────┼─────────────────────────────────┼─────────────────┤
│ reports_main    │ /app/repo... │ ["report_full_access",          │ 1               │
│                 │              │  "reports_advance_flow_full"]   │                 │
├─────────────────┼──────────────┼─────────────────────────────────┼─────────────────┤
│ budget_overview │ /app/budg... │ ["budget_full_access",          │ 1               │
│                 │              │  "full_admin"]                  │                 │
└─────────────────┴──────────────┴─────────────────────────────────┴─────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The frontend fetches this at runtime and builds the tree dynamically:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;menuArrayToTreeWithFilter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userGroupsAndPolicies&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userCognitoData&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;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isRestricted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;restrictedMenuIdentifiers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;menu_identifier&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;isRestricted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;                                    &lt;span class="c1"&gt;// per-user block&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;hasProductAccess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userCognitoData&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;  &lt;span class="c1"&gt;// subscription check&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;hasAccess&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;access_policies&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;              &lt;span class="c1"&gt;// RBAC check&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;policy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;userGroupsAndPolicies&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;policy&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;hasAccess&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&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;Adding a new feature to the nav is now a database row. No PR. No deploy. No risk.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. A Flat Group Model, Not a Hierarchy
&lt;/h3&gt;

&lt;p&gt;I considered building a role hierarchy, where &lt;code&gt;admin&lt;/code&gt; inherits from &lt;code&gt;manager&lt;/code&gt; which inherits from &lt;code&gt;user&lt;/code&gt;. I dropped the idea quickly.&lt;/p&gt;

&lt;p&gt;For this kind of business platform, access is naturally group-shaped, not hierarchical. Sales gets sales access. Marketing gets marketing access. CEO gets everything. No inheritance chain to reason about.&lt;/p&gt;

&lt;p&gt;Instead, there's a mapping table between Cognito groups and access policies:&lt;br&gt;
&lt;/p&gt;

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

Cognito Group         →    Access Policy
─────────────────────────────────────────────
sales_team            →    customer_full_access
sales_team            →    product_restricted_view
sales_team            →    dashboard_season_view

marketing_team        →    customer_non_restricted_view
marketing_team        →    social_full_access
marketing_team        →    dashboard_full_access

exec_team             →    full_admin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At login, Cognito groups get expanded into a flat list of all access policies the user holds:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;setUserGroups&lt;/span&gt; &lt;span class="o"&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;userGroupAccessList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cognitoGroups&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getCurrentUser&lt;/span&gt;&lt;span class="p"&gt;()?.&lt;/span&gt;&lt;span class="nx"&gt;userGroups&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

  &lt;span class="c1"&gt;// expand each group into its access policies&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;accessPolicies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
  &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;groupAccessByUserGroup&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;group&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;cognitoGroups&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;group&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;accessPolicies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;groupAccessByUserGroup&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;group&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;access_policy&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="c1"&gt;// store the full expanded list in session&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;userGroups&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;([...&lt;/span&gt;&lt;span class="nx"&gt;cognitoGroups&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;accessPolicies&lt;/span&gt;&lt;span class="p"&gt;])]&lt;/span&gt;
  &lt;span class="nf"&gt;setUser&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every access check from that point uses this expanded list. The indirection matters: if we want to change what the sales team can access, we update the mapping table. No Cognito change. No code change.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Two Gates on Every Route
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Before: routes only had one gate, the group check&lt;/strong&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fde9ssslq5jbgir8ti12w.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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fde9ssslq5jbgir8ti12w.png" alt="Before: routes only had one gate, the group check" width="303" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;After: routes have two gates, group check and per-user deny-list&lt;/strong&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbkmvq0fye4p79envjdau.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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbkmvq0fye4p79envjdau.png" alt="After: routes have two gates, group check and per-user deny-list" width="608" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;RestrictedRoute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;accessList&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pathname&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;gatsbyUserData&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;=&lt;/span&gt; &lt;span class="nf"&gt;useAuthContext&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;urlPathMap&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useSidebarContext&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

  &lt;span class="c1"&gt;// Gate 1: group access&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;hasAccess&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;hasValidAccess&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;accessList&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;gatsbyUserData&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;userGroups&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
    &lt;span class="na"&gt;namesOfAccessList&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;accessList&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;

  &lt;span class="c1"&gt;// Gate 2: admin deny-list&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isRestricted&lt;/span&gt; &lt;span class="o"&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;restricted_menu_identifiers&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;urlPathMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;]?.&lt;/span&gt;&lt;span class="nx"&gt;menuIdentifier&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="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;hasAccess&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;isRestricted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;AccessDenied&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both gates have to pass. The first checks group membership. The second checks whether an admin has explicitly blocked this specific item for this specific user.&lt;/p&gt;




&lt;h3&gt;
  
  
  4. Per-User Exceptions Without Engineering
&lt;/h3&gt;

&lt;p&gt;For the edge cases, where one specific person needs a different scope than their group, we built an admin UI. It shows the user's accessible menu as a tree. An admin checks or unchecks items. Changes save immediately.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Manage User Access: John Smith (Sales Team)
─────────────────────────────────────────────
☑ Customers
  ☑ Customer List
  ☑ Customer Details
  ☑ Import
☑ Products
  ☑ Tickets
  ☐ Hospitality        ← unchecked by admin
  ☑ Merchandise
☑ Dashboard
  ☐ Revenue Flow       ← unchecked by admin
  ☑ Season View
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is purely a deny-list. It can only restrict, never grant beyond what the group allows. That constraint is intentional: if it could grant extra access, you'd have two systems to keep in sync instead of one.&lt;/p&gt;

&lt;p&gt;What used to be a Cognito console operation involving an engineer is now a ten-second admin action.&lt;/p&gt;




&lt;h2&gt;
  
  
  Before vs After
&lt;/h2&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fpmux0jfc64t6hg3knorr.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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fpmux0jfc64t6hg3knorr.png" alt="Before vs after summary: access change, new feature nav, route protection, per-user scope, audit access" width="800" height="425"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Decisions I'd Make the Same Way Again
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Keeping subscription separate from RBAC.&lt;/strong&gt; Product availability is instance configuration, answered at provisioning and doesn't change. Mixing it into the permission system would have coupled two concerns with completely different owners and change frequencies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deny-list over per-user allow-list.&lt;/strong&gt; An allow-list creates a maintenance problem: you'd need to keep it in sync with group access as groups evolve. A deny-list composes cleanly on top. Edge cases are almost always about restricting, not granting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Client-side menu filtering.&lt;/strong&gt; The menu builds and filters in the browser. The real security boundary is the API layer. For an internal platform with controlled users this is the right call. The sidebar is a UX concern, not a security perimeter.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I'd Tell Someone Starting This
&lt;/h2&gt;

&lt;p&gt;The hardest part wasn't the implementation. It was resisting the urge to solve all three concerns (subscription, group access, individual exceptions) in one system.&lt;/p&gt;

&lt;p&gt;When access control feels messy, it's usually because these three things have been layered on top of each other in the same place over time. Nobody designed it that way. It accumulated.&lt;/p&gt;

&lt;p&gt;Separating them, even if the resulting code feels like more pieces, makes every individual decision obvious. You always know which layer you're working in and why.&lt;/p&gt;

&lt;p&gt;The complexity doesn't disappear. It just gets organized.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>architecture</category>
      <category>security</category>
      <category>saas</category>
    </item>
    <item>
      <title>What I learned refactoring a codebase with a 7,558-line constants file"</title>
      <dc:creator>Raviraj Jariwala</dc:creator>
      <pubDate>Sun, 21 Jun 2026 14:37:24 +0000</pubDate>
      <link>https://dev.to/raviraj_jariwala/what-i-learned-refactoring-a-codebase-with-a-7558-line-constants-file-2abg</link>
      <guid>https://dev.to/raviraj_jariwala/what-i-learned-refactoring-a-codebase-with-a-7558-line-constants-file-2abg</guid>
      <description>&lt;p&gt;If you've ever opened a file to fix a bug and spent forty minutes just figuring out where the bug lives, this post is for you.&lt;/p&gt;

&lt;p&gt;Not because the bug was hard. Because the code had no home.&lt;/p&gt;

&lt;p&gt;I've been the sole frontend developer on a sports data platform serving 5+ professional sports clubs. I've worked on this codebase for a few years now, across everything on the frontend — auth flows, build configuration, product features, UI design implementation. When I joined, there were other developers on the team. By the time this refactor started, it was just me.&lt;/p&gt;

&lt;p&gt;One of the things I was working with was a file called &lt;code&gt;constant.js&lt;/code&gt; with &lt;strong&gt;7,558 lines&lt;/strong&gt; in it. That number tells you everything you need to know about the state of things.&lt;/p&gt;

&lt;p&gt;This is what I did about it, the decisions I made along the way, and what I'd share with anyone facing something similar.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; I restructured a 6-product React/Gatsby codebase — 1,100+ files, a 7,558-line constants file, 30-minute builds — into a clean module architecture, fixed two auth bugs affecting real users, and shipped the whole thing without pausing feature delivery.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  How codebases get this way
&lt;/h2&gt;

&lt;p&gt;It's worth starting here because I think developers are too quick to blame the people who came before them.&lt;/p&gt;

&lt;p&gt;Nobody writes a 7,558-line constants file on purpose.&lt;/p&gt;

&lt;p&gt;It starts as a place to put a few shared values. Then one developer adds a constant. Another adds five more. A deadline hits, someone adds twenty at the bottom and ships. Nobody sounds an alarm when a constants file hits 500 lines. Or 1,000. Over years, across multiple developers with different styles, that file becomes something nobody fully understands and everyone is afraid to touch.&lt;/p&gt;

&lt;p&gt;That's exactly what happened here.&lt;/p&gt;

&lt;p&gt;The platform is a React + Gatsby SaaS app for professional sports organisations. 6 products in one codebase: broadcast analytics, social media tracking, news sentiment, customer intelligence, deal management, revenue forecasting.&lt;/p&gt;

&lt;p&gt;When the project started, the team was moving fast on an early-stage product. That's the right call at that stage. You ship, you figure out architecture later.&lt;/p&gt;

&lt;p&gt;The problem is "later" kept getting pushed back. By the time I joined, there were a few developers' worth of different coding styles layered on top of each other with no consistent structure.&lt;/p&gt;

&lt;p&gt;Everything lived in &lt;code&gt;src/components/&lt;/code&gt; and &lt;code&gt;src/components/pages/&lt;/code&gt;. No grouping by product, no grouping by feature. Just files, alphabetically, forever.&lt;/p&gt;

&lt;p&gt;And honestly? I followed the same pattern when I joined. You do. You're onboarding into an existing codebase, you match what's already there.&lt;/p&gt;

&lt;p&gt;Then the product matured, the client base grew, and what used to be manageable became painful to work in every single day.&lt;/p&gt;




&lt;h2&gt;
  
  
  What had actually accumulated
&lt;/h2&gt;

&lt;p&gt;Here's the concrete picture. Before any refactoring, the codebase had:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;constant.js&lt;/code&gt; with &lt;strong&gt;7,558 lines&lt;/strong&gt;, a single file holding every constant the app had ever needed&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;utils.js&lt;/code&gt; with &lt;strong&gt;1,115 lines&lt;/strong&gt;, every helper function all in one place&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;src/components/&lt;/code&gt; with &lt;strong&gt;1,100+ files&lt;/strong&gt;, no grouping by product or feature&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;3 unused npm packages&lt;/strong&gt; still being compiled on every build (&lt;code&gt;@material-ui/core&lt;/code&gt;, &lt;code&gt;@material-ui/pickers&lt;/code&gt;, &lt;code&gt;apollo-boost&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;2 login bugs&lt;/strong&gt; affecting real users&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;30+ minute build times&lt;/strong&gt; on every deploy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those two login bugs are worth explaining because they were the most user-facing pain.&lt;/p&gt;

&lt;p&gt;The first: after your first login, changing the URL from the address bar logged you out. The post-login redirect logic was conflicting with the router state, and navigating away triggered it.&lt;/p&gt;

&lt;p&gt;The second: random mid-session logouts. Picture a club analyst mid-presentation, pulling up a live report, and suddenly staring at the login screen. The cause was the idle timer living in component state. Every re-render was resetting it, creating a race condition that eventually fired the logout.&lt;/p&gt;

&lt;p&gt;And on top of all this, there was the UI situation.&lt;/p&gt;

&lt;p&gt;Some clients were still on the old version. Others had been migrated to the new one. So at any point in time there were three things running:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The legacy UI (still live for some clients)&lt;/li&gt;
&lt;li&gt;The new UI (live for others, actively getting features)&lt;/li&gt;
&lt;li&gt;A refactoring branch sitting on top of the new UI&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Three states of the same codebase. Simultaneously. Picture trying to hold all three clearly in your head every morning before writing a single line of code. New features couldn't pause. Clubs were using this every day.&lt;/p&gt;




&lt;h2&gt;
  
  
  The call that started the refactor
&lt;/h2&gt;

&lt;p&gt;I had a conversation with the client and laid it out plainly.&lt;/p&gt;

&lt;p&gt;The codebase was slowing everything down. Debugging was slow, builds were slow, adding features was getting harder because nothing had a clear home. The ask was simple: let me restructure while still delivering features. No freeze, no "we'll pause and clean up."&lt;/p&gt;

&lt;p&gt;They agreed. And then came the harder part: figuring out how to actually do it.&lt;/p&gt;




&lt;h2&gt;
  
  
  The approach
&lt;/h2&gt;

&lt;p&gt;The temptation with a refactor like this is to fix everything in one big branch. I considered it. It would have been a mistake. It ends in a merge conflict nightmare that never ships.&lt;/p&gt;

&lt;p&gt;The constraint here, that features couldn't pause, was actually a gift. It forced the refactor into pieces small enough to be independently merged. One product per PR.&lt;/p&gt;

&lt;p&gt;Discover first, then Social, News, Broadcast, Deal Management, Media Hub. Six PRs, each self-contained, each mergeable without the others being finished.&lt;/p&gt;

&lt;p&gt;But before moving a single file, the target structure needed to be clear.&lt;/p&gt;

&lt;p&gt;Picture opening your editor on the old codebase and seeing this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Before:&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;src/
  components/          (1,100+ files, no product grouping)
  components/pages/    (same problem)
  utils/
    constant.js        (7,558 lines)
    utils.js           (1,115 lines)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No labels. No ownership. Every file looks the same regardless of which of the 6 products it belongs to.&lt;/p&gt;

&lt;p&gt;Here's what the same codebase looks like after:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;After:&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;src/
  core/                shared across 2+ products
    ui/                reusable UI components
    utils/             split by domain (dates, numbers, charts)
    constants/         split by domain
    hooks/
    services/
    auth/
    features/          global features with their own API calls
  modules/
    airwave/           broadcast analytics
    reach/             social media tracking
    headlines/         news sentiment
    compass/           customer intelligence
    pipeline/          deal management
    studio/            media hub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see at a glance which product a file belongs to. You can see what's shared and what isn't.&lt;/p&gt;

&lt;p&gt;The rule for &lt;code&gt;src/core/&lt;/code&gt; is simple: if more than one product needs it, it lives here. Nothing product-specific.&lt;/p&gt;

&lt;p&gt;The rule for &lt;code&gt;src/modules/{name}/&lt;/code&gt; is visual: the folder structure mirrors the URL. If the route is &lt;code&gt;/app/airwave/insights/overview&lt;/code&gt;, the file lives at &lt;code&gt;src/modules/airwave/pages/insights/Overview/&lt;/code&gt;. You read the URL, you know exactly where to look.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;src/core/features/&lt;/code&gt; is for global features that span products or don't belong to any one of them: things with their own API calls, side effects, and state.&lt;/p&gt;

&lt;p&gt;The last piece before starting was adding path aliases to &lt;code&gt;jsconfig.json&lt;/code&gt;: &lt;code&gt;@core/*&lt;/code&gt;, &lt;code&gt;@modules/*&lt;/code&gt;, and product-specific aliases for the most complex ones.&lt;/p&gt;

&lt;p&gt;This sounds small. It's probably the highest-leverage change in the whole refactor. No more &lt;code&gt;../../../components/something&lt;/code&gt;. Every import tells you where the file lives.&lt;/p&gt;




&lt;h2&gt;
  
  
  The mechanics of moving 1,100+ files
&lt;/h2&gt;

&lt;p&gt;This is where things actually go right or wrong, so it's worth being specific.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step one: import conversion.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I wrote a script to convert relative imports to absolute imports using the new path aliases. The important thing was running it one feature folder at a time, not across the whole codebase in a single pass.&lt;/p&gt;

&lt;p&gt;If something broke, the blast radius was one feature. Fix it, verify, move to the next folder. Simple but it saved a lot of pain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step two: file moves.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;VS Code's built-in file move handles most import updates automatically. For everything else, &lt;code&gt;git mv&lt;/code&gt; in the terminal.&lt;/p&gt;

&lt;p&gt;This matters more than it sounds. &lt;code&gt;git mv&lt;/code&gt; tells git the file moved rather than treating it as a deletion and a new file. After the migration, &lt;code&gt;git blame&lt;/code&gt; and &lt;code&gt;git log --follow&lt;/code&gt; still work. You can still trace why a line was written two years ago by someone no longer on the team.&lt;/p&gt;

&lt;p&gt;In a codebase without a test suite, that history is one of the few tools you have for understanding why something was done a certain way. Worth protecting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What each PR looked like:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Every product PR ended the same way. The last few commits were always: organise product-specific constants into the module, pull anything shared into &lt;code&gt;src/core/&lt;/code&gt;, and break down any file over 500 lines into smaller, readable pieces.&lt;/p&gt;

&lt;p&gt;Not perfectly small. Just below 500 and coherent enough to open without getting lost.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The hardest part:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The deal management module was the most difficult. It felt like rearranging a room while someone was still living in it. That product was being actively changed based on client feedback at the same time I was restructuring its files. Every day there was a new conflict to resolve between the refactoring branch and what was landing on the main branch.&lt;/p&gt;

&lt;p&gt;Honestly, I thought about skipping the deal management module entirely and coming back to it later. Instead I skipped individual files within it when the conflict risk wasn't worth it, noted them, and moved on. A partial migration that ships cleanly beats a complete one that breaks something in production.&lt;/p&gt;




&lt;h2&gt;
  
  
  Fixing the bugs
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Login bug one (URL change logging users out):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The post-login redirect logic was conflicting with the router state. Navigating via the address bar after first login triggered the logout flow.&lt;/p&gt;

&lt;p&gt;Fixed by simplifying: always redirect to the home route on login, then let the router handle navigation from there. One clear responsibility, no conflicts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Login bug two (mid-session logouts):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The idle timer moved from component state to &lt;code&gt;useRef&lt;/code&gt;. No more re-renders, no more race condition.&lt;/p&gt;

&lt;p&gt;User data fetching changed from sequential to concurrent. And localStorage cleanup on logout was scoped to only the specific keys that needed clearing, instead of wiping everything.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The build time:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Removing &lt;code&gt;@material-ui/core&lt;/code&gt;, &lt;code&gt;@material-ui/pickers&lt;/code&gt;, and &lt;code&gt;apollo-boost&lt;/code&gt; from &lt;code&gt;package.json&lt;/code&gt; was the bulk of the improvement. They were being compiled on every build and hadn't been used in a long time.&lt;/p&gt;




&lt;h2&gt;
  
  
  What changed
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;3,900+ files touched&lt;/strong&gt; across 74 commits&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Around 18,000 net lines removed&lt;/strong&gt; (284k deleted, 265k added)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build time: 30+ minutes → 17-18 minutes&lt;/strong&gt;, just from removing dead weight&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Both login bugs resolved&lt;/strong&gt;, no reports since&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;All 6 products&lt;/strong&gt; with their own clearly bounded home in the codebase&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No client disruption&lt;/strong&gt; throughout, legacy and new UI both stayed live&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What I'd do differently
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Set up the structure before anyone writes code against the old one.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The hardest part of this refactor wasn't the architecture decisions. It was migrating files while new code was still landing in the old locations. If the module structure and path aliases exist from the start, people move their own code naturally. Running a large migration alongside active development adds friction that didn't need to be there.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Add ESLint rules early.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Right now the conventions live in documentation. That works while I'm the only developer. But the moment anyone else touches the codebase under pressure, the first thing they'll do is import from the familiar old path.&lt;/p&gt;

&lt;p&gt;A linting rule that fails the build enforces the architecture without relying on anyone's memory. You set it once, it quietly holds the line for you after that. Two lines of config, permanent guardrails.&lt;/p&gt;




&lt;h2&gt;
  
  
  The thing worth remembering
&lt;/h2&gt;

&lt;p&gt;Messy codebases aren't created by bad developers.&lt;/p&gt;

&lt;p&gt;They're created by good developers solving the right problems at the right time, without the bandwidth to also clean up behind themselves. The mess accumulates quietly, one reasonable decision at a time, until one day it feels heavy enough to slow everything down.&lt;/p&gt;

&lt;p&gt;The lesson isn't to plan everything perfectly from day one. That's not realistic when a product is still finding its shape.&lt;/p&gt;

&lt;p&gt;The lesson is to recognise when the codebase needs to grow up along with the product, and to make that investment before the cost gets too high.&lt;/p&gt;

&lt;p&gt;Looking back, the hardest part wasn't technical. It was doing this alongside active feature delivery without letting either side slip. Almost every win came from removing things, not adding them. The faster build came from deleting dead packages. The auth reliability came from moving a variable to the right place. The debuggability came from splitting nearly 9,000 lines of monolithic files into files that know what they are.&lt;/p&gt;

&lt;p&gt;If your codebase feels like this right now, you don't need the perfect plan before you start. Pick one product, one module, one folder. Clean it up. Ship it. Then do the next one.&lt;/p&gt;

&lt;p&gt;That's really the whole approach.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
