mirror of
https://github.com/bitbaum/accountkit.git
synced 2026-12-04 05:57:23 +01:00
Five products (evig, kivvi, petvity, surf-your-life, loki) each carried a
copy of the same bridge from next-auth/react to the shared runtime, the same
rollback-only rehearsal script and the same journey-test cookie client. They
now live here once.
- auth/nextauth-compat: createNextAuthCompat answers session (GET and POST),
csrf, providers, signout, each credentials callback and signin/<oidc> for
every configured OIDC client, sign-in or link when signed in, plus a
same-origin GET entry for server-side signIn. Only what the screens use is
open; AccountKit's own credential writers answer 404. The product supplies
its session shape, credentials resolvers (an address or a refusal code),
login codes and hooks; it gets sessionFor, sessionIdFor and currentSession.
A signed-out session reads as null, never {}.
- auth/import-rehearsal + the accountkit-rehearse-import command: a
product's import SQL in one transaction that always rolls back, reported
as counts, through the product's pg or postgres driver.
- auth/compat-testing: a cookie-keeping client that speaks what
next-auth/react sends.
test/nextauth-compat.test.js runs them on PGlite with the real runtime.
Claude-Session: https://claude.ai/code/session_019MDtmud6zcK4rStBGmYSML
Co-authored-by: Claude <noreply@anthropic.com>
97 lines
3.8 KiB
JavaScript
97 lines
3.8 KiB
JavaScript
/**
|
|
* A browser stand-in for journey tests of a product on the compat layer: it
|
|
* speaks what `next-auth/react` sends, keeps the cookie jar between calls,
|
|
* and gives each request its own client address so AccountKit's limiter
|
|
* never trips inside one test file.
|
|
*
|
|
* const client = createCompatClient({ compat, site: "https://app.example.test" });
|
|
* const result = await client.signIn("credentials", { email, password });
|
|
* expect(client.refusal(result)).toBeNull();
|
|
* expect((await client.session())?.user.id).toBe(id);
|
|
*/
|
|
export function createCompatClient(options) {
|
|
const jar = new Map();
|
|
let ip = 0;
|
|
const cookieHeader = () => [...jar].map(([name, value]) => `${name}=${value}`).join("; ");
|
|
function keep(response) {
|
|
for (const set of response.headers.getSetCookie()) {
|
|
const [pair] = set.split(";");
|
|
const at = pair.indexOf("=");
|
|
const name = pair.slice(0, at);
|
|
const value = pair.slice(at + 1);
|
|
if (/max-age=0/i.test(set) || value === "")
|
|
jar.delete(name);
|
|
else
|
|
jar.set(name, value);
|
|
}
|
|
return response;
|
|
}
|
|
/** One request to /api/auth{route}: a form POST when `form` is given, else a GET. */
|
|
async function call(route, form) {
|
|
ip += 1;
|
|
const headers = new Headers({
|
|
origin: options.site,
|
|
"x-forwarded-for": `198.51.100.${ip % 250}`,
|
|
cookie: cookieHeader(),
|
|
});
|
|
if (form)
|
|
headers.set("content-type", "application/x-www-form-urlencoded");
|
|
const request = new Request(`${options.site}/api/auth${route}`, {
|
|
method: form ? "POST" : "GET",
|
|
headers,
|
|
body: form ? new URLSearchParams(form) : undefined,
|
|
});
|
|
return keep(await (form
|
|
? options.compat.route.POST(request)
|
|
: options.compat.route.GET(request)));
|
|
}
|
|
return {
|
|
call,
|
|
/** The jar as a Cookie header, to hand to server-side helpers. */
|
|
get cookie() {
|
|
return cookieHeader();
|
|
},
|
|
/** Replace the jar (switch between people), from a Cookie header. */
|
|
set cookie(value) {
|
|
jar.clear();
|
|
for (const part of value.split("; ").filter(Boolean)) {
|
|
const at = part.indexOf("=");
|
|
jar.set(part.slice(0, at), part.slice(at + 1));
|
|
}
|
|
},
|
|
/** signIn(provider, { redirect: false }) as next-auth/react posts it. */
|
|
async signIn(provider, fields, callbackUrl = `${options.site}/`) {
|
|
const response = await call(`/callback/${provider}`, {
|
|
...fields,
|
|
csrfToken: "test",
|
|
callbackUrl,
|
|
});
|
|
return (await response.json());
|
|
},
|
|
/** signOut({ redirect: false, callbackUrl }) as next-auth/react posts it. */
|
|
async signOut(callbackUrl = `${options.site}/`) {
|
|
const response = await call("/signout", {
|
|
csrfToken: "test",
|
|
callbackUrl,
|
|
});
|
|
return (await response.json());
|
|
},
|
|
/** getSession(): the product session, or null. */
|
|
async session() {
|
|
return (await (await call("/session")).json());
|
|
},
|
|
/** useSession().update(): the same fresh read, posted. */
|
|
async update() {
|
|
return (await (await call("/session", { data: "{}" })).json());
|
|
},
|
|
/** The `error` a sign-in answer carries for the login page, or null on success. */
|
|
refusal(result) {
|
|
return new URL(result.url).searchParams.get("error");
|
|
},
|
|
/** The login page's `code` from a refused sign-in. */
|
|
code(result) {
|
|
return new URL(result.url).searchParams.get("code");
|
|
},
|
|
};
|
|
}
|
|
//# sourceMappingURL=compat-testing.js.map
|