Last active
February 16, 2021 23:08
-
-
Save jsinglet/7893b5c602a4f8bbfc238f4e98284ecc to your computer and use it in GitHub Desktop.
Multi-Tenant-Angular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { HttpHeaders } from "@angular/common/http"; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class TenantService { | |
constructor() {} | |
getTenantForHostname(hostname: string): Tenant { | |
return this.getTenantForHost(hostname.split(".")[0]); | |
} | |
getTenantForString(s: string) { | |
for (const e in Tenant) { | |
if (e.toLowerCase() === s.toLowerCase()) { | |
return Tenant[e] as Tenant; | |
} | |
} | |
return null; | |
} | |
getTenantForHost(host: string): Tenant { | |
return this.getTenantForString(host); | |
} | |
getTenant(): Tenant { | |
return this.getTenantForHostname(location.hostname); | |
} | |
addTenantToHeaders(headers: HttpHeaders): HttpHeaders { | |
return headers.append("X-Tenant-ID", this.getTenant()); | |
} | |
} | |
export enum Tenant { | |
CLIENT1 = "client1", | |
CLIENT2 = "client2" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment