Skip to content

Sign in with ChatGPT from Settings when Refract runs on this computer - #139

Merged
ponytojas merged 4 commits into
mainfrom
task/g1-sign-in-with-chatgpt-from-settings-when
Nov 26, 2026
Merged

ponytojas merged 4 commits into
mainfrom
task/g1-sign-in-with-chatgpt-from-settings-when

Conversation

@ponytojas

Copy link
Copy Markdown
Collaborator

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: Logins runs siwc.Login inside the server, one at a time (port 1455 is fixed). The same account starting again replaces its stale attempt; another account gets ErrLoginBusy (409). It times out after 10 min, can be cancelled, and reports idle | pending | done | failed with a kind (cancelled, timeout, port_busy, other_account, other). It never carries tokens.
  • Session.Store (shared with Import, still enforces REFRACT_ALLOWED_SUBJECT) and Session.SaveRegistration (keeps the issued client_id before the exchange, never over live tokens). It reuses the saved registration and login_hint on later sign-ins; {"new": true} registers another account.
  • Routes POST/GET/DELETE /api/chatgpt/login only 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/status adds sign_in_here.
  • Sentinels ErrDenied, ErrOtherAccount in siwc.Login. The CLI adds the -new hint itself, points to Settings → Assistant, and no longer asks to set REFRACT_ALLOWED_SUBJECT.

Web

  • ChatGPTSignIn.tsx: the button opens the authorize URL with window.open after 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 for other_account. A pending sign-in is picked up again when Settings reopens.
  • When signed in: Sign in again (renew or switch). Uploading a credential file stays as a secondary row.
  • The "No AI connected" and "plan usage not granted" texts no longer name 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 then new, account not allowed, and no token in logs.
  • cmd/refract: routes present only on loopback with the plan; loginsFor for v4/v6 loopback vs. any/LAN.
  • node --test for the state helpers; the i18n test passes.
  • make test passes, and npx tsc --noEmit is clean.
  • Manual on a built binary (127.0.0.1:8097, REFRACT_AUTH=none): the idle screen, the click opening auth.openai.com in 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.

@Lorena-J Lorena-J left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread web/src/ChatGPTSignIn.tsx Outdated
// 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),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cmd/refract/hosts.go
// 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() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ponytojas

Copy link
Copy Markdown
Collaborator Author

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.

@ponytojas
ponytojas merged commit 465ce4e into main Nov 26, 2026
8 checks passed
@ponytojas
ponytojas deleted the task/g1-sign-in-with-chatgpt-from-settings-when branch November 26, 2026 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sign in with ChatGPT from Settings when the server runs locally

2 participants