A CLI for publishing standard.site documents to ATProto
sequoia.pub
standard
site
lexicon
cli
publishing
1import type { Context } from "hono";
2
3const SESSION_COOKIE_NAME = "session_id";
4const SESSION_TTL = 60 * 60 * 24 * 14; // 14 days in seconds
5
6/**
7 * Get DID from session cookie
8 */
9export function getSessionDid(c: Context): string | null {
10 const cookie = c.req.header("Cookie");
11 if (!cookie) return null;
12
13 const match = cookie.match(new RegExp(`${SESSION_COOKIE_NAME}=([^;]+)`));
14 return match ? decodeURIComponent(match[1]) : null;
15}
16
17/**
18 * Set session cookie with the user's DID
19 */
20export function setSessionCookie(
21 c: Context,
22 did: string,
23 clientUrl: string,
24): void {
25 const isLocalhost = clientUrl.includes("localhost");
26 const domain = isLocalhost ? "" : "; Domain=.sequoia.pub";
27 const secure = isLocalhost ? "" : "; Secure";
28
29 c.header(
30 "Set-Cookie",
31 `${SESSION_COOKIE_NAME}=${encodeURIComponent(did)}; HttpOnly; SameSite=Lax; Path=/${domain}${secure}; Max-Age=${SESSION_TTL}`,
32 );
33}
34
35/**
36 * Clear session cookie
37 */
38export function clearSessionCookie(c: Context, clientUrl: string): void {
39 const isLocalhost = clientUrl.includes("localhost");
40 const domain = isLocalhost ? "" : "; Domain=.sequoia.pub";
41 const secure = isLocalhost ? "" : "; Secure";
42
43 c.header(
44 "Set-Cookie",
45 `${SESSION_COOKIE_NAME}=; HttpOnly; SameSite=Lax; Path=/${domain}${secure}; Max-Age=0`,
46 );
47}