![](/img/trans.png)
[英]Google Sign-in: how to avoid popup dialog if user is already signed in by using listeners?
[英]Checking for signed in Google user with Google Sign-in
我试图使用GoogleAuth.currentUser.get()
函数来获取用户,但如文档所述 :
在新初始化的GoogleAuth实例中,当前用户尚未设置。 使用currentUser.listen()方法或GoogleAuth.then()获取初始化的GoogleAuth实例。
GoogleAuth.then(onInit, onFailure)
使用GoogleAuth.then(onInit, onFailure)
可以正确检索用户的登录状态。
/**
* The Sign-In client object.
*/
var auth2;
/**
* Initializes the Sign-In client.
*/
var initClient = function() {
gapi.load('auth2', function(){
/**
* Retrieve the singleton for the GoogleAuth library and set up the
* client.
*/
auth2 = gapi.auth2.init({
client_id: CLIENT_ID,
scope: 'profile'
});
// Called once auth2 has finished initializing
auth2.then(checkForLoggedInUser, onFailure);
});
};
function checkForLoggedInUser() {
var user = auth2.currentUser.get();
var profile = user.getBasicProfile();
console.log('Email: ' + profile.getEmail());
}
您可以使用GoogleAuth.isSignedIn.get()
方法:
// 1. Make gapi.auth2 available (using gapi.load("auth2", ...))
// 2. Initialize the library (using gapi.init({ ... }))
const isSignedIn = gapi.auth2.getAuthInstance().isSignedIn.get();
如果用户已登录Google,则isSignedIn
为true
,否则为false
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.