aboutsummaryrefslogtreecommitdiff
path: root/apps/web/app/lib/theme.server.ts
blob: a9b520f339231c97a375c28d5313410bb86dca3b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { createCookieSessionStorage } from "@remix-run/cloudflare";

import { Theme, isTheme } from "./theme-provider";

const themeStorage = createCookieSessionStorage({
	cookie: {
		name: "remix__theme",
		secure: true,
		secrets: ['theme-secret-sm'],
		sameSite: "lax",
		path: "/",
		httpOnly: true,
	},
});

async function getThemeSession(request: Request) {
	const session = await themeStorage.getSession(request.headers.get("Cookie"));
	return {
		getTheme: () => {
			const themeValue = session.get("theme");
			return isTheme(themeValue) ? themeValue : Theme.DARK;
		},
		setTheme: (theme: Theme) => session.set("theme", theme),
		commit: () => themeStorage.commitSession(session),
	};
}

export { getThemeSession };