import consola from "consola" import { getOauthAppConfig, getOauthUrls } from "~/lib/api-config" import { sleep } from "~/lib/utils" import type { DeviceCodeResponse } from "./get-device-code" export async function pollAccessToken( deviceCode: DeviceCodeResponse, ): Promise { const { clientId, headers } = getOauthAppConfig() const { accessTokenUrl } = getOauthUrls() // Interval is in seconds, we need to multiply by 1000 to get milliseconds // I'm also adding another second, just to be safe const sleepDuration = (deviceCode.interval + 1) * 1000 consola.debug(`Polling access token with interval of ${sleepDuration}ms`) while (true) { const response = await fetch(accessTokenUrl, { method: "POST", headers, body: JSON.stringify({ client_id: clientId, device_code: deviceCode.device_code, grant_type: "urn:ietf:params:oauth:grant-type:device_code", }), }) if (!response.ok) { await sleep(sleepDuration) consola.error("Failed to poll access token:", await response.text()) continue } const json = await response.json() consola.debug("Polling access token response:", json) const { access_token } = json as AccessTokenResponse if (access_token) { return access_token } else { await sleep(sleepDuration) } } } interface AccessTokenResponse { access_token: string token_type: string scope: string }