Files
loki/scripts/test/print-session-token.ts
CatoandClaude Fable 5.1 95c40a9cd8 refactor: rename the product from FleetCrown to Loki (#695)
The product, its assistant, the repo, the host and every unit on the box now
share one name. 638 files, ~3,000 mentions: UI copy, config, env keys
(FLEETCROWN_* -> LOKI_*), systemd units (fleetcrown-* -> loki-*), docs,
articles, the deploy workflow other repos reuse, and nine file names.

- Three article slugs carried the old name and had been shared; permanent
  redirects keep those links alive (next.config.ts).
- test-no-retired-name.sh now also forbids the old product name across the
  whole repo, so the rename cannot creep back through templates and examples.
- ASSISTANT_STACK collapses the product and the assistant into one entry,
  because they are now the same name.

Infra was cut over ahead of this merge: /opt/loki, loki-* units, the `loki`
database and role, loki.orangecat.ch (old host 308s to it; /api, /widget,
/embed keep proxying), GitHub repos bitbaum/loki and bitbaum/loki-releases.
The Resend sending domain stays fleetcrown.orangecat.ch until the new domain
is verified in DNS.


Claude-Session: https://claude.ai/code/session_016YJVAanfj8keVSXyZnTemh

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-13 16:13:10 -07:00

92 lines
2.7 KiB
TypeScript

/**
* Prints a session cookie value for automation (dogfood, curl).
* Resolution order matches authenticated-smoke.ts (env → credentials → jwt-mint).
*/
import { config } from "dotenv";
import { smokeSessionToken } from "@/lib/brand-env";
config({ path: ".env.local", quiet: true });
config({ path: ".env.hetzner.local", quiet: true });
const BASE = (process.env.BASE ?? "https://loki.orangecat.ch").replace(/\/$/, "");
function sessionCookieName(): string {
return BASE.startsWith("https://") ? "__Secure-authjs.session-token" : "authjs.session-token";
}
async function tryMintJwt(): Promise<string | null> {
const secret = process.env.AUTH_SECRET?.trim();
if (!secret) return null;
const hetznerPassword = process.env.LOKI_DB_PASSWORD;
const hetznerHost = process.env.HETZNER_IP;
const isProd = BASE.includes("loki.orangecat.ch") || BASE.includes("orangecat.ch");
const dbUrl =
isProd && hetznerPassword && hetznerHost
? `postgres://loki:${encodeURIComponent(hetznerPassword)}@${hetznerHost}:5432/loki?sslmode=require`
: process.env.DATABASE_URL;
if (!dbUrl) return null;
const postgres = (await import("postgres")).default;
const sql = postgres(dbUrl, { max: 1 });
try {
const rows = await sql`
SELECT id, email, name, username, onboarded_at
FROM users
WHERE is_default = true OR email IS NOT NULL
ORDER BY is_default DESC, created_at ASC
LIMIT 1
`;
const u = rows[0] as
| {
id: string;
email: string | null;
name: string | null;
username: string | null;
onboarded_at: Date | null;
}
| undefined;
if (!u?.id) return null;
const { encode } = await import("@auth/core/jwt");
const token = await encode({
token: {
id: u.id,
email: u.email,
name: u.name,
username: u.username,
onboardedAt: u.onboarded_at,
onboardingComplete: Boolean(u.username && u.onboarded_at),
sub: u.id,
},
secret,
salt: sessionCookieName(),
});
const probe = await fetch(`${BASE}/api/me`, {
headers: { Cookie: `${sessionCookieName()}=${token}` },
signal: AbortSignal.timeout(15_000),
});
return probe.ok ? token : null;
} finally {
await sql.end({ timeout: 5 }).catch(() => undefined);
}
}
async function main() {
const fromEnv = smokeSessionToken().trim();
if (fromEnv) {
process.stdout.write(fromEnv);
return;
}
const minted = await tryMintJwt();
if (!minted) {
throw new Error("No session — set LOKI_SESSION_TOKEN or AUTH_SECRET + HETZNER_IP");
}
process.stdout.write(minted);
}
main().catch((e) => {
console.error(e instanceof Error ? e.message : e);
process.exit(1);
});