🛡️ Security

JWT Token Management

Deep dive into JWT-based authentication.

Token Types

TokenPurposeLifetime
Access TokenAPI authentication15 min-1hr
Refresh TokenRenew access token7-30 days

Token Structure

{
  "header": { "alg": "HS256", "typ": "JWT" },
  "payload": {
    "id": "user-uuid",
    "tenantId": "tenant-uuid",
    "role": "ADMIN",
    "iat": 1709635260,
    "exp": 1709638860
  }
}

Configuration

JWT_SECRET=your-secret-key-min-32-chars
JWT_TOKEN_EXPIRATION_TIME=3600
JWT_REFRESH_TOKEN_SECRET=your-refresh-secret
JWT_REFRESH_TOKEN_EXPIRATION_TIME=604800

Token Flow

Diagram; its source is available below
Diagram source
sequenceDiagram
    Client->>API: POST /api/auth/login
    API->>API: Validate credentials
    API-->>Client: { token, refreshToken }
    Client->>API: GET /api/employee (Bearer token)
    API->>API: Verify JWT
    API-->>Client: Employee data
    Client->>API: POST /api/auth/refresh (refreshToken)
    API-->>Client: { newToken, newRefreshToken }

Security Best Practices

PracticeImplementation
Strong secretMin 256-bit secret key
Short expirationAccess: 15-60 min
Refresh rotationIssue new refresh on use
HTTPS onlyPrevent token interception
HttpOnly cookiesPrevent XSS access
Token blacklistingInvalidate on logout

Edit this page

Copyright © 2023-Present Ever Co. LTD.Docs by Sourcey