2025-09-27 16:59:25 +09:00
# Development Authentication Setup
2025-10-02 16:33:25 +09:00
## Overview
2025-09-27 16:59:25 +09:00
2025-10-02 16:33:25 +09:00
The auth module now exposes a clean facade (`AuthFacade` ) and a layered structure. Development conveniences (CSRF bypass, throttling disable) still exist, but code placements and service names have changed. This guide outlines how to configure the dev environment and where to look in the codebase.
2025-09-27 16:59:25 +09:00
2025-10-02 16:33:25 +09:00
## File Map
2025-09-27 16:59:25 +09:00
2025-12-23 15:43:36 +09:00
| Purpose | Location |
| --------------------- | ------------------------------------------------------------- | ------------- |
| Express cookie helper | `apps/bff/src/app/bootstrap.ts` |
| Auth controller | `modules/auth/presentation/http/auth.controller.ts` |
| Guards/interceptors | `modules/auth/presentation/http/guards | interceptors` |
| Passport strategies | `modules/auth/presentation/strategies` |
| Facade (use-cases) | `modules/auth/application/auth.facade.ts` |
| Token services | `modules/auth/infra/token` |
| Rate limiter service | `modules/auth/infra/rate-limiting/auth-rate-limit.service.ts` |
| Signup/password flows | `modules/auth/infra/workflows` |
2025-09-27 16:59:25 +09:00
2025-10-02 16:33:25 +09:00
## Development Environment Flags
2025-09-27 16:59:25 +09:00
2025-10-02 16:33:25 +09:00
Add to `.env` as needed:
2025-09-27 16:59:25 +09:00
```bash
2025-10-02 16:33:25 +09:00
# Disable CSRF protection (use only locally)
2025-09-27 16:59:25 +09:00
DISABLE_CSRF=true
2025-10-02 16:33:25 +09:00
# Disable auth rate limits / lockouts
2025-09-27 16:59:25 +09:00
DISABLE_RATE_LIMIT=true
DISABLE_ACCOUNT_LOCKING=true
2026-02-03 18:28:38 +09:00
# Skip OTP verification during login (direct login after credentials)
SKIP_OTP=true
2025-10-02 16:33:25 +09:00
# Show detailed validation errors in responses
2025-09-27 16:59:25 +09:00
EXPOSE_VALIDATION_ERRORS=true
2025-10-02 16:33:25 +09:00
# Allow portal origin
2025-09-27 16:59:25 +09:00
CORS_ORIGIN=http://localhost:3000
```
2025-10-02 16:33:25 +09:00
These flags are consumed in `core/config/auth-dev.config.ts` and by guards/middleware.
2025-09-27 16:59:25 +09:00
2025-10-02 16:33:25 +09:00
## CSRF in Development
2025-09-27 16:59:25 +09:00
2025-10-02 16:33:25 +09:00
- Middleware (`core/security/middleware/csrf.middleware.ts` ) auto-issues stateless tokens.
- Frontend API client must forward `X-CSRF-Token` ; the helper already performs this.
- To bypass entirely in dev, set `DISABLE_CSRF=true` (middleware checks `devAuthConfig.disableCsrf` ).
2025-09-27 16:59:25 +09:00
2025-10-02 16:33:25 +09:00
## Rate Limiting in Development
2025-09-27 16:59:25 +09:00
2025-10-02 16:33:25 +09:00
- `AuthRateLimitService` uses Redis; if Redis isn’ t running, set `DISABLE_RATE_LIMIT=true` to bypass guards.
- Login, signup, password reset, and refresh flows now go through this centralized service.
2025-09-27 16:59:25 +09:00
2025-10-02 16:33:25 +09:00
## Common Troubleshooting
2025-09-27 16:59:25 +09:00
2025-10-02 16:33:25 +09:00
1. **CSRF mismatch** – ensure `csrf-secret` cookie and header are both present. Use `/api/security/csrf/token` if you need to fetch manually.
2. **Rate limit hits** – check headers `X-RateLimit-Remaining` or disable via env flag.
3. **Redis not running** – dev scripts (`pnpm dev:start` ) spin up Redis; otherwise set bypass flags.
4. **Cookie issues** – confirm `CORS_ORIGIN` matches portal dev origin and browser sends cookies (`credentials: 'include'` ).
2025-09-27 16:59:25 +09:00
2025-10-02 16:33:25 +09:00
## Production Safeguards
2025-09-27 16:59:25 +09:00
2025-10-02 16:33:25 +09:00
- Remove/bypass flags (`DISABLE_*` ) in staging/production.
- Ensure `CSRF_SECRET_KEY` is set in production so tokens are signed with stable key.
- Tune rate limits via env values (`LOGIN_RATE_LIMIT_LIMIT` , etc.).
2025-09-27 16:59:25 +09:00
2025-10-02 16:33:25 +09:00
## Quick Validation Steps
2025-09-27 16:59:25 +09:00
2025-10-02 16:33:25 +09:00
1. Start Redis/Postgres via `pnpm dev:start` .
2. Launch BFF and portal (`pnpm --filter @customer-portal/bff dev` , `pnpm --filter @customer-portal/portal dev` ).
3. In browser, verify CSRF token is returned on first GET (check `X-CSRF-Token` response header).
4. Attempt login/signup; inspect console for clear warnings if rate limit triggers.
2025-09-27 16:59:25 +09:00
2025-10-02 16:33:25 +09:00
## Reference Documentation
2025-09-27 16:59:25 +09:00
2025-10-02 16:33:25 +09:00
- `docs/AUTH-MODULE-ARCHITECTURE.md` – full module layout.
- `docs/STRUCTURE.md` – high-level repo map.
- `docs/REDIS-TOKEN-FLOW-IMPLEMENTATION.md` – deep dive on refresh token storage.