feat(packages): add standalone auth and payment
This commit is contained in:
28
packages/auth/src/marketplace-context.ts
Normal file
28
packages/auth/src/marketplace-context.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { HttpHeaders } from '@angular/common/http';
|
||||
import { Injectable, inject } from '@angular/core';
|
||||
import { MARKETPLACES_AUTH_CONFIG } from './config';
|
||||
|
||||
export const MARKETPLACE_DOMAIN_HEADER = 'X-Marketplace-Domain';
|
||||
|
||||
export function normalizeMarketplaceDomain(domain: string): string {
|
||||
return domain.trim().toLowerCase().replace(/\.$/, '');
|
||||
}
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class AuthMarketplaceContext {
|
||||
private readonly config = inject(MARKETPLACES_AUTH_CONFIG);
|
||||
|
||||
domain(): string {
|
||||
const configured = this.config.marketplaceDomain;
|
||||
const domain = typeof configured === 'function'
|
||||
? configured()
|
||||
: configured ?? (typeof location === 'undefined' ? '' : location.hostname);
|
||||
return normalizeMarketplaceDomain(domain);
|
||||
}
|
||||
|
||||
headers(extra?: Record<string, string>): HttpHeaders {
|
||||
const domain = this.domain();
|
||||
if (!domain) throw new Error('Marketplace domain cannot be resolved');
|
||||
return new HttpHeaders({ [MARKETPLACE_DOMAIN_HEADER]: domain, ...extra });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user