fix(login): respect branding themeMode for theme toggle, fix CSP (#11903)

Closes #11721

# Which Problems Are Solved

The login app now correctly handles the themeMode from branding
settings:

- Hide toggle when themeMode is LIGHT or DARK — the theme is forced,
users cannot switch
- Show 3-option toggle (light / system / dark) when themeMode is AUTO or
UNSPECIFIED

# How the Problems Are Solved

- Introduced a `BrandingContext` to pass `themeMode` from `ThemeWrapper`
down to `ThemeSwitch`

# Additional Changes

- removed the CSP from next.config.ts to prevent a precedency issue
where the CSP was actually not applied
This commit is contained in:
Max Peintner
2026-03-31 12:25:09 +00:00
committed by GitHub
parent 604d5cb0a9
commit ec0bd5790f
5 changed files with 141 additions and 51 deletions
+27 -12
View File
@@ -1,9 +1,10 @@
"use client";
import { setTheme } from "@/helpers/colors";
import { BrandingSettings } from "@zitadel/proto/zitadel/settings/v2/branding_settings_pb";
import { BrandingSettings, ThemeMode } from "@zitadel/proto/zitadel/settings/v2/branding_settings_pb";
import { useTheme } from "next-themes";
import { ReactNode, useEffect } from "react";
import { ReactNode, useEffect, useLayoutEffect } from "react";
import { setThemeMode } from "./branding-context";
type Props = {
branding: BrandingSettings | undefined;
@@ -17,23 +18,37 @@ export const ThemeWrapper = ({ children, branding }: Props) => {
setTheme(document, branding);
}, [branding]);
// Handle branding themeMode to force specific theme
// Publish themeMode to the module-level store so ThemeSwitch can read it
useEffect(() => {
setThemeMode(branding?.themeMode ?? ThemeMode.UNSPECIFIED);
}, [branding?.themeMode]);
// Handle branding themeMode to force specific theme.
// Uses useLayoutEffect to apply before paint and writes the forced value
// to localStorage so next-themes doesn't fall back to system default.
useLayoutEffect(() => {
if (branding?.themeMode !== undefined) {
// Based on the proto definition:
// THEME_MODE_UNSPECIFIED = 0
// THEME_MODE_AUTO = 1
// THEME_MODE_LIGHT = 2
// THEME_MODE_DARK = 3
switch (branding.themeMode) {
case 2: // THEME_MODE_LIGHT
case ThemeMode.LIGHT:
document.documentElement.classList.remove("dark");
try {
localStorage.setItem("cp-theme", "light");
} catch {
/* localStorage unavailable (e.g. private mode) */
}
setNextTheme("light");
break;
case 3: // THEME_MODE_DARK
case ThemeMode.DARK:
document.documentElement.classList.add("dark");
try {
localStorage.setItem("cp-theme", "dark");
} catch {
/* localStorage unavailable (e.g. private mode) */
}
setNextTheme("dark");
break;
case 1: // THEME_MODE_AUTO
case 0: // THEME_MODE_UNSPECIFIED
case ThemeMode.AUTO:
case ThemeMode.UNSPECIFIED:
default:
setNextTheme("system");
break;