2016-01-31 11 views
5

私はGoogle's guideをフォローしています。ページの更新後にユーザーをログアウトするにはどうすればよいですか?

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取得:

gapi.auth2は、ページを更新した後、私がやっている不定になりますことを考慮。

私はまた、ローカルストレージにauthインスタンスを格納しようとしましたが、それを実行すると、文字列化中に循環オブジェクトの値のエラーが発生しました。

一つかのうソリューションは、彼が不要なリダイレクトを行うほか、ログに記録されているすべてのGoogleのサービスに影響を与えることはなく、アウトだけで自分のアプリケーションのユーザーにログインするが、

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

を行うことです。

別の方法がありますか?代わりにGoogleAuthライブラリのシングルトンを取得し、私のサインインページのコントローラにクライアントを設定する、私はindex.htmlファイルでそれを初期化しなければならなかった

+0

同様の質問を呼び出した後.then呼び出す必要があります(http://stackoverflow.com/questions/12909332/how-to-logout-of-an-application-where-i-used-oauth2-to-login-with-google) – Roberto

答えて

5

<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が定義される前に実行され、クリックハンドラをサインインボタンに正常に接続することはできません。それを避けるために

- エレガントな解決策であることはないが - 、私はgapi.auth2が初期化されるまで待つように$intervalを使用:

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); 

EDIT:別の可能な解決策は約束を使用することです約束が解決されるまで、つまり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> 

をそしてコントローラで:(のための2倍の時間を取って

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

しかし、このアプローチの欠点は、それが遅いことである 実行してそれを達成することがかのうです接続するクリックハンドラ)は、$intervalを使用する最初のハンドラよりも優先されます。 [私はグーグルでログインをのOAuth2を使用したアプリケーションからログアウトするには?]:簡単な方法がある

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(); 
    }); 
}); 
関連する問題