| 157 | }; |
| 158 | |
| 159 | export const pollDeviceAuthCode = async ({ |
| 160 | apiBaseUrl, |
| 161 | code, |
| 162 | fetch, |
| 163 | signal, |
| 164 | }: PollDeviceAuthCodeOptions): Promise<DeviceAuthPollResult> => { |
| 165 | const requestInit: RequestInit = signal === undefined ? {} : { signal }; |
| 166 | const response = await fetch( |
| 167 | `${trimTrailingSlash(apiBaseUrl)}/api/device-auth/codes/${encodeURIComponent(code)}`, |
| 168 | requestInit |
| 169 | ); |
| 170 | |
| 171 | switch (response.status) { |
| 172 | case 200: { |
| 173 | const data: unknown = await response.json(); |
| 174 | return { auth: parseApprovedAuth(data), status: 'approved' }; |
| 175 | } |
| 176 | case 202: { |
| 177 | return { status: 'pending' }; |
| 178 | } |
| 179 | case 403: { |
| 180 | return { status: 'denied' }; |
| 181 | } |
| 182 | case 410: { |
| 183 | return { status: 'expired' }; |
| 184 | } |
| 185 | default: { |
| 186 | throw new Error('Failed to check sign-in status.'); |
| 187 | } |
| 188 | } |
| 189 | }; |
| 190 | |
| 191 | export const validateAuthToken = async ({ |
| 192 | apiBaseUrl, |