From a1629f937ced8e7620362086e98fb9f84888a246 Mon Sep 17 00:00:00 2001 From: Alex Krizhanovsky Date: Sat, 5 Mar 2016 00:00:09 +0300 Subject: [PATCH] Wrap GAPI.init into api.load. Fix - "Cannot read property 'access_token' of undefined" --- gapi.js | 77 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/gapi.js b/gapi.js index 3170b9c..11e8d07 100644 --- a/gapi.js +++ b/gapi.js @@ -371,45 +371,50 @@ angular.module('gapi', []) */ GAPI.init = function () { - var app = GAPI.app, - deferred = $q.defer(), - attemptCounter = 0, - onAuth = function (response) { - attemptCounter++; - if(attemptCounter > 3) { - deferred.reject('Login attempt failed. Attempted to login ' + attemptCounter + ' times.'); - return; - } - // The response could tell us the user is not logged in. - if(response && !response.error) { - if(response.status && response.status.signed_in === true) { - app.oauthToken = gapi.auth.getToken(); - deferred.resolve(app); - } else { - deferred.reject("App failed to log-in to Google API services."); - } - } else { - deferred.notify('Login attempt failed. Trying again. Attempt #' + attemptCounter); - gapi.auth.authorize({ - client_id: app.clientId, - scope: app.scopes, - immediate: true - }, onAuth - ); - } - }; - deferred.notify('Trying to log-in to Google API services.'); + gapi.load('auth', function() { + + var app = GAPI.app, + deferred = $q.defer(), + attemptCounter = 0, + onAuth = function (response) { + attemptCounter++; + if(attemptCounter > 3) { + deferred.reject('Login attempt failed. Attempted to login ' + attemptCounter + ' times.'); + return; + } + // The response could tell us the user is not logged in. + if(response && !response.error) { + if(response.status && response.status.signed_in === true) { + app.oauthToken = gapi.auth.getToken(); + deferred.resolve(app); + } else { + deferred.reject("App failed to log-in to Google API services."); + } + } else { + deferred.notify('Login attempt failed. Trying again. Attempt #' + attemptCounter); + gapi.auth.authorize({ + client_id: app.clientId, + scope: app.scopes, + immediate: true + }, onAuth + ); + } + }; + + deferred.notify('Trying to log-in to Google API services.'); + + gapi.auth.authorize({ + client_id: app.clientId, + scope: app.scopes, + immediate: true + }, onAuth + ); - gapi.auth.authorize({ - client_id: app.clientId, - scope: app.scopes, - immediate: true - }, onAuth - ); + return deferred.promise; - return deferred.promise; - } + }); + }; return GAPI; })