System Flow
Request Flow
- Student or admin browser makes HTTPS request
- Cloudflare proxies to Railway
- Express middleware resolves
organization_idfrom hostname (4-strategy resolver) - Clerk JWT verified → DB user fetched by
clerk_id→req.userattached - Route handler applies
scopeToOrg(tenant routes) orrequirePlatformAdmin(platform routes) - PostgreSQL query executes with org scoping enforced
- Response returned
Auth Middleware Chain
Request
└── hostnameResolver (org_id from hostname)
└── resolveClerkSecretKey (routes JWT to correct Clerk instance)
└── requireAuth (verifies token, attaches req.user)
└── requireAdmin | requireOrgAdmin | requirePlatformAdmin (role gate)
└── scopeToOrg (WHERE organization_id = req.user.organizationId)Two Clerk Instances
| Instance | Used By | Domain |
|---|---|---|
| Tenant Students | Student app sign-in | Scoped to tenant domain (e.g. clerk.freightpathacademy.com) |
| GritCert Admin | Platform admin console | clerk.gritcert.com |
Tokens from each instance are distinguished by the JWT iss claim.
resolveClerkSecretKey() routes verification to the correct instance automatically.
Last updated on