# caioxdeveloper/fastify-secure-api architecture

> Architecture diagram of the GitHub repository caioxdeveloper/fastify-secure-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/caioxdeveloper/fastify-secure-api
- Repository: https://github.com/caioxdeveloper/fastify-secure-api

## Overview

This repository is a Fastify secure API starter. Its principal workflow is seeded user credentials → rate-limited login → signed session and CSRF cookies → session-authenticated routes with permission and CSRF checks → JSON results or stable errors. The graph includes the documented health, user-list, admin-summary, and audit-ping capabilities, plus the security/configuration layers. Source excerpts show the core wiring; no separate persistent database or external service is present (users are held in memory).

Main entry points: [`src/app.ts`](https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/app.ts) (API bootstrap), [`src/modules/auth/routes.ts`](https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/modules/auth/routes.ts) (Auth routes), [`src/modules/users/routes.ts`](https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/modules/users/routes.ts) (User and admin routes), [`src/modules/users/store.ts`](https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/modules/users/store.ts) (User store)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["API runtime"]
  node_bootstrap["API bootstrap<br/>[app.ts]"]
  node_config["Environment config<br/>[env.ts]"]
  node_errors["Error responses<br/>[errors.ts]"]
end

subgraph group_identity["Identity and sessions"]
  node_auth["Auth routes<br/>[routes.ts]"]
  node_sessionplugin["Session plugin<br/>[session.ts]"]
  node_sessionlogic["Session tokens<br/>[session.ts]"]
  node_crypto["Crypto primitives<br/>[crypto.ts]"]
end

subgraph group_policy["Security policy"]
  node_rbac["Role permissions<br/>[rbac.ts]"]
  node_csrf["CSRF validation<br/>[csrf.ts]"]
end

subgraph group_users["User domain"]
  node_store[("User store<br/>[store.ts]")]
  node_userroutes["User and admin routes<br/>[routes.ts]"]
end

node_client(("API client"))

node_client -->|"sends requests"| node_bootstrap
node_bootstrap -->|"loads config"| node_config
node_bootstrap -->|"seeds users"| node_store
node_bootstrap -->|"registers plugin"| node_sessionplugin
node_bootstrap -->|"registers routes"| node_auth
node_bootstrap -->|"registers routes"| node_userroutes
node_bootstrap -->|"formats errors"| node_errors
node_auth -->|"verifies credentials"| node_store
node_auth -->|"creates token"| node_sessionlogic
node_auth -->|"checks logout"| node_csrf
node_sessionplugin -->|"verifies token"| node_sessionlogic
node_sessionplugin -->|"reads secret"| node_config
node_sessionlogic -->|"signs payload"| node_crypto
node_store -->|"hashes passwords"| node_crypto
node_userroutes -->|"lists users"| node_store
node_userroutes -->|"checks permissions"| node_rbac
node_userroutes -->|"checks mutations"| node_csrf
node_csrf -->|"compares tokens"| node_crypto
node_client -->|"logs in"| node_auth
node_client -->|"requests resources"| node_userroutes

click node_bootstrap "https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/app.ts"
click node_config "https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/config/env.ts"
click node_errors "https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/lib/errors.ts"
click node_auth "https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/modules/auth/routes.ts"
click node_sessionplugin "https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/plugins/session.ts"
click node_sessionlogic "https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/lib/session.ts"
click node_crypto "https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/lib/crypto.ts"
click node_store "https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/modules/users/store.ts"
click node_userroutes "https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/modules/users/routes.ts"
click node_rbac "https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/lib/rbac.ts"
click node_csrf "https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/lib/csrf.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_bootstrap,node_config,node_errors,node_client toneBlue
class node_auth,node_sessionplugin,node_sessionlogic,node_crypto toneAmber
class node_rbac,node_csrf toneMint
class node_store,node_userroutes toneRose
```

## Components

### API runtime

- [API bootstrap](https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/app.ts): `src/app.ts`
- [Environment config](https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/config/env.ts): `src/config/env.ts`
- [Error responses](https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/lib/errors.ts): `src/lib/errors.ts`

### Identity and sessions

- [Auth routes](https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/modules/auth/routes.ts): `src/modules/auth/routes.ts`
- [Session plugin](https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/plugins/session.ts): `src/plugins/session.ts`
- [Session tokens](https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/lib/session.ts): `src/lib/session.ts`
- [Crypto primitives](https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/lib/crypto.ts): `src/lib/crypto.ts`

### Security policy

- [Role permissions](https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/lib/rbac.ts): `src/lib/rbac.ts`
- [CSRF validation](https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/lib/csrf.ts): `src/lib/csrf.ts`

### User domain

- [User store](https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/modules/users/store.ts): `src/modules/users/store.ts`
- [User and admin routes](https://github.com/caioxdeveloper/fastify-secure-api/blob/main/src/modules/users/routes.ts): `src/modules/users/routes.ts`

### Other components

- **API client**

## Connections

- API client → API bootstrap: sends requests (evidence: `README.md`)
- API bootstrap → Environment config: loads config (evidence: `src/app.ts`)
- API bootstrap → User store: seeds users (evidence: `src/app.ts`)
- API bootstrap → Session plugin: registers plugin (evidence: `src/app.ts`)
- API bootstrap → Auth routes: registers routes (evidence: `src/app.ts`)
- API bootstrap → User and admin routes: registers routes (evidence: `src/app.ts`)
- API bootstrap → Error responses: formats errors (evidence: `src/app.ts`)
- Auth routes → User store: verifies credentials (evidence: `src/modules/auth/routes.ts`)
- Auth routes → Session tokens: creates token (evidence: `src/modules/auth/routes.ts`)
- Auth routes → CSRF validation: checks logout (evidence: `src/modules/auth/routes.ts`)
- Session plugin → Session tokens: verifies token (evidence: `src/plugins/session.ts`)
- Session plugin → Environment config: reads secret (evidence: `src/plugins/session.ts`)
- Session tokens → Crypto primitives: signs payload (evidence: `src/lib/session.ts`)
- User store → Crypto primitives: hashes passwords (evidence: `src/modules/users/store.ts`)
- User and admin routes → User store: lists users (evidence: `src/modules/users/routes.ts`)
- User and admin routes → Role permissions: checks permissions (evidence: `src/modules/users/routes.ts`)
- User and admin routes → CSRF validation: checks mutations (evidence: `src/modules/users/routes.ts`)
- CSRF validation → Crypto primitives: compares tokens (evidence: `src/lib/csrf.ts`)
- API client → Auth routes: logs in (evidence: `README.md`)
- API client → User and admin routes: requests resources (evidence: `README.md`)
