![](/img/trans.png)
[英]Google Sign-in: how to avoid popup dialog if user is already signed in by using listeners?
[英]How to avoid the google Sign In Pop up when the user is already logged in?
我正在使用以下代碼使用戶通過google登錄。
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" type="text/css">
<script src="https://apis.google.com/js/api:client.js"></script>
<script>
var googleUser = {};
var startApp = function () {
gapi.load('auth2', function () {
// Retrieve the singleton for the GoogleAuth library and set up the client.
auth2 = gapi.auth2.init({
client_id: [my client id],
cookiepolicy: 'single_host_origin',
// Request scopes in addition to 'profile' and 'email'
scope: 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/plus.me'
});
});
};
function checkUser() {
if (auth2.isSignedIn.get()) {
onSignIn(auth2.currentUser.po.po);
}
else {
gapi.auth.authorize({ client_id: [my client id], scope: 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/plus.me', immediate: false }, onSignIn);
}
}
function onSignIn(googleUser) {
alert('Access Token' + googleUser.access_token);
//location.href = "ServerSide.aspx?acccessToken=" + googleUser.access_token;
}
</script>
<style type="text/css">
#customBtn {
display: inline-block;
background: #4285f4;
color: white;
width: 190px;
border-radius: 5px;
white-space: nowrap;
}
#customBtn:hover {
cursor: pointer;
}
span.label {
font-weight: bold;
}
span.icon {
/*background: url('/identity/sign-in/g-normal.png') transparent 5px 50% no-repeat;*/
display: inline-block;
vertical-align: middle;
width: 42px;
height: 42px;
border-right: #2265d4 1px solid;
}
span.buttonText {
display: inline-block;
vertical-align: middle;
padding-left: 42px;
padding-right: 42px;
font-size: 14px;
font-weight: bold;
/* Use the Roboto font that is loaded in the <head> */
font-family: 'Roboto', sans-serif;
}
</style>
<!-- In the callback, you would hide the gSignInWrapper element on a
successful sign in -->
<div id="gSignInWrapper" onclick="checkUser()">
<span class="label">Sign in with:</span>
<div id="customBtn" class="customGPlusSignIn">
<span class="icon"></span>`enter code here`
<span class="buttonText">Google</span>
</div>
</div>
<div id="name"></div>
<script>startApp();</script>
有了這個代碼,如果用戶甚至登錄到Google,彈出的聲音看起來就很奇怪。所以我想如果用戶登錄到Google彈出窗口不應該直接進入系統。 為此,我在按鈕單擊時使用了“ auth2.isSignedIn.get()”,但是即使用戶登錄到Google,此功能也總是給我錯誤。 我被困在這一點。如果您有任何辦法實現這一目標,請告訴我。
在Google身份驗證實例GoogleAuth.isSignedIn.get()上可以使用isSignedIn方法,請在下面使用
gapi.auth2.getAuthInstance()。isSignedIn.get()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.