Google Sign In API: isSignedIn.get() returning inconsistent values

╄→尐↘猪︶ㄣ 提交于 2019-12-04 03:24:30

问题


I'm trying to check if a user is signed in or not, but I'm getting really inconsistent results that seem to have some sort of race condition involved. I basically took the code Google Developer Website:

gapi.load('auth2', function() {
  auth2 = gapi.auth2.init({
    client_id: 'my_client_id.apps.googleusercontent.com',
  });

  console.log(auth2.isSignedIn.get());

  setTimeout(function(){console.log(auth2.isSignedIn.get())},50);

  setTimeout(function(){console.log(auth2.isSignedIn.get())},500);

  setTimeout(function(){console.log(auth2.isSignedIn.get())},1000);
});

For some reason, the first two return false, and the second to return true. I have double checked, and I am signed in, so it seems like the first two should be returning true. What am I missing her? I've looked at the documentation, and there doesn't seem to be anything that indicated something asynchronous happening, and I'm not sure what I should be waiting on before I can get a reliable result from the isSignedIn call.


回答1:


Maybe I missed it in the documentation somewhere (if I did, it'd be nice if someone could point it out to me), but it looks like you can use Promises to make sure the GoogleAuth instance is ready. Here's what I did to get a consistent result:

gapi.load('auth2', function() {

  gapi.auth2.init({

    client_id: 'my_client_info.apps.googleusercontent.com',

  }).then(function(){

    auth2 = gapi.auth2.getAuthInstance();
    console.log(auth2.isSignedIn.get()); //now this always returns correctly        

  });
});


来源:https://stackoverflow.com/questions/33378710/google-sign-in-api-issignedin-get-returning-inconsistent-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!