<?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: Juma Paul</title>
    <description>The latest articles on DEV Community by Juma Paul (@juma-paul).</description>
    <link>https://dev.to/juma-paul</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%2F3885536%2F746922b7-01e9-4f44-b64b-cc0e9e51223b.jpeg</url>
      <title>DEV Community: Juma Paul</title>
      <link>https://dev.to/juma-paul</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/juma-paul"/>
    <language>en</language>
    <item>
      <title>I Built a Self-Hosted Auth System Like Clerk — Here's What I Learned</title>
      <dc:creator>Juma Paul</dc:creator>
      <pubDate>Sat, 18 Apr 2026 05:55:10 +0000</pubDate>
      <link>https://dev.to/juma-paul/i-built-a-self-hosted-auth-system-like-clerk-heres-what-i-learned-fnc</link>
      <guid>https://dev.to/juma-paul/i-built-a-self-hosted-auth-system-like-clerk-heres-what-i-learned-fnc</guid>
      <description>&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%2Fm8r4h21i61e2va8t7cnw.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%2Fm8r4h21i61e2va8t7cnw.png" alt=" " width="800" height="563"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every serious project needs authentication. As someone early in my career, I kept running into the same problem: register, login, email verification, password reset, and OAuth. Every. Single. Project. I’d used Clerk before, and it’s excellent, but I started wondering: how does it actually work under the hood? That question turned into AuthKit — a self-hosted, multi-tenant identity API I now use across all my projects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Problem&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I’d used Clerk on a previous project, and it felt like magic — you drop in a few components, and suddenly you have login, OAuth, user management, all of it. But magic made me uncomfortable. I didn’t understand what was happening underneath. How does token rotation work? How do you securely store sessions? How do services like Clerk isolate thousands of customers on the same infrastructure?&lt;/p&gt;

&lt;p&gt;The only way I knew how to answer those questions was to build it myself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Solution: AuthKit&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I built a self-hosted, multi-tenant identity API that I deploy once and can connect to any project. Each application registers as a tenant, receives its own API key, and gets its own isolated identity stack — OAuth, 2FA, email verification, password resets, account deletion and restoration, everything you’d expect from a modern auth platform.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Technical Highlights&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;Multi-Tenancy&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each request includes a tenant API key&lt;/li&gt;
&lt;li&gt;Every database query is automatically scoped  to that tenant&lt;/li&gt;
&lt;li&gt;Two apps can have users with the same email address while remaining completely isolated — no separate infrastructure required&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;em&gt;Security layers&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;httpOnly cookies for XSS protection&lt;/li&gt;
&lt;li&gt;Short-lived access tokens (15 minutes)&lt;/li&gt;
&lt;li&gt;7-day refresh tokens that rotate on every use and are stored server-side&lt;/li&gt;
&lt;li&gt;Bcrypt-hashed passwords&lt;/li&gt;
&lt;li&gt;CSRF-safe OAuth flows&lt;/li&gt;
&lt;li&gt;Rate-limited endpoints&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;em&gt;Token Refresh (the hardest part)&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The real challenge wasn't the backend — it was coordinating refresh logic on the frontend&lt;/li&gt;
&lt;li&gt;When several requests fail at once, each attempts to trigger a refresh, leading to race conditions and invalid tokens&lt;/li&gt;
&lt;li&gt;AuthKit solves this by queueing failed requests, performing a single refresh, then replaying everything with the new token&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;em&gt;JWT rotation&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Access tokens are short-lived; refresh tokens rotate on every use&lt;/li&gt;
&lt;li&gt;Each rotation invalidates the previous token and issues a new one&lt;/li&gt;
&lt;li&gt;Refresh tokens are stored in the database and revoked on use&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The Frontend&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I built a reference client in Next.js to prove the system works end‑to‑end. It handles automatic token refresh, shows clear feedback when rate limits are hit, and uses a mobile‑first responsive layout. The 2FA setup supports both QR codes and manual entry.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I Learned&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Building AuthKit taught me why services like Clerk and Auth0 make the choices they do. Error codes matter a lot — distinguishing &lt;em&gt;TOKEN_EXPIRED&lt;/em&gt; from &lt;em&gt;UNAUTHORIZED&lt;/em&gt;, for example, lets the frontend decide whether to refresh silently or redirect to the login page. Toast timing matters — if a toast persists across navigations, the user sees duplicate error messages, and the UI feels chaotic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Try It Yourself&lt;/strong&gt;&lt;br&gt;
The full source is on GitHub. If you're learning auth or want a starting point for your own projects, give it a spin.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://authkit-demo-six.vercel.app" rel="noopener noreferrer"&gt;Live Demo&lt;/a&gt; &lt;br&gt;
  &lt;a href="https://github.com/juma-paul/authkit" rel="noopener noreferrer"&gt;Backend Repo&lt;/a&gt; &lt;br&gt;
  &lt;a href="https://github.com/juma-paul/authkit-demo" rel="noopener noreferrer"&gt;Frontend Repo&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;If it helps, a star would mean a lot!&lt;/p&gt;

</description>
      <category>authentication</category>
      <category>webdev</category>
      <category>softwareengineering</category>
      <category>node</category>
    </item>
  </channel>
</rss>
