gapi.auth.signOut();不工作我迷路了 [英] gapi.auth.signOut(); not working I'm lost

查看:56
本文介绍了gapi.auth.signOut();不工作我迷路了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我用来登录google的代码.我在login.php上有一个ID为authorize-button的元素.当单击它登录就好了.

Below is the code I am using to login with google. I have an element on login.php with id authorize-button. When clicked it logs in just fine.

我的头文件中有一个注销链接.当我单击注销时,它会调用gapi.auth.signOut();,然后它会破坏会话并重定向回login.php

I have a logout link in my header file. When I click the logout it calls gapi.auth.signOut(); then it destroys session and redirects back to login.php

据我所知,这确实发生了,但随后它只是将用户直接登录到Google的我们的网站中.这是一种痛苦,因为我们的某些用户从google登录切换为facebook登录.

This happens as far as I can tell but then it just logs the user right back into our site with google. This is a pain as some of our users switch from google to facebook logins.

在此先感谢您的帮助.

function handleClientLoad() {
    gapi.client.setApiKey(apiKey);
    window.setTimeout(checkAuth, 1);
}

function checkAuth() {
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
}

function handleAuthResult(authResult) {
    var authorizeButton = document.getElementById('authorize-button');


    if (authResult && !authResult.error) {
        //authorizeButton.style.visibility = 'hidden';
        makeApiCall();
    } else {
        //authorizeButton.style.visibility = '';
        authorizeButton.onclick = handleAuthClick;
    }
}

function handleAuthClick(event) {
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
    return false;
}

function signOut() {
    gapi.auth.signOut();
}


function makeApiCall() {

    gapi.client.load('oauth2', 'v2', function() {
        var request = gapi.client.oauth2.userinfo.get();

        request.execute(function(logResponse) {

            var myJSON = {
                "myFirstName": logResponse.given_name,
                "myLastName": logResponse.family_name,
                "name": logResponse.name,
                "socialEmailAddress": logResponse.email
            };

            gapi.client.load('plus', 'v1', function() {

                var request = gapi.client.plus.people.get({
                    'userId': 'me'
                });
                request.execute(function(logResponse2) {
                    //alert(JSON.stringify(logResponse));
                    myJSON['profilePicture'] = logResponse2.image.url;
                    myJSON['socialId'] = logResponse2.id;
                    //alert(JSON.stringify(myJSON));
                    $.ajax({
                        type: "POST",
                        url: "includes/login-ajax.php",
                        data: "function=googleLogin&data=" + JSON.stringify(myJSON),
                        dataType: "html",
                        success: function(msg) {

                            if (msg == 1) {

                                //window.location = "settings.php";
                            }
                        }
                    });
                });
            });
        });
    });
}

推荐答案

请确保在登录按钮代码中将cookie策略设置为除none之外的其他值.例如:

Make sure you have set your cookie-policy to a value other than none in your sign-in button code. For example:

function handleAuthClick(event) {
  gapi.auth.authorize(
    {
      client_id: clientId, 
      scope: scopes, 
      immediate: false, 
      cookie_policy: 'single_host_origin'
    },
    handleAuthResult);
  return false;
}

请注意,如果您是从localhost运行的,则注销将不起作用.

Note that sign out will not work if you are running from localhost.

这篇关于gapi.auth.signOut();不工作我迷路了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆