From 09be9002b5aa8f5ad7c602bcef6b53bbe0398cd3 Mon Sep 17 00:00:00 2001
From: Skitty1238 <157652284+Skitty1238@users.noreply.github.com>
Date: Wed, 4 Jun 2025 18:28:35 -0400
Subject: fixed google authentication + linking and task creation via GT button

---
 src/client/apis/GoogleAuthenticationManager.tsx | 98 ++++++++++++++++---------
 1 file changed, 62 insertions(+), 36 deletions(-)

(limited to 'src/client/apis/GoogleAuthenticationManager.tsx')

diff --git a/src/client/apis/GoogleAuthenticationManager.tsx b/src/client/apis/GoogleAuthenticationManager.tsx
index 1b1d6f734..a93e03e60 100644
--- a/src/client/apis/GoogleAuthenticationManager.tsx
+++ b/src/client/apis/GoogleAuthenticationManager.tsx
@@ -42,46 +42,72 @@ export class GoogleAuthenticationManager extends ObservableReactComponent<object
         this.openState && this.resetState(0, 0);
     }
 
-    public fetchOrGenerateAccessToken = async (displayIfFound = false) => {
-        const response = await Networking.FetchFromServer('/readGoogleAccessToken');
-        // if this is an authentication url, activate the UI to register the new access token
-        if (new RegExp(AuthenticationUrl).test(response)) {
-            this.isOpen = true;
-            this.authenticationLink = response;
-
-            // GETS STUCK AT THIS PROMISE!!
-            return new Promise<string>(resolve => {
-                this.disposer?.();
-                this.disposer = reaction(
-                    () => this.authenticationCode,
-                    async authenticationCode => {
-                        if (authenticationCode && /\d{1}\/[\w-]{55}/.test(authenticationCode)) {
-                            resolve(authenticationCode);
-                            this.disposer?.();
-                            // const response2 = await Networking.PostToServer('/writeGoogleAccessToken', { authenticationCode });
-                            // runInAction(() => {
-                            //     this.success = true;
-                            //     this.credentials = response2 as { user_info: { name: string; picture: string }; access_token: string };
-                            // });
-                            // resolve((response2 as { access_token: string }).access_token);
-                            this.resetState();
-                        }
-                    }
-                );
-            });
-        }
+    // public fetchOrGenerateAccessToken = async (displayIfFound = false) => {
+    //     const response = await Networking.FetchFromServer('/readGoogleAccessToken');
+    //     // if this is an authentication url, activate the UI to register the new access token
+
+    //     if (new RegExp(AuthenticationUrl).test(response)) {
+    //         this.isOpen = true;
+    //         this.authenticationLink = response;
 
-        // otherwise, we already have a valid, stored access token and user info
-        const response2 = JSON.parse(response) as { user_info: { name: string; picture: string }; access_token: string };
-        if (displayIfFound) {
+    //         // GETS STUCK AT THIS PROMISE!!
+    //         return new Promise<string>(resolve => {
+    //             this.disposer?.();
+    //             this.disposer = reaction(
+    //                 () => this.authenticationCode,
+    //                 async authenticationCode => {
+    //                     if (authenticationCode && /\d{1}\/[\w-]{55}/.test(authenticationCode)) {
+    //                         resolve(authenticationCode);
+    //                         this.disposer?.();
+    //                         // const response2 = await Networking.PostToServer('/writeGoogleAccessToken', { authenticationCode });
+    //                         // runInAction(() => {
+    //                         //     this.success = true;
+    //                         //     this.credentials = response2 as { user_info: { name: string; picture: string }; access_token: string };
+    //                         // });
+    //                         // resolve((response2 as { access_token: string }).access_token);
+    //                         this.resetState();
+    //                     }
+    //                 }
+    //             );
+    //         });
+    //     }
+
+    //     // otherwise, we already have a valid, stored access token and user info
+    //     const response2 = JSON.parse(response) as { user_info: { name: string; picture: string }; access_token: string };
+    //     if (displayIfFound) {
+    //         runInAction(() => {
+    //             this.success = true;
+    //             this.credentials = response2;
+    //         });
+    //         this.resetState(-1, -1);
+    //         this.isOpen = true;
+    //     }
+    //     return (response2 as { access_token: string }).access_token;
+    // };
+
+    public fetchOrGenerateAccessToken = async (): Promise<string> => {
+        const response = await Networking.FetchFromServer('/readGoogleAccessToken');
+    
+        // This will return a JSON object with { access_token, user_info } if already linked
+        try {
+            const parsed = JSON.parse(response) as { access_token: string; user_info: { name: string; picture: string } };
+    
             runInAction(() => {
                 this.success = true;
-                this.credentials = response2;
+                this.credentials = parsed;
             });
-            this.resetState(-1, -1);
-            this.isOpen = true;
+    
+            return parsed.access_token;
+        } catch (err) {
+            console.warn('Not linked yet or invalid JSON. Redirecting to auth...');
+            // This is an auth URL — redirect the user to /refreshGoogle
+            if (typeof response === 'string' && response.startsWith('http')) {
+                window.location.href = response;
+                return ''; // Won’t be used — this page will reload anyway
+            }
+    
+            throw new Error('Unable to fetch Google access token.');
         }
-        return (response2 as { access_token: string }).access_token;
     };
 
     resetState = action((visibleForMS: number = 3000, fadesOutInMS: number = 500) => {
@@ -132,7 +158,7 @@ export class GoogleAuthenticationManager extends ObservableReactComponent<object
                     </button>
                 ) : null}
                 {this.showPasteTargetState ? <input className={'paste-target'} onChange={action(e => (this.authenticationCode = e.currentTarget.value))} placeholder={prompt} /> : null}
-                {this.credentials ? (
+                {this.credentials?.user_info?.picture ? (
                     <>
                         <img className={'avatar'} src={this.credentials.user_info.picture} />
                         <span className={'welcome'}>Welcome to Dash, {this.credentials.user_info.name}</span>
-- 
cgit v1.2.3-70-g09d2

