Skip to Content
Living documentation — last reviewed 2026-05-28
FeaturesUsers AuthUsers & Auth — Code Map

Users & Auth — Code Map

API

Auth module (apps/api/src/auth/)

FilePurpose
auth.module.tsGlobal module. Provides CLERK_CLIENT token (Clerk SDK), injected anywhere as @Inject(CLERK_CLIENT) private clerk: ClerkClient.
auth.guard.tsGlobal AuthGuard (registered in AppModule). Verifies bearer JWT via @clerk/backend.verifyToken, attaches request.auth = {userId, sessionId, user}. Test bypass for non-prod.
current-user.decorator.ts`@CurrentUser(‘userId'
public.decorator.ts@Public() marks a handler as outside the AuthGuard.
auth.guard.unit.spec.tsCoverage of verify + test-bypass + bearer parsing.

Users module (apps/api/src/users/)

FilePurpose
users.module.tsExports UsersService.
users.controller.tsRoutes /users/me, /users/me (PATCH), /users/me (DELETE), /users/:id.
users.service.tsfindOrCreateFromClerk, findByClerkId (30s cache), syncFromClerk, syncToClerk, updateProfile, isProfileComplete, deleteSelf, hasOutstandingConsents, national ID helpers.
national-id-encryption.service.tsEnvelope encryption for Israeli ID.
dto/update-profile.dto.tsAll editable fields + Israeli ID/phone/DOB validators (isValidIsraeliId, isValidDob, normalizeIsraeliPhone).
validation.unit.spec.tsNational ID + phone + DOB validators.

Webhooks module (apps/api/src/webhooks/)

FilePurpose
webhooks.module.tsWires controller.
clerk-webhook.controller.tsPOST /webhooks/clerk@Public() + Svix verify. Dispatches user.created, user.updated, user.deleted.

Routes

MethodPathHandler
GET/users/meUsersController.getMe
PATCH/users/meUsersController.updateMe
DELETE/users/meUsersController.deleteMe
GET/users/:idUsersController.getById (self-only)
POST/webhooks/clerkClerkWebhookController.handleWebhook

Web

Routes (auth)

RouteDescription
apps/web/src/app/[lang]/(auth)/sign-in/[[...sign-in]]/page.tsxClerk sign-in.
apps/web/src/app/[lang]/(auth)/sign-up/[[...sign-up]]/page.tsxClerk sign-up.
apps/web/src/app/[lang]/auth/reset/...Password reset (Clerk-hosted).
apps/web/src/app/[lang]/(protected)/complete-profile/...Profile completion form (if profileComplete=false).
apps/web/src/app/[lang]/(protected)/accept-terms/...Legal consents gate (if pendingLegalConsents=true).

Middleware

apps/web/src/middleware.ts — uses clerkMiddleware from @clerk/nextjs/server. Protects all locale-prefixed paths under (protected)/. Skips API routes and tRPC. Resolves locale via cookie / Negotiator.

Components

  • apps/web/src/components/protected-shell.tsx — wraps Clerk’s UserProvider and role-router.
  • apps/web/src/components/role-router.tsx — routes user to /dashboard/overview (owner/admin/coach) or / (member) based on memberships.
  • apps/web/src/components/guest/ — guest user menu, prefs.
  • apps/web/src/components/legal/legal-acceptance-form.tsx — consents UI used in onboarding and in the accept-terms route.

DB tables

TableUsed as
usersOwned. Global identity.
membershipsJoined to users; counts toward org access.
device_tokensPush notification targets; soft-deleted on user delete.
subscriptionsCancelled on user delete.
legal_documents / legal_consentsRead by hasOutstandingConsents.
member_profilesPer-(user, org) extended profile.

Shared schemas

  • UserResponse in libs/shared/src/lib/schemas/user.schema.ts — includes profileComplete, pendingLegalConsents, nationalIdMasked.
  • UserWithMembershipsResponse — same plus memberships[].
  • normalizeIsraeliPhone, isValidIsraeliId, isValidDob, normalizeIsraeliId, relationshipValues — validators.

Tests

FileWhat it covers
apps/api/src/auth/auth.guard.unit.spec.tsJWT verify, test bypass, bearer parsing.
apps/api/src/users/national-id-encryption.service.unit.spec.tsEncrypt/decrypt + mask.
apps/api/src/users/validation.unit.spec.tsProfile validators.
apps/web/e2e/Clerk auth flows are stubbed via usePersona + storageState (see CLAUDE.md “E2E Driver Pattern”).