我正在尝试使用他们的新 API 实现 google 登录:https ://developers.google.com/identity/sign-in/web/
登录和注销工作正常。我的问题是我不知道如何在没有服务器端的情况下管理其他页面上的会话。
所以我尝试了这段代码 - https://developers.google.com/identity/sign-in/web/session-state
它对我来说效果不佳。我不想在每个页面上都有 google 登录按钮。如果我删除“auth2.attachClickHandler ..”部分,整个代码都不起作用。
我想要的只是在其他页面中(而不是在带有 google 按钮的页面中)指示用户是否仍然连接到。你能帮助我吗?
编辑: 我尝试了答案中建议的以下代码,但我收到一条错误消息:“Uncaught TypeError: Cannot read property 'init' of undefined”
代码:
var auth2 = gapi.auth2.init({
client_id : 'ID.apps.googleusercontent.com'
});
auth2.then(function() {
var isSignedIn = auth2.isSignedIn.get();
var currentUser = auth2.currentUser.get();
if (isSignedIn) {
console.log("signed in");
// User is signed in.
// Pass currentUser to onSignIn callback.
} else {
console.log("NOT signed in");
// User is not signed in.
// call auth2.attachClickHandler
// or even better call gapi.signin2.render
}
});