mirror of
https://github.com/Afilmory/afilmory
synced 2026-04-25 07:15:36 +00:00
@@ -31,8 +31,6 @@ const logger = createLogger('Auth')
|
|||||||
|
|
||||||
@injectable()
|
@injectable()
|
||||||
export class AuthProvider implements OnModuleInit {
|
export class AuthProvider implements OnModuleInit {
|
||||||
private instances = new Map<string, Promise<BetterAuthInstance>>()
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly config: AuthConfig,
|
private readonly config: AuthConfig,
|
||||||
private readonly drizzleProvider: DrizzleProvider,
|
private readonly drizzleProvider: DrizzleProvider,
|
||||||
@@ -66,21 +64,6 @@ export class AuthProvider implements OnModuleInit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private buildCookiePrefix(tenantSlug: string | null): string {
|
|
||||||
if (!tenantSlug) {
|
|
||||||
return 'better-auth'
|
|
||||||
}
|
|
||||||
|
|
||||||
const sanitizedSlug = tenantSlug
|
|
||||||
.trim()
|
|
||||||
.toLowerCase()
|
|
||||||
.replaceAll(/[^a-z0-9_-]/g, '-')
|
|
||||||
.replaceAll(/-+/g, '-')
|
|
||||||
.replaceAll(/^-|-$/g, '')
|
|
||||||
|
|
||||||
return sanitizedSlug ? `better-auth-${sanitizedSlug}` : 'better-auth'
|
|
||||||
}
|
|
||||||
|
|
||||||
private async resolveTenantIdOrProvision(tenantSlug: string | null): Promise<string | null> {
|
private async resolveTenantIdOrProvision(tenantSlug: string | null): Promise<string | null> {
|
||||||
const tenantIdFromContext = this.resolveTenantIdFromContext()
|
const tenantIdFromContext = this.resolveTenantIdFromContext()
|
||||||
if (tenantIdFromContext) {
|
if (tenantIdFromContext) {
|
||||||
@@ -119,37 +102,7 @@ export class AuthProvider implements OnModuleInit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private determineProtocol(host: string, provided: string | null): string {
|
|
||||||
if (provided && (provided === 'http' || provided === 'https')) {
|
|
||||||
return provided
|
|
||||||
}
|
|
||||||
if (host.includes('localhost') || host.startsWith('127.') || host.startsWith('0.0.0.0')) {
|
|
||||||
return 'http'
|
|
||||||
}
|
|
||||||
return 'https'
|
|
||||||
}
|
|
||||||
|
|
||||||
private applyTenantSlugToHost(host: string, fallbackHost: string, tenantSlug: string | null): string {
|
|
||||||
if (!tenantSlug) {
|
|
||||||
return host
|
|
||||||
}
|
|
||||||
|
|
||||||
const [hostName, hostPort] = host.split(':') as [string, string?]
|
|
||||||
if (hostName.startsWith(`${tenantSlug}.`)) {
|
|
||||||
return host
|
|
||||||
}
|
|
||||||
|
|
||||||
const [fallbackName, fallbackPort] = fallbackHost.split(':') as [string, string?]
|
|
||||||
if (hostName !== fallbackName) {
|
|
||||||
return host
|
|
||||||
}
|
|
||||||
|
|
||||||
const portSegment = hostPort ?? fallbackPort
|
|
||||||
return portSegment ? `${tenantSlug}.${fallbackName}:${portSegment}` : `${tenantSlug}.${fallbackName}`
|
|
||||||
}
|
|
||||||
|
|
||||||
private buildBetterAuthProvidersForHost(
|
private buildBetterAuthProvidersForHost(
|
||||||
tenantSlug: string | null,
|
|
||||||
providers: SocialProvidersConfig,
|
providers: SocialProvidersConfig,
|
||||||
oauthGatewayUrl: string | null,
|
oauthGatewayUrl: string | null,
|
||||||
): Record<string, { clientId: string; clientSecret: string; redirectUri?: string }> {
|
): Record<string, { clientId: string; clientSecret: string; redirectUri?: string }> {
|
||||||
@@ -207,11 +160,7 @@ export class AuthProvider implements OnModuleInit {
|
|||||||
options: AuthModuleOptions,
|
options: AuthModuleOptions,
|
||||||
): Promise<BetterAuthInstance> {
|
): Promise<BetterAuthInstance> {
|
||||||
const db = this.drizzleProvider.getDb()
|
const db = this.drizzleProvider.getDb()
|
||||||
const socialProviders = this.buildBetterAuthProvidersForHost(
|
const socialProviders = this.buildBetterAuthProvidersForHost(options.socialProviders, options.oauthGatewayUrl)
|
||||||
tenantSlug,
|
|
||||||
options.socialProviders,
|
|
||||||
options.oauthGatewayUrl,
|
|
||||||
)
|
|
||||||
|
|
||||||
// Use tenant-aware adapter for multi-tenant user/account isolation
|
// Use tenant-aware adapter for multi-tenant user/account isolation
|
||||||
// This ensures that user lookups (by email) and account lookups (by provider)
|
// This ensures that user lookups (by email) and account lookups (by provider)
|
||||||
|
|||||||
Reference in New Issue
Block a user