(email: string)
| 33 | // The real product login, headless (login → hosted AuthKit → callback), then |
| 34 | // the real create-organization flow so the card renders an org-scoped URL. |
| 35 | const signInWithOrg = async (email: string): Promise<string> => { |
| 36 | const login = await fetch(new URL("/api/auth/login", baseUrl), { redirect: "manual" }); |
| 37 | const stateCookie = cookiePair(login, "wos-login-state"); |
| 38 | const authorizeUrl = new URL(login.headers.get("location") ?? ""); |
| 39 | if (!stateCookie) throw new Error(`login did not redirect to AuthKit (${login.status})`); |
| 40 | authorizeUrl.searchParams.set("login_hint", email); |
| 41 | const consent = await fetch(authorizeUrl, { redirect: "manual" }); |
| 42 | const callbackUrl = consent.headers.get("location"); |
| 43 | if (consent.status !== 302 || !callbackUrl) { |
| 44 | throw new Error(`AuthKit emulator did not redirect (${consent.status})`); |
| 45 | } |
| 46 | const callback = await fetch(callbackUrl, { |
| 47 | redirect: "manual", |
| 48 | headers: { cookie: stateCookie }, |
| 49 | }); |
| 50 | let session = cookiePair(callback, "wos-session"); |
| 51 | if (!session) throw new Error(`callback set no session (${callback.status})`); |
| 52 | |
| 53 | const label = email.split("@")[0]!; |
| 54 | const created = await fetch(new URL("/api/auth/create-organization", baseUrl), { |
| 55 | method: "POST", |
| 56 | headers: { |
| 57 | "content-type": "application/json", |
| 58 | origin: new URL(baseUrl).origin, |
| 59 | cookie: session, |
| 60 | }, |
| 61 | body: JSON.stringify({ name: `Org ${label}` }), |
| 62 | }); |
| 63 | if (!created.ok) throw new Error(`create-organization failed (${created.status})`); |
| 64 | session = cookiePair(created, "wos-session") ?? session; |
| 65 | return session; // "wos-session=<sealed>" |
| 66 | }; |
| 67 | |
| 68 | const email = `user-${randomUUID().slice(0, 8)}@e2e.test`; |
| 69 | const session = await signInWithOrg(email); |
no test coverage detected