diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d5423e..d53bc96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed +- **`coursecode login` after Deny:** The browser sign-in fallback (used when Cloud has no device-code login) now stops as soon as you click Deny and says so, instead of waiting until the login times out. + ## [0.4.5] - 2026-11-06 ### Added diff --git a/lib/cloud.js b/lib/cloud.js index cb7ceaa..67f8534 100644 --- a/lib/cloud.js +++ b/lib/cloud.js @@ -625,6 +625,15 @@ async function runLegacyLoginFlow() { process.exit(1); } + // 400 is final: the user clicked Deny (or the session is invalid). + if (pollRes.status === 400) { + let body = {}; + try { body = JSON.parse(await pollRes.text()); } catch { /* ignore */ } + const reason = body.error === 'Denied' ? 'was denied in the browser' : `failed: ${body.error || 'HTTP 400'}`; + console.error(`\nāŒ Login ${reason}. Run \`coursecode login\` to try again.\n`); + process.exit(1); + } + if (!pollRes.ok) continue; const data = JSON.parse(await pollRes.text()); diff --git a/tests/lib/cloud-auth.test.js b/tests/lib/cloud-auth.test.js index 01ad51f..62bbce3 100644 --- a/tests/lib/cloud-auth.test.js +++ b/tests/lib/cloud-auth.test.js @@ -110,6 +110,36 @@ describe('cloud auth and binding regression guards', () => { expect(stdoutSpy).toHaveBeenCalled(); }); + it('legacy login stops polling as soon as the user clicks Deny', async () => { + vi.resetModules(); + mockOsHome(tempHome); + vi.doMock('child_process', async () => { + const actual = await vi.importActual('child_process'); + const execFile = vi.fn(() => ({ on: vi.fn() })); + return { ...actual, default: { ...actual.default, execFile }, execFile }; + }); + + const fetchMock = mockCloudFetchSequence([ + // No device-code endpoint → legacy nonce flow + { ok: false, status: 404, body: { error: 'Not found' } }, + { ok: true, status: 200, body: { ok: true } }, + { ok: false, status: 400, body: { error: 'Denied' } }, + ]); + + vi.spyOn(console, 'log').mockImplementation(() => {}); + const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); + vi.spyOn(process, 'exit').mockImplementation((code) => { + throw new Error(`process.exit(${code})`); + }); + + const cloud = await import('../../lib/cloud.js'); + await expect(cloud.login()).rejects.toThrow('process.exit(1)'); + + expect(fetchMock).toHaveBeenCalledTimes(3); + expect(errorSpy.mock.calls.flat().join(' ')).toContain('denied in the browser'); + expect(fs.existsSync(path.join(tempHome, '.coursecode', 'credentials.json'))).toBe(false); + }, 10_000); + it('status resolves slug from local project config and keeps org query behavior', async () => { fs.mkdirSync(path.join(tempProject, '.coursecode'), { recursive: true }); fs.writeFileSync(