← All projects

node-refresh-token-rotation

An Express + Sequelize (Postgres) API that issues short-lived JWT access tokens and rotates refresh tokens on each use, storing them per user.

hijal/node-refresh-token-rotation@ 388ae1f

Problem

Keep JWT sessions short while allowing silent renewal, and be able to invalidate a leaked refresh token.

Built with

  • Node.js
  • Express
  • Sequelize
  • PostgreSQL
  • JSON Web Tokens
  • bcrypt
  • winston

Architecture

Refresh-token rotation

Inferred
  1. Refresh cookie Client presents the token
  2. Stored-token check Matches the user's token array
  3. Rotation Removes the used token
  4. New token pair Returns renewed credentials

Route layering

Inferred

routes/open holds the unauthenticated auth endpoints; routes/protected (users, employees) is mounted behind the jwtVerify middleware.

Refresh-token reuse handling

Inferred

Because a refresh token is removed from the user's stored array when it is used, presenting an already-rotated token would not match any stored entry. The login path also rebuilds the array from the incoming cookie.

Evidence

Login signs a 1-minute access token and a 10-minute refresh token, both with APP_SECRET.

Verified

Refresh tokens are stored as a JSONB array on the users table (refresh_token column, default empty array).

Verified

Protected routes sit behind a jwtVerify middleware that verifies the Authorization bearer token and loads the user by user_key, returning 401/403/404 as appropriate.

Verified

Passwords are hashed with bcryptjs and compared with bcrypt.compare on login.

Verified

Database schema is defined through sequelize-cli migrations for the users and employees tables.

Verified

Logging uses winston with an AsyncLocalStorage store so log lines can carry a per-request id.

Verified

The 404 handler in app.js calls an undefined createError, a leftover from an express-generator scaffold.

Verified

Contribution

Sole developer

Owner-confirmed sole work

Owner-confirmed: sole, original work; not based on a specific tutorial. Express + Sequelize (Postgres) API implementing JWT refresh-token rotation with reuse detection, bcrypt password hashing, and AsyncLocalStorage-based request-id logging.