MCPcopy Create free account
hub / github.com/PaperDebugger/paperdebugger / getGoogleAuthToken

Function getGoogleAuthToken

webapp/_webapp/src/libs/oauth.ts:81–101  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

79 * Works in both browser/extension and Office Add-in environments.
80 */
81export 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
103export function getAppleAuthToken() {
104 const randomState = Math.random().toString(36).substring(2, 15);

Callers 1

LoginWithGoogleFunction · 0.90

Calls 2

openInBrowserFunction · 0.85
googleAuthUrlFunction · 0.85

Tested by

no test coverage detected