Dev.to Security ๐Ÿ” Cybersecurity ๐Ÿ‘ 0 ๐Ÿ“– 2 min read

Auth for Express in One Router Mount: Rotating Refresh Tokens, OAuth, and CSRF

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 Cred,

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 Cred, an open-source, adapter-based auth library for Node.js.

What you get

Mount one router and you have:

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

Setup

npm install @cred-kit/express @cred-kit/core express cookie-parser express-rate-limit mongoose nodemailer
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: '[email protected]',
    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);

Configuration is validated at startup, so a bad secret or URL fails immediately instead of on the first request.

Design decisions

Refresh token rotation with replay detection. Refresh tokens are opaque lookupId.secret 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.

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

No user enumeration. Unknown users and wrong passwords return the same error, and forgot-password always returns the same message.

Fire-and-forget audit logs. An audit-store outage can never fail a login.

Architecture

@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

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.

Limitations

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.

Try it

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.

๐Ÿ“ฐ Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes โ€” full credit and traffic to the original publisher.