谷歌正在弃用旧的登录按钮 https://developers.google.com/identity/sign-in/web/sign-in有利于新的那一个 https://developers.google.com/identity/gsi/web。我有一个单页迷你应用程序,其中服务器仅返回静态页面内容。该页面要求用户登录并在客户端处理返回的令牌。
旧按钮很好地处理了页面重新加载的情况,立即使用新令牌调用回调。但是,新按钮似乎无法处理页面重新加载。
当使用带有 HTML 的 JavaScript API 时,例如:
<div id="parent" />
<script>
function initGoogleSignIn() {
google.accounts.id.initialize({
client_id: 'YOUR_GOOGLE_CLIENT_ID',
auto_select: true,
callback: onSignIn,
});
google.accounts.id.renderButton(document.getElementById('parent'),{});
}
function onSignIn(payload) {
let unverifiedResponsePayload = JSON.parse(atob(payload.credential.split('.')[1])); //this is just an example - instead you should _verify_ the token before any actual use
console.log(unverifiedResponsePayload.email);
}
</script>
<script src="https://accounts.google.com/gsi/client" onload="initGoogleSignIn()"></script>
根据文档 https://developers.google.com/identity/gsi/web/reference/js-reference#auto_select将 auto_select 设置为 true 应该会导致“当之前只有一个 Google 会话批准了您的应用程序时,无需任何用户交互即可自动返回 ID 令牌。”但是,登录然后重新加载页面不会调用回调。旧按钮确实会以类似的设置调用回调。
问题是如何实现获取令牌的旧行为,而无需使用新按钮重新加载页面上的用户交互?
我发现,可以配置“一键”,这将导致在页面重新加载时调用回调,但只有在显示一键块和几秒钟的延迟后才调用。
我仍然想知道这是否是一个预期的行为,只是没有正确记录,或者文档是正确的,并且该行为将由 Google 在库中修复。
这是如何初始化Google登录可以更改功能以使用“一键”:
function initGoogleSignIn() {
google.accounts.id.initialize({
client_id: 'YOUR_GOOGLE_CLIENT_ID',
auto_select: true,
callback: onSignIn,
prompt_parent_id: 'parent', //DOM ID of the container element for One Tap block
});
google.accounts.id.prompt((notification) => {
if (notification.isNotDisplayed() || notification.isSkippedMoment() || (notification.isDismissedMoment() && notification.getDismissedReason() != 'credential_returned')) {
// no sign-in happened, display the button
google.accounts.id.renderButton(document.getElementById('parent'),{});
}
});
}
这种方法仍然会为调用回调增加明显的延迟(在我的例子中>4秒)。旧按钮的速度要快得多,而且没有任何视觉表示。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)