{"record":{"id":"1975aa23b8c620b9","repo":"dubinc/dub","slug":"an-error-occurred-during-authentication-please-tr","errorCode":null,"errorMessage":"An error occurred during authentication. Please try again.","messagePattern":"An error occurred during authentication\\. Please try again\\.","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"packages/cli/src/api/callback.ts","lineNumber":72,"sourceCode":"\n      const configInfo: DubConfig = {\n        access_token: accessToken.trim(),\n        refresh_token: refreshToken,\n        expires_at: expiresAt,\n        domain: \"dub.sh\",\n      };\n\n      await setConfig(configInfo);\n      spinner.succeed(\"Configuration completed\");\n\n      logger.info(\"\");\n      logger.info(chalk.green(\"Logged in successfully!\"));\n      logger.info(\"\");\n\n      res.writeHead(200, { \"Content-Type\": \"text/html\" });\n      res.end(\"Authentication successful! You can close this window.\");\n    } catch (error) {\n      res.writeHead(500, { \"Content-Type\": \"text/html\" });\n      res.end(\"An error occurred during authentication. Please try again.\");\n    } finally {\n      server.close();\n      process.exit(0);\n    }\n  });\n\n  setTimeout(() => {\n    server.close();\n    process.exit(0);\n  }, 300000);\n\n  server.listen(4587);\n}\n","sourceCodeStart":54,"sourceCodeEnd":87,"githubUrl":"https://github.com/dubinc/dub/blob/f216b94a24ca5a0a48c6543ee10392c9006c8b75/packages/cli/src/api/callback.ts#L54-L87","documentation":"This is the HTTP 500 response the callback server sends when anything inside the token-exchange/config-save block throws — most commonly a failure of oauthClient.authorizationCode.getToken (invalid or expired code, code_verifier/PKCE mismatch, redirect_uri mismatch) or an error writing the local config. The browser shows this message; the underlying exception is swallowed and the server exits.","triggerScenarios":"Exchanging the authorization code fails (code already used, expired, or PKCE codeVerifier/redirectUri don't match those used in the authorize request), or setConfig fails (e.g. unwritable config file/permissions).","commonSituations":"Refreshing the callback page so the code is consumed twice, restarting login without redoing the authorize step, clock skew invalidating tokens, or ~/.config (or equivalent) being read-only.","solutions":["Re-run the login command from scratch so a fresh authorization code and codeVerifier are generated — used/expired codes cannot be retried.","Ensure the redirectUri passed to getToken exactly matches the one used in the authorize request and the OAuth app registration.","Check write permissions on the CLI's config file location if login consistently fails after 'Verifying'.","Enable verbose/debug logging or temporarily patch the catch to log `error` to see the true cause, since it is currently discarded."],"exampleFix":"// before\n} catch (error) {\n  res.writeHead(500, { \"Content-Type\": \"text/html\" });\n  res.end(\"An error occurred during authentication. Please try again.\");\n}\n// after\n} catch (error) {\n  console.error(\"Auth failed:\", error); // surface the real cause\n  res.writeHead(500, { \"Content-Type\": \"text/html\" });\n  res.end(\"An error occurred during authentication. Please try again.\");\n}","handlingStrategy":"try-catch","validationCode":"// Pre-flight checks before login\nimport { access, constants } from 'fs/promises';\nawait access(configPath, constants.W_OK); // throws early if config file isn't writable\n// Also ensure redirectUri matches the one used in the authorize request\nif (authorizeRedirectUri !== tokenExchangeRedirectUri) {\n  throw new Error('redirect_uri mismatch between authorize and token requests');\n}","typeGuard":"function isOAuthTokenError(e: unknown): e is Error {\n  return e instanceof Error && /token|code|verifier|grant/i.test(e.message);\n}","tryCatchPattern":"try {\n  const tokens = await oauthClient.authorizationCode.getToken({ code, redirectUri, codeVerifier });\n  await setConfig({ ...tokens, domain: 'dub.sh' });\n} catch (error) {\n  console.error('Token exchange or config save failed:', error);\n  // Retry the whole login flow with a fresh code; a consumed code cannot be reused\n}","preventionTips":["Never refresh the callback page — the code is single-use and replay fails.","Keep redirectUri identical across the authorize request, the token request, and the app registration.","Ensure the CLI config directory/file is writable before starting login.","Log the caught error in the catch block so failures aren't silently reduced to a 500."],"tags":["oauth","pkce","token-exchange","cli"],"backgroundTag":"oauth-token-exchange-failed","analyzedSha":"f216b94a24ca5a0a48c6543ee10392c9006c8b75","analyzedAt":"2026-08-31T18:35:50.395Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}