Files
accountkit/test/auth-runtime.test.js
CatoandClaude 4b99dfda73 fix(auth): a server component's session carries actorId, like an API route's (v0.5.2) (#11)
`getSession()` returned better-auth's raw session, whose user has no
`actorId`, while `requireUser()` attached one. A product reading
`session.user.actorId` in a server component — Heidi's dashboard, its
channels page, every page gated on "is somebody signed in" — got
`undefined` for every signed-in person and drew the signed-out page, while
its API routes, going through `requireUser`, worked. That split is how it
stayed invisible: nothing was red, the page was merely wrong.

One reader now: `sessionFor(headers)` resolves the session and the actor
id; `getSession` and `requireUser` both go through it, and it is exported
for a product that holds the headers itself. The runtime test pins the two
answers to each other.


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

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-22 18:43:42 +02:00

108 lines
3.4 KiB
JavaScript

// The Next.js runtime: who is asking, and which id a product's rows use.
import { test, before, after } from "node:test";
import assert from "node:assert/strict";
import { PGlite } from "@electric-sql/pglite";
import { drizzle } from "drizzle-orm/pglite";
import { pushSchema } from "drizzle-kit/api";
import { createAuthRuntime } from "../dist/auth/next.js";
import * as schema from "../dist/auth/schema.js";
process.env.BETTER_AUTH_SECRET = "r".repeat(48);
const SITE = {
url: "https://app.example.ch",
title: "Example",
slug: "example",
};
let pg, db, runtime;
let n = 0;
before(async () => {
pg = new PGlite();
db = drizzle(pg, { schema: schema.authSchema });
await (await pushSchema(schema.authSchema, db)).apply();
runtime = createAuthRuntime({
site: SITE,
mail: { configured: false, send: async () => {} },
isDatabaseConfigured: () => true,
getDatabase: () => db,
legacyIdentityProvider: "orangecat",
});
});
after(() => pg.close());
async function signUp() {
const r = await runtime.POST(
new Request(`${SITE.url}/api/auth/sign-up/email`, {
method: "POST",
headers: {
"content-type": "application/json",
origin: SITE.url,
"x-forwarded-for": `198.51.100.${++n}`,
},
body: JSON.stringify({
email: `p${n}@example.ch`,
password: "correct horse battery",
name: "",
}),
}),
);
assert.equal(r.status, 200);
return {
cookie: r.headers
.getSetCookie()
.map((c) => c.split(";")[0])
.join("; "),
user: (await r.json()).user,
};
}
const who = (cookie) =>
runtime.requireUser(
new Request(`${SITE.url}/api/x`, { headers: { cookie } }),
);
test("no session: the answer to send is 401", async () => {
const r = await who("");
assert.equal(r.response.status, 401);
});
test("a person who registered here is their user id", async () => {
const { cookie, user } = await signUp();
assert.equal((await who(cookie)).user.actorId, user.id);
});
test("a person who came in through OrangeCat keeps the OrangeCat id their rows already use", async () => {
const { cookie, user } = await signUp();
// OrangeCat was their first way in; the password came after.
await db.insert(schema.account).values({
id: `a${n}`,
accountId: "oc-actor-9",
providerId: "orangecat",
userId: user.id,
createdAt: new Date(Date.now() - 60 * 60_000),
});
assert.equal((await who(cookie)).user.actorId, "oc-actor-9");
});
test("linking OrangeCat later never moves an id that already has data", async () => {
const { cookie, user } = await signUp();
await db.insert(schema.account).values({
id: `b${n}`,
accountId: "oc-actor-late",
providerId: "orangecat",
userId: user.id,
createdAt: new Date(Date.now() + 60 * 60_000),
});
assert.equal((await who(cookie)).user.actorId, user.id);
});
test("a server component's session carries the same actorId as an API route's", async () => {
// The two readers used to disagree: `getSession` returned better-auth's raw
// session (no `actorId`) while `requireUser` attached one, so every server
// component read `undefined` and drew the signed-out page.
const { cookie, user } = await signUp();
const session = await runtime.sessionFor(new Headers({ cookie }));
assert.equal(session.user.actorId, user.id);
assert.equal(session.user.actorId, (await who(cookie)).user.actorId);
assert.equal(await runtime.sessionFor(new Headers()), null);
});