Skip to content

Auth Module

Public Summary

JWT-based authentication with secure token rotation, refresh token reuse detection, and rate-limited login.

Internal Details

Files

FileRole
auth.controller.jsHTTP handlers for login, logout, refresh
auth.service.jsAuthentication business logic
auth.routes.jsRoute definitions with middleware
auth.schema.jsZod validation schemas
user.model.jsMongoose User schema
auth.repository.jsUser data access
token.service.jsJWT sign/verify utilities

Endpoints

MethodPathAuthDescription
POST/loginPublic (rate-limited)Authenticate user
GET/logoutCookieClear tokens and invalidate session
GET/refreshCookieRotate access token via refresh token

Data Model — User

username    : String (unique, required)
password    : String (bcrypt hash)
refreshToken: [String] — array of active refresh tokens

Token Flow

Security Controls

  • Rate limiting on /login to prevent brute-force attacks.
  • Refresh token rotation: old token is replaced on each refresh call.
  • Reuse detection: if a previously-rotated token is presented, all tokens for that user are wiped — forces full re-login.
  • httpOnly, secure, sameSite cookie flags on refresh tokens.

Extension Points

  • Add roles/permissions by extending the User model and verifyAdminUser middleware.
  • Add OAuth providers by creating alternative auth strategies in the service layer.

Source Anchors

PathRelevance
apps/server/src/modules/auth/Controller, service, routes, schema, model, repository
apps/server/src/modules/auth/middleware/verifyJWT, verifyAdminUser, optionalVerifyJWT, credentials

Failure Modes

FailureBehavior
Invalid credentials401 with generic message
Expired refresh token403, client redirects to login
Token reuse detectedAll user tokens wiped, 403
Rate limit exceeded429, retry after cooldown

Student Obrok engineering documentation.