Start a one-shot loopback HTTP server to receive the OAuth redirect.
( port: number, onCode: (code: string) => void, )
| 190 | |
| 191 | /** Start a one-shot loopback HTTP server to receive the OAuth redirect. */ |
| 192 | function startCallbackServer( |
| 193 | port: number, |
| 194 | onCode: (code: string) => void, |
| 195 | ): http.Server { |
| 196 | const server = http.createServer((req, res) => { |
| 197 | const url = new URL(req.url ?? "/", `http://localhost:${port}`) |
| 198 | if (url.pathname !== "/callback") { |
| 199 | res.writeHead(404, { "Content-Type": "text/html" }) |
| 200 | res.end(callbackPage("not-found")) |
| 201 | return |
| 202 | } |
| 203 | const code = url.searchParams.get("code") |
| 204 | const error = url.searchParams.get("error") |
| 205 | if (code) { |
| 206 | onCode(code) |
| 207 | res.writeHead(200, { "Content-Type": "text/html" }) |
| 208 | res.end(callbackPage("success")) |
| 209 | } else if (error) { |
| 210 | const desc = url.searchParams.get("error_description") ?? error |
| 211 | res.writeHead(400, { "Content-Type": "text/html" }) |
| 212 | res.end(callbackPage("error", desc)) |
| 213 | } else { |
| 214 | res.writeHead(400, { "Content-Type": "text/html" }) |
| 215 | res.end(callbackPage("error", "Missing authorization code in the callback.")) |
| 216 | } |
| 217 | // Close after responding so the port is freed immediately. |
| 218 | server.close() |
| 219 | }) |
| 220 | server.listen(port, "127.0.0.1") |
| 221 | return server |
| 222 | } |
| 223 | |
| 224 | /** Render the OAuth callback page. Styled to match matterai.so: dark |
| 225 | * background, cyan/teal accents, centered card, clean typography. */ |
no test coverage detected