Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/renderer/routes/LoginWithDeviceFlow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export const LoginWithDeviceFlowRoute: FC = () => {

const startPolling = async () => {
setIsPolling(true);
const intervalMs = Math.max(5000, session.intervalSeconds * 1000);

try {
while (isActive && Date.now() < session.expiresAt) {
Expand All @@ -95,6 +94,7 @@ export const LoginWithDeviceFlowRoute: FC = () => {
return;
}

const intervalMs = Math.max(5000, session.intervalSeconds * 1000);
await new Promise((resolve) => {
timeoutId = setTimeout(resolve, intervalMs);
});
Expand Down
19 changes: 17 additions & 2 deletions src/renderer/utils/forges/github/flows.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,30 @@ describe('renderer/utils/forges/github/flows.ts', () => {
expect(token).toBe('device-token-xyz');
});

it('returns null when authorization is pending or slow_down', async () => {
it('returns null and does not change interval when authorization is pending', async () => {
const pendingErr = Object.create(RequestError.prototype);
pendingErr.response = { data: { error: 'authorization_pending' } };

exchangeDeviceCodeMock.mockRejectedValueOnce(pendingErr);

const token = await flows.pollGitHubDeviceFlow(baseSession as DeviceFlowSession);
const session = { ...baseSession } as DeviceFlowSession;
const token = await flows.pollGitHubDeviceFlow(session);

expect(token).toBeNull();
expect(session.intervalSeconds).toBe(5);
});

it('returns null and increases interval by 5 when slow_down', async () => {
const slowDownErr = Object.create(RequestError.prototype);
slowDownErr.response = { data: { error: 'slow_down' } };

exchangeDeviceCodeMock.mockRejectedValueOnce(slowDownErr);

const session = { ...baseSession } as DeviceFlowSession;
const token = await flows.pollGitHubDeviceFlow(session);

expect(token).toBeNull();
expect(session.intervalSeconds).toBe(10);
});

it('throws on other errors', async () => {
Expand Down
7 changes: 6 additions & 1 deletion src/renderer/utils/forges/github/flows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ export async function pollGitHubDeviceFlow(session: DeviceFlowSession): Promise<
const response = err.response?.data as DeviceFlowErrorResponse;
const errorCode = response.error;

if (errorCode === 'authorization_pending' || errorCode === 'slow_down') {
if (errorCode === 'authorization_pending') {
return null;
}

if (errorCode === 'slow_down') {
session.intervalSeconds += 5;
return null;
}
}
Expand Down
Loading