2016-01-31 44 views
5

我正在關注Google's guide以註銷用戶。如何在頁面刷新後註銷用戶?

考慮到gapi.auth2將不確定刷新頁面後,我做的:

if (gapi.auth2) { 
    var auth2 = gapi.auth2.getAuthInstance(); 
    auth2.signOut(); 
} else { 
    gapi.load('auth2', function() { 
     gapi.auth2.init({ 
      client_id: 'myAppID', 
      cookiepolicy: 'single_host_origin' 
     }).signOut(); 
    }); 
} 

但我得到的其他塊uncaught exception: This method can only be invoked after the token manager is started

我也嘗試將auth實例存儲在本地存儲中,但這樣做會導致一些週期性的對象值錯誤,同時對其進行字符串化。

一個更多鈔票的解決辦法是做一個

document.location.href = "https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=myUrl"; 

,但不用了只有我的應用程序將用戶登錄,這將影響到所有谷歌的服務中,他被記錄,除了做不必要的重定向。

有沒有不同的方法?

+0

類似的問題:[?如何註銷一個應用程序,我使用的OAuth2到登錄隨着谷歌的] (http://stackoverflow.com/questions/12909332/how-to-logout-of-an-application-where-i-used-oauth2-to-login-with-google) – Roberto

回答

5

不是檢索單身的GoogleAuth庫,並在我的登錄頁面控制器設置客戶端的,我曾在index.html文件來初始化它:

<script src="https://apis.google.com/js/api:client.js?onload=start" async defer></script> 
<script> 
    function start() { 
     gapi.load('auth2', function() { 
     auth2 = gapi.auth2.init({ 
      client_id: 'myAppID', 
      cookiepolicy: 'single_host_origin' 
     }); 
     }); 
    } 
</script> 

這解決了這個問題,註銷。但是,如果登錄頁面已刷新,其控制器邏輯將在定義gapi.auth2之前執行,並且將點擊處理程序成功附加到登錄按鈕將不可行。

爲了避免 - 雖然不是一個完美的解決方案 - 我用$interval等到gapi.auth2被初始化:

waitForAuth2Initialization = $interval(function() { 
    console.log("Checking..."); 
    if (!gapi.auth2) 
     return; 
    console.log("Ok, gapi.auth2 is not undefined anymore"); 
    var auth2 = gapi.auth2.getAuthInstance(); 
    // Attach signin 
    auth2.attachClickHandler... 

    $interval.cancel(waitForAuth2Initialization); 
}, 50); 

編輯:另一種可能的解決方案是使用一個承諾控制器邏輯回調等待,直到承諾解決,即在Google API完全加載並且gapi.auth2可以使用之前。 它是更多鈔票做以實現:

<script src="https://apis.google.com/js/api:client.js?onload=start" async defer></script> 
<script> 
    gapiPromise = (function() { 
     var deferred = $.Deferred(); 
     window.start = function() { 
      deferred.resolve(gapi); 
     }; 
     return deferred.promise(); 
    }()); 
    auth2Promise = gapiPromise.then(function() { 
     var deferred = $.Deferred(); 
     gapi.load('auth2', function() { 
      auth2 = gapi.auth2.init({ 
       client_id: 'myAppID', 
       cookiepolicy: 'single_host_origin' 
      }).then(function() { 
       deferred.resolve(gapi.auth2); 
      }); 
     }); 
     return deferred.promise(); 
    }); 
</script> 

然後在控制器:

auth2Promise.then(function() { 
    console.log("Ok, gapi.auth2 is not undefined anymore"); 
    var auth2 = gapi.auth2.getAuthInstance(); 
    // Attach signin 
    auth2.attachClickHandler... 
}); 

但是,這種方法的缺點是,它是比較慢(考慮了兩倍的時間點擊處理程序被附加)比第一個使用$interval

6

還有一個更簡單的方法,你只需要調用gapi.auth2.init後調用。然後

gapi.load('auth2', function() { 
    var auth2 = gapi.auth2.init({ 
     client_id: 'myAppID', 
     cookiepolicy: 'single_host_origin' 
    }); 
    auth2.then(function(){ 
     // this get called right after token manager is started 
     auth2.signOut(); 
    }); 
}); 
相關問題