DigitalPaani runs an operations platform for water treatment: sewage, effluent and water treatment plants managed from one SaaS. The founders pitched it on Shark Tank India in 2024. Every day it is used by plant operators, client engineers, senior client staff, and DigitalPaani’s own customer success managers and super admins.
Problem
When I joined, access control was a role name returned by the login API and kept in the browser’s localStorage, unencrypted. There were no tokens. I replaced that with JWT access and refresh tokens early on, but roles were still hard-coded throughout the product.
The roadmap had a Users module to fix this properly: workspaces, user groups, roles and permissions, all configurable. Each feature could then be sold on its own or in packages, and each person would see only what they were allowed to see.
It jumped the queue when a large sale came to depend on it. Based on its pilot, the client could bring around 1,000 treatment plants onto the platform within a year, about 20 times what we ran at the time. Hard-coded roles managed by hand could not scale to that, and were not secure enough for it.
Constraints
- About a month to build the feature, then two weeks to work out the migration and run it.
- Two of the four engineers had left and the third was on another priority. I built it end to end, later handing parts to newly hired juniors.
- 300+ legacy APIs in a four-year-old system, undocumented, with role checks hard-coded inside them.
- Existing clients had to keep working without doing anything.
Decisions
A web of scopes, not a tree of roles
Workspaces and user groups relate many-to-many. A group can span several workspaces, a workspace can hold several groups, and one person can belong to many of both with a different role in each. Permissions are atomic create, read, update and delete rights on a feature: about 100 of them across some 30 features.
Roles are templates, named bundles of permissions, much like AWS IAM policies (internally we called them rules). A role applies within a scope, so read access to a feature on one plant says nothing about another plant. This is what let sales package features, and let clients build their own hierarchies after the first onboarding.
Build it in-house, under the deadline
We chose not to bring in an external authorisation library or service. With weeks rather than months, and a scope model this tangled, a third-party tool looked both expensive and uncertain to fit. A library might have worked; building the core ourselves was the pragmatic call for the deadline.
One middleware in front of every legacy route
Rewriting 300+ undocumented APIs was not an option. I read through them to find the patterns in their hard-coded role checks, then wrote one authorisation middleware that wraps every legacy route. Where an endpoint had a role check hard-coded inside it, I took the check out and refactored the endpoint while I was there, so the new permission model decided who could call it.
Permission checks as a React hook
Each gated element (a button, a table, a whole module) asks the backend through checkGlobalPermission, wrapped in a usePermissionCheck hook that decides whether it renders. A page makes at most four or five of these checks, each under about 150 ms, run in parallel and cached.
A permission tree that stays fast for power users
The admin page where roles and permissions are assigned shows the whole tree. For a senior customer success manager with access to 100 clients, that is around 1,000 permissions on one screen, and it lagged. Caching and prefetching with TanStack Query removed the redundant API calls, and that pattern became the team’s default for data fetching.
Rehearse the cutover on production data
Every existing client and user had to move onto the new model without noticing. I wrote migration scripts and rehearsed them in staging against a snapshot of production, so the real run was not the first one. On the night, the scripts ran and the servers switched over through a sequential PM2 graceful reload, one process at a time, so live requests were never cut off.
Result
- The migration of 300+ APIs took 14 days and ran overnight with zero downtime and no dropped connections, tested before morning. Clients had nothing to do, and I stayed on call through the first days of rollout.
- The 1,000-plant client signed and began onboarding.
- By the time I left, a little under six months after launch, the platform had grown from about 25 clients to more than 100, and from about 150 users to more than 1,000.
- Features could now be sold individually or in packages.
- Clients manage their own users and access, which made them more confident about their security. Customer success still onboards senior accounts, but onboarding became smoother, not just faster.
Alongside this I started the frontend design-system work with the design team, then handed it, and later frontend feature work, to the new juniors through knowledge-transfer sessions, PR reviews and shared Postman conventions.