Google Sign In Button — How to get id_token on server when using redirect flow

和自甴很熟 提交于 2019-12-30 06:24:49

问题


I'm adding a "Sign In with Google" federated login button to my site. For desktop I'm using the popup window which works great like this:

gapi.auth2.init();
gapi.auth2.getAuthInstance().signIn().then(function(user) {
  var id_token = user.getAuthResponse().id_token;
  // ajax call to pass this to server
});

However on mobile we want to use redirects instead of popups, since separate tabs are a little awkward in mobile browsers. I just change to:

gapi.auth2.init({
  ux_mode: 'redirect',
  redirect_uri: 'http://example.com/google_login/'
});

This works but it adds the id_token I need as a hash fragment, which the server can't see. It ends up at:

http://example.com/google_login/#id_token=ASDFASDFASDFASDF

I guess I could render a connector page with a script tag the pulls out window.location.hash and redirects to itself with that as a query param, but seems crazy I'd have to do that. Is there a better way?

来源:https://stackoverflow.com/questions/44729443/google-sign-in-button-how-to-get-id-token-on-server-when-using-redirect-flow

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