<?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: Nehan Ahmed</title>
    <description>The latest articles on DEV Community by Nehan Ahmed (@nehan_ahmed_f4143e819061d).</description>
    <link>https://dev.to/nehan_ahmed_f4143e819061d</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%2F3444423%2F3b36c768-a947-4966-84a1-3de282aca5e0.png</url>
      <title>DEV Community: Nehan Ahmed</title>
      <link>https://dev.to/nehan_ahmed_f4143e819061d</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nehan_ahmed_f4143e819061d"/>
    <language>en</language>
    <item>
      <title>Auth for Express in One Router Mount: Rotating Refresh Tokens, OAuth, and CSRF</title>
      <dc:creator>Nehan Ahmed</dc:creator>
      <pubDate>Wed, 25 Nov 2026 07:43:04 +0000</pubDate>
      <link>https://dev.to/nehan_ahmed_f4143e819061d/auth-for-express-in-one-router-mount-rotating-refresh-tokens-oauth-and-csrf-ob8</link>
      <guid>https://dev.to/nehan_ahmed_f4143e819061d/auth-for-express-in-one-router-mount-rotating-refresh-tokens-oauth-and-csrf-ob8</guid>
      <description>&lt;p&gt;Every Express project I started began the same way: registration, email verification, login, refresh tokens, password reset, OAuth. Each time I rewrote it, and each time the details drifted. So I extracted it into &lt;strong&gt;Cred&lt;/strong&gt;, an open-source, adapter-based auth library for Node.js.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you get
&lt;/h2&gt;

&lt;p&gt;Mount one router and you have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Registration with email verification&lt;/li&gt;
&lt;li&gt;Password login with account lockout (5 attempts, 15 minutes)&lt;/li&gt;
&lt;li&gt;Google and GitHub OAuth, implemented with &lt;code&gt;fetch&lt;/code&gt; (no Passport)&lt;/li&gt;
&lt;li&gt;Rotating refresh tokens with replay detection&lt;/li&gt;
&lt;li&gt;Password reset, profile management, and audit logs&lt;/li&gt;
&lt;li&gt;CSRF protection and rate limiting&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm install @cred-kit/express @cred-kit/core express cookie-parser express-rate-limit mongoose nodemailer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;express&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'express'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;credExpress&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@cred-kit/express'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&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="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'trust proxy'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;cred&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;credExpress&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;database&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;MONGO_URI&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="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="no"&gt;EMAIL_FROM&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'no-reply@example.com'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="no"&gt;BACKEND_URL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'http://localhost:3000'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="no"&gt;SMTP_HOST&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SMTP_HOST&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="no"&gt;SMTP_USER&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SMTP_USER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="no"&gt;SMTP_PASS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SMTP_PASS&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;jwtSecret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;JWT_SECRET&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// 32+ characters&lt;/span&gt;
  &lt;span class="n"&gt;clientUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'http://localhost:5173'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;backendUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'http://localhost:3000'&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="n"&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="s1"&gt;'/api'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cred&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000&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;Configuration is validated at startup, so a bad secret or URL fails immediately instead of on the first request.&lt;/p&gt;

&lt;h2&gt;
  
  
  Design decisions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Refresh token rotation with replay detection.&lt;/strong&gt; Refresh tokens are opaque &lt;code&gt;lookupId.secret&lt;/code&gt; strings, and only the secret is stored, bcrypt-hashed. Each refresh atomically consumes the old token and issues a successor in the same family. If a consumed token is presented again, the entire family is revoked, which limits the damage of a stolen token.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cookie-based sessions.&lt;/strong&gt; The access token (a 15-minute HS256 JWT) and refresh token live in &lt;code&gt;httpOnly&lt;/code&gt; cookies. The refresh cookie is path-scoped to &lt;code&gt;/api/auth&lt;/code&gt; and its value is AES-256-GCM encrypted. Unsafe requests require a double-submit CSRF token.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No user enumeration.&lt;/strong&gt; Unknown users and wrong passwords return the same error, and forgot-password always returns the same message.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fire-and-forget audit logs.&lt;/strong&gt; An audit-store outage can never fail a login.&lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@cred-kit/express   → HTTP transport (routes, cookies, CSRF, rate limits)
@cred-kit/core      → auth rules, framework- and storage-agnostic
@cred-kit/mongo-storage / @cred-kit/smtp → default adapters
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Core talks to storage and email through small adapter interfaces, so you can replace MongoDB or SMTP with your own implementation and keep ownership of your user data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limitations
&lt;/h2&gt;

&lt;p&gt;Cred is pre-1.0, so APIs may change between minor versions. It is ESM-only, the bundled storage adapter is MongoDB only, and the rate limiter is in-memory, meaning counters are not shared across instances. CORS, helmet, and HTTPS stay with the host app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/NehanAhmed/cred" rel="noopener noreferrer"&gt;github.com/NehanAhmed/cred&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;npm: &lt;code&gt;npm i @cred-kit/express&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;MIT licensed, with 445 tests. I'd especially value feedback on the security model and the adapter API. If Cred saves you from rewriting auth, a star on the repo helps others find it.&lt;/p&gt;

</description>
      <category>node</category>
      <category>typescript</category>
      <category>opensource</category>
      <category>security</category>
    </item>
  </channel>
</rss>
