From 0d1d884ae24c74cc92b3be7a429873bde3d4370c Mon Sep 17 00:00:00 2001
From: Sam Wilkins <35748010+samwilkins333@users.noreply.github.com>
Date: Wed, 9 Oct 2019 05:38:16 -0400
Subject: clean up and extensibility for authentication manager

---
 src/client/apis/AuthenticationManager.tsx | 42 ++++++++++++++++++-------------
 1 file changed, 25 insertions(+), 17 deletions(-)

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

diff --git a/src/client/apis/AuthenticationManager.tsx b/src/client/apis/AuthenticationManager.tsx
index 75a50d8f9..7d8d4f534 100644
--- a/src/client/apis/AuthenticationManager.tsx
+++ b/src/client/apis/AuthenticationManager.tsx
@@ -6,6 +6,8 @@ import { Opt } from "../../new_fields/Doc";
 import { Identified } from "../Network";
 import { RouteStore } from "../../server/RouteStore";
 
+const AuthenticationUrl = "https://accounts.google.com/o/oauth2/v2/auth";
+
 @observer
 export default class AuthenticationManager extends React.Component<{}> {
     public static Instance: AuthenticationManager;
@@ -30,24 +32,30 @@ export default class AuthenticationManager extends React.Component<{}> {
         runInAction(() => this.clickedState = value);
     }
 
-    public executeFullRoutine = async (authenticationLink: string) => {
-        this.authenticationLink = authenticationLink;
-        this.isOpen = true;
-        return new Promise<string>(async resolve => {
-            const disposer = reaction(
-                () => this.authenticationCode,
-                authenticationCode => {
-                    if (authenticationCode) {
-                        Identified.PostToServer(RouteStore.writeGooglePhotosAccessToken, { authenticationCode }).then(token => {
-                            this.isOpen = false;
-                            this.hasBeenClicked = false;
-                            resolve(token);
-                            disposer();
-                        });
+    public executeFullRoutine = async (service: string) => {
+        let response = await Identified.FetchFromServer(`/read${service}AccessToken`);
+        // 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;
+            return new Promise<string>(async resolve => {
+                const disposer = reaction(
+                    () => this.authenticationCode,
+                    authenticationCode => {
+                        if (authenticationCode) {
+                            Identified.PostToServer(`/write${service}AccessToken`, { authenticationCode }).then(token => {
+                                this.isOpen = false;
+                                this.hasBeenClicked = false;
+                                resolve(token);
+                                disposer();
+                            });
+                        }
                     }
-                }
-            );
-        });
+                );
+            });
+        }
+        // otherwise, we already have a valid, stored access token
+        return response;
     }
 
     constructor(props: {}) {
-- 
cgit v1.2.3-70-g09d2

