我正在努力在网络应用上添加google登录流程。试图弄清楚我们是否可以实现此功能,
如果用户使用多个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>
另外,Google登录按钮点击处理程序的编写方式为
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());
});
}
}
要解决此问题,我尝试过
当用户从应用程序注销时,从Google GoogleAuth.signOut();
当用户单击google登录按钮时,首先撤消访问权限,然后它将始终提示登录弹出窗口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());
});
}
}
这三个修复程序始终会打开弹出窗口,因此它修复了第一个用例,但在第二个用例上失败了。
我非常感谢有关如何实现两个用例的想法。
非常感谢!