Skip to content

Instantly share code, notes, and snippets.

@jsinglet
Last active February 16, 2021 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsinglet/7893b5c602a4f8bbfc238f4e98284ecc to your computer and use it in GitHub Desktop.
Save jsinglet/7893b5c602a4f8bbfc238f4e98284ecc to your computer and use it in GitHub Desktop.
Multi-Tenant-Angular
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