Skip to content

Commit 4d6195d

Browse files
committed
fix(auth): allow multiple polls for approved status
Handle race condition where CLI may poll multiple times after approval. Server now returns approved status for both 'approved' and 'used' states, marking as used only on first successful fetch (idempotent).
1 parent 5dd6789 commit 4d6195d

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/routes/api/auth/cli/poll/+server.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export const GET: RequestHandler = async ({ platform, url }) => {
2222
return json({ status: 'pending' });
2323
}
2424

25-
if (row.status === 'approved' && row.token_id && row.user_id) {
25+
// Handle both 'approved' and 'used' status - CLI may need to poll multiple times
26+
if ((row.status === 'approved' || row.status === 'used') && row.token_id && row.user_id) {
2627
const token = await env.DB.prepare('SELECT * FROM api_tokens WHERE id = ?')
2728
.bind(row.token_id)
2829
.first<{ id: string; token: string; expires_at: string }>();
@@ -31,9 +32,12 @@ export const GET: RequestHandler = async ({ platform, url }) => {
3132
.bind(row.user_id)
3233
.first<{ username: string }>();
3334

34-
await env.DB.prepare("UPDATE cli_auth_codes SET status = 'used' WHERE id = ?")
35-
.bind(code_id)
36-
.run();
35+
// Mark as used after first successful fetch (idempotent)
36+
if (row.status === 'approved') {
37+
await env.DB.prepare("UPDATE cli_auth_codes SET status = 'used' WHERE id = ?")
38+
.bind(code_id)
39+
.run();
40+
}
3741

3842
return json({
3943
status: 'approved',

0 commit comments

Comments
 (0)