Open History to see the last commit
1// Sign in with ChatGPT redirects only to http://127.0.0.1:<port>/auth/callback.
2// Vite listens on localhost ([::1]), so nothing answers on 127.0.0.1. This
3// sends 127.0.0.1:3000 requests to the same path on localhost:3000, where
4// completeChatGptSignIn finishes the sign-in.
5import { createServer } from 'node:http'
6
7const PORT = 3000
8
9createServer((req, res) => {
10 // Only redirect requests addressed to 127.0.0.1, so a localhost request
11 // that lands here cannot loop.
12 if (!req.headers.host?.startsWith('127.0.0.1')) {
13 res.writeHead(404).end()
14 return
15 }
16 res.writeHead(302, { Location: `http://localhost:${PORT}${req.url ?? '/'}` })
17 res.end()
18})
19 .on('error', (error) => {
20 console.warn(
21 `[chatgpt-callback] 127.0.0.1:${PORT} unavailable: ${error.message}`,
22 )
23 // ponytail: stay alive so `concurrently -k` does not stop the dev server.
24 setInterval(() => {}, 1 << 30)
25 })
26 .listen(PORT, '127.0.0.1', () => {
27 console.log(
28 `[chatgpt-callback] http://127.0.0.1:${PORT} → localhost:${PORT}`,
29 )
30 })