()
| 79 | * Works in both browser/extension and Office Add-in environments. |
| 80 | */ |
| 81 | export async function getGoogleAuthToken(): Promise<string | null> { |
| 82 | const sleepMs = 3000; |
| 83 | const maxRetries = (120 * 1000) / sleepMs; // 120 seconds retry |
| 84 | const randomState = Math.random().toString(36).substring(2, 15); |
| 85 | |
| 86 | openInBrowser(googleAuthUrl(randomState)); |
| 87 | |
| 88 | const endpoint = `${process.env.PD_API_ENDPOINT || ""}/oauth2/status?state=${randomState}`; |
| 89 | for (let attempt = 0; attempt < maxRetries; attempt++) { |
| 90 | const res = await fetch(endpoint); |
| 91 | if (res.status === 410) { |
| 92 | throw new Error("state is used"); |
| 93 | } |
| 94 | const data = await res.json(); |
| 95 | if (data.access_token) { |
| 96 | return data.access_token; |
| 97 | } |
| 98 | await new Promise((resolve) => setTimeout(resolve, sleepMs)); |
| 99 | } |
| 100 | throw new Error("get auth token timeout"); |
| 101 | } |
| 102 | |
| 103 | export function getAppleAuthToken() { |
| 104 | const randomState = Math.random().toString(36).substring(2, 15); |
no test coverage detected