Overview
KilomeTracker uses a Backend for Frontend (BFF) pattern. Every file atsrc/app/api/**/route.js is a server-side proxy handler — page components never call the external backend directly. All client-side fetch() calls go to /api/* routes on the same Next.js origin.
API_BASE_URL is a server-side environment variable. It is never exposed to the browser. Set it in .env.local:How each proxy route works
Read the JWT from the HTTP-only cookie
The
bffFetch helper reads the token cookie using Next.js cookies(). Because the cookie is httpOnly, browser JavaScript cannot access it — only server-side code can.Return 401 if no token is present
If the request arrives without a valid cookie (unauthenticated user),
bffFetch returns a 401 response immediately without making a network call to the backend.Forward the request to the backend
The proxy attaches the token as a
Bearer header and forwards the request — including the original HTTP method, body, and any additional headers — to API_BASE_URL.bffFetch helper (src/lib/backendFetch.ts) encapsulates steps 1–3 and is used by all proxy routes except login and logout, which manage the cookie directly.
API route structure
All proxy routes live undersrc/app/api/.
| Route group | Endpoints |
|---|---|
api/auth/ | login, logout, register, me, updateprofile, updatepassword |
api/auth/users/ | List users (GET); get, deactivate, reactivate a user by ID |
api/auth/users/[id]/role/ | Update user role (PUT) |
api/vehicles/ | List and create vehicles |
api/vehicles/[alias]/ | Get, update, deactivate a vehicle by alias |
api/vehicles/[alias]/stats/ | Comprehensive vehicle statistics |
api/vehicles/[alias]/fuel-efficiency/ | Fuel efficiency metrics |
api/vehicles/[alias]/reactivate/ | Reactivate a deactivated vehicle |
api/routes/ | List, create, get, update, delete routes |
api/refuels/ | List, create, get, update, delete refuels |
api/refuels/vehicle/[alias]/analysis/ | Fuel analysis for a specific vehicle |
api/maintenance/ | List and create maintenance records |
api/maintenance/[id]/ | Get, update, delete a maintenance record |
api/maintenance/upcoming/ | Maintenance due soon |
api/expenses/ | List and create expenses |
api/expenses/[id]/ | Get, update, delete an expense |
api/expenses/summary/ | Aggregated spending by category |
api/expenses/upcoming/ | Recurring expenses due in the next 30 days |
Client-side fetch pattern
All page components use"use client" and call /api/* routes using the browser’s fetch API. They never reference API_BASE_URL or call the external backend directly.