Skip to main content

Authentication Module

The auth module handles user registration, login, JWT token management, email verification, and password reset flows.

Technology

  • Strategy: JWT Bearer tokens via Passport.js
  • Password Hashing: bcrypt
  • Token Types: Access token, refresh token, email verification token
  • Email: Nodemailer with Gmail OAuth2

Auth Flow

┌──────────┐     POST /auth/register     ┌──────────┐
│ Client │ ──────────────────────────▶ │ Backend │
│ │ │ │
│ │ ◀──── 201 + Verification ── │ Creates │
│ │ Email Sent │ User + │
│ │ │ Token │
│ │ POST /auth/verify-email │ │
│ │ ──────────────────────────▶ │ Verifies │
│ │ ◀──── 200 OK ────────────── │ Email │
│ │ │ │
│ │ POST /auth/login │ │
│ │ ──────────────────────────▶ │ Issues │
│ │ ◀──── Access + Refresh ──── │ JWTs │
│ │ Tokens │ │
│ │ │ │
│ │ POST /auth/refresh │ │
│ │ ──────────────────────────▶ │ Rotates │
│ │ ◀──── New Access Token ──── │ Token │
└──────────┘ └──────────┘

Endpoints

MethodPathAuthDescription
POST/auth/registerPublicRegister a new user
POST/auth/loginPublicLogin and receive JWT tokens
POST/auth/refreshRefresh TokenGet a new access token
POST/auth/verify-emailPublicVerify email with code/token
POST/auth/forgot-passwordPublicSend password reset email
POST/auth/reset-passwordPublicReset password with token
GET/auth/meBearer TokenGet current user profile

JWT Configuration

TokenSecret Env VarExpiration Env VarPurpose
Access TokenJWT_SECRETJWT_EXPIRATION_TIMEAPI request authentication
Refresh TokenJWT_REFRESH_SECRETAccess token renewal
Verification TokenJWT_VERIFICATION_SECRETEmail verification links

Guards

The backend uses NestJS guards to protect routes:

  • JwtAuthGuard — Validates the Bearer token on protected endpoints
  • Applied globally or per-controller via @UseGuards(JwtAuthGuard)
tip

Public endpoints use custom decorators to bypass the auth guard.