sendou.ink/app/features/theme/core/session.server.ts
Kalle c8ea75ebb6
SQL solution migration to Kysely + getting rid of routes folder (#1530)
* Kysely initial

* Badges initial

* Badge routes migrated

* Badges migrated

* Calendar work

* Fix one type problem

* Calendar work

* findResultsByUserId work

* Calendar reworking finished

* PlusSuggestions work

* Migrated suggestions

* Builds progress

* Migrated builds

* Admin migrated

* Migrate articles

* User search

* Faster getUser

* Selectable/insertable as global

* Refresh prod db script + patronTier index

* identifierToUserId

* updateProfile

* findByIdentifier

* More indexes

* User upsert

* upsertLite

* findAllPlusMembers

* updateResultHighlights

* updateMany

* User finished migration

* Fix types

* Fix PlusVotingResult typing

* PlusVotingRepository WIP

* Migrated resultsByMonthYear

* Migrated plusVotes (done with db. related migrations)

* Plus code to features folder

* Fix TODOs

* Export

* Fix range

* Migrate some user pages

* Move rest user routes

* Move /play

* Map list generator

* Front page

* Move map list generation logic

* Move plus voting logic

* Info

* API

* Adjust TODOs

* theme

* Auth

* Remove TODO
2023-11-04 13:15:36 +02:00

40 lines
1.1 KiB
TypeScript

import { createCookieSessionStorage } from "@remix-run/node";
import { isTheme } from "./provider";
import type { Theme } from "./provider";
import invariant from "tiny-invariant";
const TEN_YEARS_IN_SECONDS = 315_360_000;
if (process.env.NODE_ENV === "production") {
invariant(process.env["SESSION_SECRET"], "SESSION_SECRET is required");
}
const sessionSecret = process.env["SESSION_SECRET"] ?? "secret";
const themeStorage = createCookieSessionStorage({
cookie: {
name: "theme",
secure: process.env.NODE_ENV === "production",
secrets: [sessionSecret],
sameSite: "lax",
path: "/",
httpOnly: true,
maxAge: TEN_YEARS_IN_SECONDS,
},
});
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 : null;
},
setTheme: (theme: Theme) => session.set("theme", theme),
commit: () => themeStorage.commitSession(session),
destroy: () => themeStorage.destroySession(session, { maxAge: 0 }),
};
}
export { getThemeSession };