Tenant Resolution
Understanding how the system identifies which tenant a request belongs to.
Resolution Strategies
Header-Based (Default)
Extract tenant ID from HTTP header:
MultiTenantModule.forRoot({
tenantResolution: {
strategy: 'header',
headerName: 'x-tenant-id',
},
});
Request example:
curl -H "x-tenant-id: acme" http://localhost:3000/api/users
Subdomain-Based
Use subdomain as tenant identifier:
MultiTenantModule.forRoot({
tenantResolution: {
strategy: 'subdomain',
},
});
Examples:
acme.yourapp.com→ tenant: acmetechcorp.yourapp.com→ tenant: techcorp
JWT Token
Extract tenant from JWT claims:
MultiTenantModule.forRoot({
tenantResolution: {
strategy: 'jwt',
jwtClaimName: 'tenantId',
},
});
JWT payload example:
{
"sub": "user123",
"tenantId": "acme",
"exp": 1234567890
}
Custom Resolution
Implement custom logic:
MultiTenantModule.forRoot({
tenantResolution: {
strategy: 'custom',
customResolver: (request: any) => {
// Custom logic here
const tenantId = request.query.tenant || request.body.tenantId;
return tenantId;
},
},
});
Best Practices
- Always validate tenant existence
- Handle missing tenant gracefully
- Log tenant resolution for debugging
- Cache tenant lookups when possible
Next Steps
- Implement Authentication
- Configure Testing