mirror of
https://github.com/Afilmory/afilmory
synced 2026-02-01 22:48:17 +00:00
feat(auth): refine tenant context handling and slug resolution
- Enhanced AuthController to derive requestedSlug based on tenant context and placeholder status. - Updated TenantContextResolver to utilize requestedSlug for setting tenant headers, improving accuracy in tenant identification. - Ensured consistent handling of tenant slugs across authentication processes. Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -11,6 +11,7 @@ import { SystemSettingService } from 'core/modules/configuration/system-setting/
|
||||
import { eq } from 'drizzle-orm'
|
||||
import type { Context } from 'hono'
|
||||
|
||||
import { PLACEHOLDER_TENANT_SLUG } from '../tenant/tenant.constants'
|
||||
import { getTenantContext, isPlaceholderTenantContext } from '../tenant/tenant.context'
|
||||
import { TenantService } from '../tenant/tenant.service'
|
||||
import type { SocialProvidersConfig } from './auth.config'
|
||||
@@ -124,9 +125,13 @@ export class AuthController {
|
||||
if (tenantId) {
|
||||
try {
|
||||
const aggregate = await this.tenantService.getById(tenantId)
|
||||
const isPlaceholder = aggregate.tenant.slug === PLACEHOLDER_TENANT_SLUG
|
||||
const existingRequestedSlug = tenantContext?.requestedSlug ?? null
|
||||
const derivedRequestedSlug = existingRequestedSlug ?? (isPlaceholder ? null : (aggregate.tenant.slug ?? null))
|
||||
tenantContext = {
|
||||
tenant: aggregate.tenant,
|
||||
isPlaceholder: false,
|
||||
isPlaceholder,
|
||||
requestedSlug: derivedRequestedSlug,
|
||||
}
|
||||
} catch {
|
||||
// ignore; fallback to placeholder context if resolution fails
|
||||
@@ -143,7 +148,7 @@ export class AuthController {
|
||||
session: authContext.session,
|
||||
tenant: {
|
||||
id: tenantContext.tenant.id,
|
||||
slug: tenantContext.tenant.slug ?? null,
|
||||
slug: tenantContext.requestedSlug ?? tenantContext.tenant.slug ?? null,
|
||||
isPlaceholder: isPlaceholderTenantContext(tenantContext),
|
||||
},
|
||||
}
|
||||
@@ -385,8 +390,9 @@ export class AuthController {
|
||||
const tenantContext = getTenantContext()
|
||||
if (tenantContext?.tenant?.id) {
|
||||
headers.set('x-tenant-id', tenantContext.tenant.id)
|
||||
if (tenantContext.tenant.slug) {
|
||||
headers.set('x-tenant-slug', tenantContext.tenant.slug)
|
||||
const effectiveSlug = tenantContext.requestedSlug ?? tenantContext.tenant.slug
|
||||
if (effectiveSlug) {
|
||||
headers.set('x-tenant-slug', effectiveSlug)
|
||||
}
|
||||
}
|
||||
return headers
|
||||
|
||||
@@ -187,7 +187,10 @@ export class TenantContextResolver {
|
||||
|
||||
private applyTenantHeaders(context: Context, tenantContext: TenantContext): void {
|
||||
context.header(HEADER_TENANT_ID, tenantContext.tenant.id)
|
||||
context.header(HEADER_TENANT_SLUG, tenantContext.tenant.slug)
|
||||
const effectiveSlug = tenantContext.requestedSlug ?? tenantContext.tenant.slug
|
||||
if (effectiveSlug) {
|
||||
context.header(HEADER_TENANT_SLUG, effectiveSlug)
|
||||
}
|
||||
}
|
||||
|
||||
private async getBaseDomain(): Promise<string> {
|
||||
|
||||
Reference in New Issue
Block a user