| 138 | } |
| 139 | |
| 140 | const authHeaders = async ( |
| 141 | target: TargetShape, |
| 142 | identity: Identity, |
| 143 | ): Promise<Record<string, string>> => { |
| 144 | if (identity.headers) return identity.headers; |
| 145 | const credentials = identity.credentials; |
| 146 | if (!credentials) throw new Error(`identity ${identity.label} has no credentials`); |
| 147 | const response = await fetch(new URL("/api/auth/sign-in/email", target.baseUrl), { |
| 148 | method: "POST", |
| 149 | headers: { "content-type": "application/json", origin: new URL(target.baseUrl).origin }, |
| 150 | body: JSON.stringify(credentials), |
| 151 | redirect: "manual", |
| 152 | }); |
| 153 | const cookie = (response.headers.getSetCookie?.() ?? []).map((c) => c.split(";")[0]).join("; "); |
| 154 | if (!cookie) throw new Error(`sign-in set no cookie (${response.status})`); |
| 155 | return { cookie }; |
| 156 | }; |
| 157 | |
| 158 | const request = async <T>( |
| 159 | target: TargetShape, |