diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-08-04 20:33:37 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-08-04 20:33:37 +0900 |
| commit | 0f0324b396f3eab53606c8f770d26337dd0e291a (patch) | |
| tree | 0a2afd4701535b11c81fb3908d8c241eaaeb7d21 /frontend/app/.server/cookie.ts | |
| parent | d87507918f33b289ac4fc4dece8a54fa3aa34923 (diff) | |
| download | phperkaigi-2025-albatross-0f0324b396f3eab53606c8f770d26337dd0e291a.tar.gz phperkaigi-2025-albatross-0f0324b396f3eab53606c8f770d26337dd0e291a.tar.zst phperkaigi-2025-albatross-0f0324b396f3eab53606c8f770d26337dd0e291a.zip | |
feat: authenticate users in admin pages
Diffstat (limited to 'frontend/app/.server/cookie.ts')
| -rw-r--r-- | frontend/app/.server/cookie.ts | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/frontend/app/.server/cookie.ts b/frontend/app/.server/cookie.ts new file mode 100644 index 0000000..cccbe78 --- /dev/null +++ b/frontend/app/.server/cookie.ts @@ -0,0 +1,41 @@ +import { Cookie, CookieOptions } from "@remix-run/server-runtime"; +import { parse, serialize } from "cookie"; + +// Remix's createCookie() returns "structured" cookies, which are cookies that hold a JSON-encoded object. +// This is not suitable for interoperation with other systems that expect a simple string value. +// This function creates an "unstructured" cookie, a simple plain text. +export function createUnstructuredCookie( + name: string, + cookieOptions?: CookieOptions, +): Cookie { + const { secrets = [], ...options } = { + path: "/", + sameSite: "lax" as const, + ...cookieOptions, + }; + + return { + get name() { + return name; + }, + get isSigned() { + return secrets.length > 0; + }, + get expires() { + return typeof options.maxAge !== "undefined" + ? new Date(Date.now() + options.maxAge * 1000) + : options.expires; + }, + async parse(cookieHeader, parseOptions) { + if (!cookieHeader) return null; + const cookies = parse(cookieHeader, { ...options, ...parseOptions }); + return name in cookies ? cookies[name] : null; + }, + async serialize(value, serializeOptions) { + return serialize(name, value, { + ...options, + ...serializeOptions, + }); + }, + }; +} |
