<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[MY Publications]]></title><description><![CDATA[MY Publications]]></description><link>https://nehan.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Thu, 26 Nov 2026 11:54:44 GMT</lastBuildDate><atom:link href="https://nehan.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Auth for Express in One Router Mount: Rotating Refresh Tokens, OAuth, and CSRF]]></title><description><![CDATA[This Article Shows how you can get a express auth functionality with complete production-ready features in your app easily. Just Mount credExpress.]]></description><link>https://nehan.hashnode.dev/auth-for-express-in-one-mount</link><guid isPermaLink="true">https://nehan.hashnode.dev/auth-for-express-in-one-mount</guid><category><![CDATA[node]]></category><category><![CDATA[TypeScript]]></category><category><![CDATA[authentication]]></category><category><![CDATA[Security]]></category><dc:creator><![CDATA[Nehan Ahmed]]></dc:creator><pubDate>Wed, 25 Nov 2026 07:49:28 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6b0691a70870a0474f1b03a1/2892277a-4947-43c8-a2b7-06a830a88fd4.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>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 <strong>Cred</strong>, an open-source, adapter-based auth library for Node.js.</p>
<h2>What you get</h2>
<p>Mount one router and you have:</p>
<ul>
<li>Registration with email verification</li>
<li>Password login with account lockout (5 attempts, 15 minutes)</li>
<li>Google and GitHub OAuth, implemented with <code>fetch</code> (no Passport)</li>
<li>Rotating refresh tokens with replay detection</li>
<li>Password reset, profile management, and audit logs</li>
<li>CSRF protection and rate limiting</li>
</ul>
<h2>Setup</h2>
<pre><code class="language-bash">npm install @cred-kit/express @cred-kit/core express cookie-parser express-rate-limit mongoose nodemailer
</code></pre>
<pre><code class="language-ts">import express from 'express';
import { credExpress } from '@cred-kit/express';

const app = express();
app.set('trust proxy', 1);

const cred = await credExpress({
  database: { uri: process.env.MONGO_URI! },
  email: {
    EMAIL_FROM: 'no-reply@example.com',
    BACKEND_URL: 'http://localhost:3000',
    SMTP_HOST: process.env.SMTP_HOST!,
    SMTP_USER: process.env.SMTP_USER,
    SMTP_PASS: process.env.SMTP_PASS
  },
  jwtSecret: process.env.JWT_SECRET!, // 32+ characters
  clientUrl: 'http://localhost:5173',
  backendUrl: 'http://localhost:3000'
});

app.use('/api', cred);
app.listen(3000);
</code></pre>
<p>Configuration is validated at startup, so a bad secret or URL fails immediately instead of on the first request.</p>
<h2>Design decisions</h2>
<p><strong>Refresh token rotation with replay detection.</strong> Refresh tokens are opaque <code>lookupId.secret</code> 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.</p>
<p><strong>Cookie-based sessions.</strong> The access token (a 15-minute HS256 JWT) and refresh token live in <code>httpOnly</code> cookies. The refresh cookie is path-scoped to <code>/api/auth</code> and its value is AES-256-GCM encrypted. Unsafe requests require a double-submit CSRF token.</p>
<p><strong>No user enumeration.</strong> Unknown users and wrong passwords return the same error, and forgot-password always returns the same message.</p>
<p><strong>Fire-and-forget audit logs.</strong> An audit-store outage can never fail a login.</p>
<h2>Architecture</h2>
<pre><code class="language-text">@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
</code></pre>
<p>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.</p>
<h2>Limitations</h2>
<p>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.</p>
<h2>Try it</h2>
<ul>
<li>GitHub: <a href="https://github.com/NehanAhmed/cred">github.com/NehanAhmed/cred</a></li>
<li>npm: <code>npm i @cred-kit/express</code></li>
</ul>
<p>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.</p>
]]></content:encoded></item></channel></rss>