agentleFS
manishdashsharmamanishdashsharma/express-backend-skills/skills/auth-sessions/SKILL.md

auth-sessions

Skill0 starsChanged 15 days agoMITView on GitHub

Secure authentication for Express APIs — short-lived access JWT in memory, refresh token in an httpOnly cookie rotated in place with a reuse grace window and theft detection, server-side session records, email/password with verification, Google ID-token sign-in, logout and logout-everywhere, role middleware. Use when building or reviewing sign-up, sign-in, refresh, logout, sessions, JWT or OAuth login in a Node.js backend.

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    When refreshToken is null, the controller does not touch the cookie.

    line 121
  2. Distinctive:

    If a user can be disabled or roles change often, re-check against a short-TTL cached user record instead of trusting stale claims.

    line 128

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Auth & Sessions

A complete, tested implementation ships in the create-backend skill's Markdown bundles: ../create-backend/templates/common.md (src/modules/auth/ controller, routes, validations, token utils, middleware), ../create-backend/templates/mongodb.md (auth.service.js, user.model.js, session.model.js) and ../create-backend/templates/postgres.md (auth.service.js, prisma/schema.prisma). Write those files; the rules below explain them.

Model

TokenLifetimeWhere it livesReadable by JS
Access JWT15 minResponse body → client memory onlyyes (in memory, never localStorage)
Refresh JWT7 dayshttpOnly; Secure; SameSite=Strict cookie, path=/v1/authno

The client sends Authorization: Bearer <access>. On 401 it calls POST /v1/auth/refresh once (cookie rides along), then replays the original request once.

Session record

One row/document per sign-in (Mongo collection or Prisma table):

Session { id, userId, tokenHash, previousTokenHash, userAgent, ip,
          expiresAt, revokedAt, refreshedAt, createdAt, updatedAt }
  • Store only sha256(refreshToken), never the token.
  • Index userId; TTL index on expiresAt (Mongo: { expireAfterSeconds: 0 }) or a cleanup job (Postgres).
  • Session is the one collection that may skip isActive — revokedAt is its state.

Endpoints (GET/POST only)

RouteNotes
POST /v1/auth/sign-upCreate user, hash password (bcrypt, rounds from config), send verify email. Do not issue a session until verified if verification is required.
POST /v1/auth/sign-inConstant-shape error for wrong email or password: "Email or password is incorrect". Strict per-IP rate limit.
POST /v1/auth/googleVerify the Google ID token server-side with google-auth-library against GOOGLE_CLIENT_ID; find or create the user by googleId; email counts as verified.
POST /v1/auth/refreshRotate (below).
POST /v1/auth/logoutRevoke this session, clear the cookie.
POST /v1/auth/logout-allRevoke every session for the user.
GET /v1/auth/meCurrent user, safe fields only.
GET /v1/auth/sessionsList active sessions, mark the current one.

Issuing a session

const createSession = async (userId, ctx) => {
  const sessionId = randomUUID();
  const refreshToken = signRefreshToken({ userId, sessionId, jti: randomUUID() });
  await saveSession({
    id: sessionId,
    userId,
    tokenHash: hashToken(refreshToken),
    userAgent: ctx.userAgent,
    ip: ctx.ip,
    expiresAt: new Date(Date.now() + config.cookie.maxAgeMs),
  });
  return { accessToken: signAccessToken(claimsOf(user)), refreshToken };
};

Controller sets the cookie and returns only the access token and safe user fields:

res.cookie(config.cookie.refreshName, refreshToken, {
  httpOnly: true,
  secure: config.cookie.secure,
  sameSite: config.cookie.sameSite,
  path: config.cookie.path,
  maxAge: config.cookie.maxAgeMs,
});
return httpResponse(req, res, 200, responseMessage.custom('Signed in'), { accessToken, user });

Refresh rotation (in place, race-safe, theft-detecting)

const REFRESH_REUSE_GRACE_MS = 10000;
const withinGrace = (moment) => Boolean(moment) && Date.now() - moment.getTime() <= REFRESH_REUSE_GRACE_MS;

const refreshSessionService = async (rawToken, ctx) => {
  if (!rawToken) throw unauthorized('No active session');
  let payload;
  try { payload = verifyRefreshToken(rawToken); } catch { throw unauthorized('Session expired, please sign in again'); }

  const session = await findSession(payload.sid);
  if (!session || String(session.userId) !== payload.sub) throw unauthorized('Session not found');
  if (session.revokedAt || session.expiresAt <= new Date()) throw unauthorized('Session expired, please sign in again');

  const presented = hashToken(rawToken);
  const accessToken = signAccessToken(await loadClaims(session.userId));

  if (presented === session.previousTokenHash && withinGrace(session.refreshedAt)) {
    return { accessToken, refreshToken: null };
  }
  if (presented !== session.tokenHash) {
    await revokeAllSessions(session.userId);
    logger.security('Refresh token reuse detected', { userId: session.userId });
    throw unauthorized('Session invalidated, please sign in again');
  }

  const refreshToken = signRefreshToken({ userId: session.userId, sessionId: session.id, jti: randomUUID() });
  const rotated = await rotateIfCurrent(session.id, presented, {
    tokenHash: hashToken(refreshToken),
    previousTokenHash: presented,
    refreshedAt: new Date(),
    expiresAt: new Date(Date.now() + config.cookie.maxAgeMs),
  });
  return { accessToken, refreshToken: rotated ? refreshToken : null };
};

(Shown compact for reading; write it with full braces per lint rules.)

Why each piece exists:

  • Rotate in place on the same session row — the session id stays stable for "list my devices".
  • Grace window — two tabs refreshing at once both present the old token; the second gets an access token without a new cookie instead of being logged out.
  • Reuse outside the grace window means the token was stolen → revoke every session for the user.
  • rotateIfCurrent is a conditional update (where: { id, tokenHash: presented, revokedAt: null } / findOneAndUpdate with the same filter) so two concurrent rotations cannot both win.
  • When refreshToken is null, the controller does not touch the cookie.

Middleware

  • authenticate verifies the access token and sets req.user = { id, role } (plus a tenant id such as storeId when the product is multi-tenant). Missing/invalid → 401 with a generic message, logged with logger.security.
  • authorize(...roles) → 403 when the role is not allowed.
  • Tenant membership checks (tenant resolver middleware) return 404 for non-members so existence is never confirmed.
  • If a user can be disabled or roles change often, re-check against a short-TTL cached user record instead of trusting stale claims.

Rules

  • Passwords: bcrypt, rounds from config (≥ 12). Never log, return or cache a password or hash.
  • User payloads never include password, tokenHash, or private fields (phone, etc.) unless a dedicated, authorized endpoint returns them.
  • Email verify and password reset tokens are single-purpose JWTs with their own short expiry; reset revokes all sessions.
  • Credential endpoints: strict per-IP rate limit plus per-account failure counter; email sends have a cooldown and hourly cap.
  • SameSite=Strict + cookie path /v1/auth keeps the refresh cookie off every other request.
  • Logout clears the cookie with the same options it was set with.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.