我正在努力在网络应用程序上添加谷歌登录流程。试图弄清楚我们是否可以实现这个功能,
如果用户在多个 google 帐户中登录,则显示多登录弹出窗口,用户可以在其中选择帐户。
如果用户只登录了一个 google 帐户并且之前已经登录了应用程序,则绕过登录页面并将他们定向到应用程序。
我想auth2.signIn,它会检查有多少帐户存在,并在此基础上实现上述用例。
作为进一步的上下文,登录页面代码的 javascript 当前是:
<script src="https://apis.google.com/js/platform.js?onload=init_google_signin" async defer></script>
<script>
function init_google_signin() {
var auhtInstance,
clientID = 'client_id';
auhtInstance = gapi.load('auth2', function () {
/**
* Retrieve the singleton for the GoogleAuth library and set up the
* client.
*/
auth2 = gapi.auth2.init({
client_id: clientID,
scope: 'email profile'
});
});
}
</script>
此外,谷歌登录按钮点击处理程序写成,
function handleGoogleSignIn() {
var GoogleAuth,
currentUser,
googleAuthPromise;
GoogleAuth = gapi.auth2.getAuthInstance();
currentUser = GoogleAuth.currentUser.get();
if (currentUser && currentUser.isSignedIn()) {
// submit form with access_token and user info, service will redirect user to logged-in page
setGoogleFormValues(currentUser, currentUser.getBasicProfile());
} else {
googleAuthPromise = GoogleAuth.signIn();
googleAuthPromise.then(function (user) {
// submit form with access_token and user info, service will redirect user to logged-in page
setGoogleFormValues(currentUser, currentUser.getBasicProfile());
});
}
}
为了解决这个问题,我试过了,
当用户从应用程序注销时,从谷歌注销
GoogleAuth.signOut();当用户点击谷歌登录按钮时,首先撤销访问然后它总是会提示登录弹出窗口
GoogleAuth.disconnect();在登录时使用
prompt: 'select_account',所以 handleGoogleSignIn 看起来像,function handleGoogleSignIn() { var GoogleAuth, currentUser, googleAuthPromise; GoogleAuth = gapi.auth2.getAuthInstance(); currentUser = GoogleAuth.currentUser.get(); googleAuthPromise = GoogleAuth.signIn(prompt: 'select_account'); googleAuthPromise.then(function (user) { // submit form with access_token and user info, service will redirect user to logged-in page setGoogleFormValues(currentUser, currentUser.getBasicProfile()); }); } }
这三个修复程序总是打开弹出窗口,因此它修复了第一个用例但在第二个用例上失败了。
我真的很感激关于如何实现这两个用例的想法。
非常感谢!