oauth 2.0 - Check for returning users. Google sign-in session (JavaScript) -


i'm trying implement google sign-in using new api : https://developers.google.com/identity/sign-in/web/
sign in, out, , displaying user data works fine.
i'm having trouble make webpage remember user signed in when refreshes page or leaves site , comes back. i'll show (in console, example) user data, hide button etc..
it seems have use listeners explained here, , marked solved, still can't make work.
this have working:

<!doctype html> <html> <head>   <script src="https://apis.google.com/js/platform.js" async defer></script>     <meta name="google-signin-client_id" content="***.apps.googleusercontent.com">    <script type="text/javascript">       function onsignin(googleuser) {     var profile = googleuser.getbasicprofile();         console.log('id: ' + profile.getid()); // not send backend! use id token instead.     console.log('name: ' + profile.getname());     console.log('image url: ' + profile.getimageurl());     console.log('email: ' + profile.getemail());   }   function signout() {     var auth2 = gapi.auth2.getauthinstance();             auth2.disconnect().then(function () {               console.log('user signed out.');     });   }   </script>  </head> <body>   <div id="but" class="g-signin2" data-onsuccess="onsignin"></div>   <a href="#" onclick="signout();">sign out</a> </body> </html> 

as i've been here while, apreciate full working example. thanks!

it looks missing callback on loading platform.js , not initializing client. change include to:

  <script src="https://apis.google.com/js/client:platform.js?onload=startapp" async defer></script> 

then can perform post-initialization steps in startapp, example:

function startapp() {   gapi.load('auth2', function() {     gapi.client.load('plus','v1').then(function() {       gapi.signin2.render('signin-button', {           scope: 'profile',           fetch_basic_profile: false });       gapi.auth2.init({fetch_basic_profile: false,           scope:'profile'}).then(             function (){               console.log('init');               auth2 = gapi.auth2.getauthinstance();               auth2.issignedin.listen( function() {                   console.log(auth2.currentuser.get());                 });               auth2.then(function(resp){                   console.log(auth2.currentuser.get());                 });             });     });   }); } 

i put a demo here.


Comments