Repository navigation
Sign in with ChatGPT from Settings when Refract runs on this computer - #139
Conversation
Lorena-J
left a comment
There was a problem hiding this comment.
I’d like to see a few changes before merging. Please refresh the connection state when Settings reopens, cancel any pending sign-in when the user signs out, and keep the credentials-file flow available for remote users behind a reverse proxy. I reproduced the stale Settings state in Chrome with a mocked sign-in. There’s also a conflict with main in CHANGELOG.md.
| // A sign-in started before Settings was closed is still running on the server. | ||
| useEffect(() => { | ||
| api<LoginState>('/api/chatgpt/login').then( | ||
| (st) => st.state === 'pending' && setLogin(st), |
There was a problem hiding this comment.
If I close Settings, finish signing in in the browser, and reopen Settings, Refract still shows “Not connected” until I reload. I reproduced this in Chrome with a mocked sign-in. Could we refresh the ChatGPT status when Settings reopens and the attempt is already done?
There was a problem hiding this comment.
Fixed: when Settings opens and the server reports the sign-in as done, it fetches the ChatGPT status again, so the account shows without a reload. Covered by e2e/chatgpt-signin.spec.ts, which mocks your scenario (Settings closed while pending, sign-in done, Settings reopened); it fails without the fix and passes in Chrome and WebKit.
| // listens on all its interfaces gets none: OpenAI's redirect to the host's | ||
| // 127.0.0.1 can't reach it. | ||
| func loginsFor(addr net.Addr) *siwc.Logins { | ||
| if tcp, ok := addr.(*net.TCPAddr); ok && tcp.IP.IsLoopback() { |
There was a problem hiding this comment.
A loopback listener can also be the upstream for a reverse proxy, as in our deployment docs. In that setup, users see the sign-in button even though Refract opens the browser on the server and the callback needs to reach that same machine. Could we keep the credentials-file flow for users accessing Refract remotely?
There was a problem hiding this comment.
Good catch. The decision is now per request: sign_in_here and the /api/chatgpt/login routes need a loopback peer, a loopback Host (localhost, *.localhost or a loopback IP) and no proxy headers (Forwarded, X-Forwarded-For, X-Forwarded-Host, X-Real-IP). A browser behind a reverse proxy gets the credentials-file flow, and the login routes answer 403. Covered by TestSignInHereOnlyFromThisComputer and TestFromThisComputer; the README says so too.
…chatgpt-from-settings-when
…t on sign-out and refresh Settings when it finished
|
Thanks for the review. All three points are addressed in the latest commit: Settings refreshes the connection when a sign-in finished while it was closed; signing out cancels a pending sign-in (TestSignOutCancelsPendingSignIn); and browsers behind a reverse proxy keep the credentials-file flow. I also merged main and resolved the CHANGELOG.md conflict, keeping both entries. make test and tsc --noEmit pass. |
Closes #61.
When the server listens on loopback (the macOS app,
refract local,make dev), Settings → Assistant → Sign in with ChatGPT runs the whole sign-in in the server: no terminal, no credential file. This removes the last step of the Mac beta that needed a terminal (#93).Server
internal/siwc/logins.go:Loginsrunssiwc.Logininside the server, one at a time (port 1455 is fixed). The same account starting again replaces its stale attempt; another account getsErrLoginBusy(409). It times out after 10 min, can be cancelled, and reportsidle | pending | done | failedwith a kind (cancelled,timeout,port_busy,other_account,other). It never carries tokens.Session.Store(shared withImport, still enforcesREFRACT_ALLOWED_SUBJECT) andSession.SaveRegistration(keeps the issuedclient_idbefore the exchange, never over live tokens). It reuses the saved registration andlogin_hinton later sign-ins;{"new": true}registers another account.POST/GET/DELETE /api/chatgpt/loginonly when the plan is offered and the listener is loopback (loginsFor). A container listening on all interfaces doesn't get them, because the redirect couldn't reach it.GET /api/chatgpt/statusaddssign_in_here.ErrDenied,ErrOtherAccountinsiwc.Login. The CLI adds the-newhint itself, points to Settings → Assistant, and no longer asks to setREFRACT_ALLOWED_SUBJECT.Web
ChatGPTSignIn.tsx: the button opens the authorize URL withwindow.openafter the request (the desktop app sends it to the default browser, as with the GitHub device flow), then shows a waiting state with a spinner, Open the sign-in page as a fallback link and Cancel, polling every 1.5 s. Failures are in plain words with Try again, or Use the new account forother_account. A pending sign-in is picked up again when Settings reopens.refract login. Spanish entries are added.Tests
go test ./internal/siwc/ -race: start → callback → stored credential, the registration reused on the next start, busy for another account, restart replaces, cancel frees the port, timeout, denied, port in use, other account thennew, account not allowed, and no token in logs.cmd/refract: routes present only on loopback with the plan;loginsForfor v4/v6 loopback vs. any/LAN.node --testfor the state helpers; the i18n test passes.make testpasses, andnpx tsc --noEmitis clean.127.0.0.1:8097,REFRACT_AUTH=none): the idle screen, the click openingauth.openai.comin a new tab, the waiting screen, and Cancel returning to idle. Not yet checked: a real end-to-end ChatGPT account and the signed Mac app; both go in the Verify the packaged macOS beta from first launch through update #93 run.