Let an installation look different via ENVIRONMENT_NAME and ENVIRONMENT_COLOR
Setting ENVIRONMENT_NAME (and optionally ENVIRONMENT_COLOR, a name or hex value) gives the sidebar that colour, adds a name badge next to the title and on the sign-in page, and prefixes the browser tab title, so a dev copy can't be taken for production. Unset, nothing changes. The colour is validated to a plain #rrggbb before it reaches the page's styles. A public GET /api/environment lets the sign-in page use it too. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
44cc9d1cf2
commit
7e196be56d
11 files changed
+253
-3
No files matched your search
@@ -1,3 +1,48 @@
|
||||
/** Colours that can be asked for by name; all dark enough to carry white text. Anything else must be a hex colour. */
|
||||
export const ENVIRONMENT_COLORS: Record<string, string> = {
|
||||
red: "#b91c1c",
|
||||
orange: "#c2410c",
|
||||
amber: "#b45309",
|
||||
green: "#15803d",
|
||||
teal: "#0f766e",
|
||||
blue: "#1d4ed8",
|
||||
purple: "#7e22ce",
|
||||
pink: "#be185d",
|
||||
gray: "#475569",
|
||||
grey: "#475569",
|
||||
};
|
||||
const DEFAULT_ENVIRONMENT_COLOR = ENVIRONMENT_COLORS.orange;
|
||||
|
||||
export interface EnvironmentLook {
|
||||
/** Shown as a badge and in the browser tab; may be empty when only a colour was set. */
|
||||
name: string;
|
||||
/** A #rrggbb colour. */
|
||||
color: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* How this installation says what it is — so a dev copy can't be mistaken for production. Both values come from the environment
|
||||
* (ENVIRONMENT_NAME, ENVIRONMENT_COLOR). Neither set means a normal look; a name alone gets a default colour. The colour is only
|
||||
* ever a validated #rrggbb value, because it is placed into the page's styles.
|
||||
*/
|
||||
export function parseEnvironment(nameRaw: string | undefined, colorRaw: string | undefined): { look: EnvironmentLook | null; warning?: string } {
|
||||
const name = (nameRaw ?? "").replace(/\p{Cc}/gu, "").trim().slice(0, 24);
|
||||
const wanted = (colorRaw ?? "").trim().toLowerCase();
|
||||
|
||||
let color: string | null = null;
|
||||
let warning: string | undefined;
|
||||
if (wanted) {
|
||||
if (ENVIRONMENT_COLORS[wanted]) color = ENVIRONMENT_COLORS[wanted];
|
||||
else if (/^#[0-9a-f]{6}$/.test(wanted)) color = wanted;
|
||||
else if (/^#[0-9a-f]{3}$/.test(wanted)) color = `#${wanted[1]}${wanted[1]}${wanted[2]}${wanted[2]}${wanted[3]}${wanted[3]}`;
|
||||
else warning = `ENVIRONMENT_COLOR "${colorRaw}" isn't a colour name (${Object.keys(ENVIRONMENT_COLORS).join(", ")}) or a hex value like #b45309 — using the default.`;
|
||||
}
|
||||
if (!name && !color) return { look: null, warning };
|
||||
return { look: { name, color: color ?? DEFAULT_ENVIRONMENT_COLOR }, warning };
|
||||
}
|
||||
|
||||
const environmentLook = parseEnvironment(process.env.ENVIRONMENT_NAME, process.env.ENVIRONMENT_COLOR);
|
||||
|
||||
export const env = {
|
||||
port: Number(process.env.PORT ?? 3000),
|
||||
nodeEnv: process.env.NODE_ENV ?? "development",
|
||||
@@ -11,6 +56,8 @@ export const env = {
|
||||
clientId: process.env.AUTHENTIK_CLIENT_ID ?? "",
|
||||
clientSecret: process.env.AUTHENTIK_CLIENT_SECRET ?? "",
|
||||
},
|
||||
environment: environmentLook.look,
|
||||
environmentWarning: environmentLook.warning,
|
||||
get authEnabled() {
|
||||
return Boolean(this.authentik.issuerUrl && this.authentik.clientId && this.authentik.clientSecret);
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user