伙计,我的函数在哪里?
正在尝试调用 GoogleUser.signIn()
结果 TypeError: user.signIn is not a function
目标
我想在用户让他们的计算机进入休眠状态后刷新过期的凭据(在单页应用程序中)。我已经使用 auth2.currentUser.listen
为事件浏览器窗口实现了凭据刷新如我的回答所述:How to refresh expired google sign-in logins?
由于让您的计算机进入休眠状态似乎会使此方法无效,因此我需要针对该极端情况的另一种解决方案。 GoogleUser.signIn()
似乎符合要求。
问题
Google 登录 API 中的文档 GoogleUser.signIn()
说:
Signs in the user. Use this method to request additional scopes for incremental authorization or to sign in a user after the user has signed out. When you use GoogleUser.signIn(), the sign-in flow skips the account chooser step.
对我来说重要的部分是最后一部分(这是与 GoogleAuth.signIn()
的主要区别)
the sign-in flow skips the account chooser step.
为什么我不想显示帐户选择器?这是一个非常糟糕的用户体验。他们可能刚刚登录到我的应用程序。他们没有注销,他们的计算机只是进入休眠状态(可能只是在他们与妈妈通电话时的一秒钟)。
不幸的是,尝试调用此函数会导致 TypeError: user.signIn is not a function
(我将对象存储在名为 user
的变量中)。
Ur Codez Suxor,大声笑
也许,这是一个简化的版本:
var Auth = {};
Auth.user = null;
// registered as handler for signin component
function onSignIn(googleUser){
Auth.user = googleUser;
var auth2 = gapi.auth2.getAuthInstance();
auth2.currentUser.listen(function(user){
Auth.user = user;
// refresh credentials with new user object
});
}
// called before any action that requires credentials
function getCredentials(callback){
var expired = Auth.user.getAuthResponse().expires_at < Date.now();
if(expired){
var p = Auth.user.signIn(); // <- throws TypeError
p.then(function(){
Auth.user = auth2.currentUser.get();
// refresh credentials here, too
});
} else {
return callback();
}
}
最佳答案
尝试使用 GoogleAuth.signIn()
而不是 GoogleUser.signIn()
。
例如:
var auth2 = gapi.auth2.getAuthInstance();
auth2.signIn().then(function() {
....
});
关于javascript - GoogleUser.signIn 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37617313/