mirror of
https://github.com/OHTU-German-learning-website/OHTU-German-learning-website.git
synced 2026-10-12 19:13:51 +03:00
1.6 KiB
1.6 KiB
Backend middleware (API helpers)
Last updated: November 30, 2025
This document summarizes the small set of middleware wrappers used by Next.js API route handlers. For implementation, see the source files in gradesa/src/backend/middleware/.
Key wrappers
withAuth— enforces authentication (optionally admin-only) and attachesrequest.user.withInputValidation— validates request bodies using Zod schemas and attaches the parsed data torequest.body.
Usage highlights
- Compose wrappers for common patterns. Example (authentication outside validation):
// PUT endpoint: auth + validation
export const PUT = withAuth(
withInputValidation(async (req) => {
const user = req.user;
const data = req.body;
// ...
}, updateProfileSchema)
);
withAuthoptions: pass{ isAdmin: true }to require admin privileges.
Error responses
withAuthreturns401for missing/invalid session and403for insufficient privileges.withInputValidationreturns400with adetailsarray (Zod-style errors) for invalid input.
Testing
- Middleware tests live with other backend tests under
gradesa/src/backend/test/(search forwithAuthandwithInputValidationtests). - To run the app's unit tests from the repo root:
cd /host/gradesa
npm test
Notes
- Keep middleware small and focused; prefer composition over duplication.
- For handler-specific behavior (optional auth), inspect
request.userdirectly in the handler instead of usingwithAuth.
See also: gradesa/README.md (root onboarding), and gradesa/src/SOURCE_OVERVIEW.md for a high-level map of source folders.