var webRoot = 'https://localhost:8080/'; if(typeof window !== 'undefined'){ webRoot = 'https://' + window.location.host + '/'; if (window.location.hostname == 'localhost' || window.location.hostname == '127.0.0.1' || ((window.location.port != "") && (window.location.port > 1023))) { // We're probably running against the DevAppServer webRoot = 'http://' + window.location.host.replace("8081","8080") + '/'; } } var apiRoot = webRoot + '_ah/api'; var ApiLoader = function(config){ this.config = config; this.scopes = config.scopes; var me = this; this.apis = config.apis; if(!this.apis){ this.apis = []; } this.reload = function(){ if(!gapi.auth2){ console.error("not init yet"); return; } var user = getUserInfo(); if(me.config.callback){ me.config.callback(user); } setUserElements(user); } var getUserInfo = function(){ var defaultUser = {isSignedIn:false}; var googleAuth = gapi.auth2.getAuthInstance(); if(!googleAuth){ return defaultUser; } var user = googleAuth.currentUser.get().getBasicProfile(); if(!user){ return defaultUser; } user.isSignedIn = googleAuth.isSignedIn.get(); return user; } this.signIn = function(options){ var googleAuth = gapi.auth2.getAuthInstance(); if(!options){ options = {}; } options.scope = me.scopes.join(" "); options.ux_mode = "redirect"; localStorage.urlBeforeSignIn = window.location.href; return googleAuth.signIn(options).then(()=>{ me.reload(); }); } this.signInWithPrompt = function(options){ //return gapi.auth2.getAuthInstance().disconnect().then(()=>{ return this.signIn({"prompt":"select_account"}); //}); } this.load = function(){ gapi.load('client:auth2', ()=>{ var discoveryDocs = me.apis.select(api=>{ var discoveryDoc = api.setRoot ? apiRoot + "/" : "https://www.googleapis.com/"; discoveryDoc += `discovery/v1/apis/${api.name}/${api.version}/rest`; return discoveryDoc; }); var options = { discoveryDocs: discoveryDocs, clientId: me.config.clientId, }; options.scope = me.scopes.join(" "); return gapi.client.init(options).then(()=>{ console.log("Done loading APIs!"); if(me.config.callback){ if(localStorage.urlBeforeSignIn){ var location = localStorage.urlBeforeSignIn; delete localStorage.urlBeforeSignIn; window.location.href = location; return; } var user = getUserInfo(); if(!user.isSignedIn){ if(me.config.autoSignIn){ me.signIn(); }else{ me.config.callback(user); } }else{ setUserElements(user); me.config.callback(user); } } }); }); } var setUserElements = function(user){ if(!user || !user.isSignedIn){ return; } if(me.config.elementSwitchAccounts){ var element = document.getElementById(me.config.elementSwitchAccounts); if(element){ if(element.tagName == "IMG"){ element.src = user.getImageUrl(); } element.onclick = e => apiLoader.signInWithPrompt(); element.style.cursor = "pointer"; } } if(me.config.elementUserName){ var element = document.getElementById(me.config.elementUserName); if(element){ element.innerHTML = user.getName(); } } } this.getURLParameter = function(name) { var url = window.location.href; var value = decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(url)||[,""])[1].replace(/\+/g, '%20'))||null; /*if(value && url.indexOf(this.config.clientId) < 0){ localStorage[name] = value; }else{ value = localStorage[name]; }*/ return value; } }