当用户登陆页面时,谷歌身份验证被加载并启动。Google 登录 API 提供了一个侦听器函数,如果用户更改 (GoogleAuth.currentUser.listen()) 就会触发该函数。我想要实现的功能的预期行为包括观察用户在浏览器的其他选项卡中登录 gmail。
const userChanged = user => {
if (user) {
console.log('CHANGED TO USER: ', user);
}
};
const loadGoogleAuth = () => {
window.gapi.load('auth2', () => {
const gapiAuth = window.gapi.auth2;
gapiAuth.init({
client_id: '<YOUR-CLIENT-ID>.apps.googleusercontent.com',
cookie_policy: 'none', //my hunch is this could be affecting listener capabilities?
scope: 'profile email openid',
});
const authInstance = gapiAuth.getAuthInstance();
const element = document.getElementById('googleSignIn');
authInstance.attachClickHandler(
element,
{},
googleUser => {
console.log(`Signed in: ${googleUser.getBasicProfile().getName()}`);
},
error => {
console.log('Sign-in error', error);
}
);
authInstance.currentUser.listen(userChanged);
});
};
const withLifeCycles = lifecycle({
componentDidMount() {
if (window.gapi) {
loadGoogleAuth();
}
},
});
这是有效的,但仅适用于用户登录的第一个 gmail 或在用户登陆页面之前已经登录的 gmail。换句话说,如果我使用 Gmail A 登录,它会检测到用户更改,但如果我退出 Gmail A 并随后登录到 Gmail B,它就不会检测到 Gmail B。但是,它将随后检测 Gmail A 的重新登录,而不是任何其他 gmail 地址。侦听器功能是否可以检测任何 gmail 地址上的登录,或者仅检测检测到的第一个 gmail?