Skip to content

๐Ÿ”‘ Authentication โ€‹

KitchenAsty uses JWT (JSON Web Tokens) for stateless authentication.

โš™๏ธ Configuration โ€‹

dotenv
JWT_SECRET=your-random-secret-here
JWT_EXPIRES_IN=7d
  • ๐Ÿ” JWT_SECRET โ€” Used to sign and verify tokens. Must be a strong, random string in production.
  • โฑ๏ธ JWT_EXPIRES_IN โ€” Token lifetime. Accepts values like 7d, 24h, 3600 (seconds).

๐ŸŽซ Token Format โ€‹

Tokens are issued on login and included in the Authorization header:

Authorization: Bearer <token>

The JWT payload contains:

json
{
  "id": "cuid",
  "email": "user@example.com",
  "type": "staff",
  "role": "SUPER_ADMIN"
}

type is either "staff" (User model) or "customer" (Customer model).

๐Ÿ‘ฅ Roles โ€‹

RoleDescriptionPermissions
SUPER_ADMINFull accessEverything including delete operations and staff management
MANAGERLocation managementCreate/edit menu, orders, locations, coupons, automation
STAFFDay-to-day operationsView and update orders, reservations, reviews

๐Ÿ”’ Middleware โ€‹

Four middleware functions control access:

MiddlewareEffect
authenticateRequires a valid JWT. Rejects with 401 if missing or invalid.
optionalAuthParses JWT if present but does not reject unauthenticated requests. Used for guest checkout.
requireStaffRequires type: "staff". Rejects customers with 403.
requireRole(...roles)Requires the user's role to be one of the specified roles. Rejects with 403.

๐Ÿ“ Example route protection โ€‹

typescript
// Any authenticated user
router.get('/me', authenticate, getMe);

// Staff only
router.get('/orders', authenticate, requireStaff, listOrders);

// Manager or Super Admin
router.post('/items', authenticate, requireStaff, requireRole('SUPER_ADMIN', 'MANAGER'), createMenuItem);

๐Ÿ”— Social Login โ€‹

See Social Login for Google and Facebook OAuth configuration.