Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signing out of Google+ using JS

I am trying to put a Google+ login button on my site. The button works perfectly. I have a Sign Out button also. I hide the sign in button when the user logs in.My code is here:

<span id="signinButton">
    <span
        class="g-signin"
        data-callback="signinCallback"
        data-clientid="******************"
        data-cookiepolicy="single_host_origin"
        data-requestvisibleactions="http://schema.org/AddAction"
        data-scope="https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email">
    </span>
</span> &nbsp;&nbsp;&nbsp;&nbsp;
<button id="revokeButton" onclick="gapi.auth.signOut()">Sign Out</button>

<script>
    function signinCallback(authResult) {
        if (authResult['status']['signed_in']) {
            document.getElementById('signinButton').setAttribute('style', 'display: none');
            console.log("User successfully logged in!!");
        } else {
            console.log('Sign-in state: ' + authResult['error']);
        }
    }
</script>
<script>
    function disconnectUser(access_token) {
        var revokeUrl = 'https://accounts.google.com/o/oauth2/revoke?token=' +
                access_token;

        // Perform an asynchronous GET request.
        $.ajax({
            type: 'GET',
            url: revokeUrl,
            async: false,
            contentType: "application/json",
            dataType: 'jsonp',
            success: function(nullResponse) {
                document.getElementById('signinButton').setAttribute('style', 'display: display');
                // The response is always undefined.
                console.log("Success in logging out!");
            },
            error: function(e) {
                // Handle the error
                console.log(e);
                // You could point users to manually disconnect if unsuccessful
                // https://plus.google.com/apps
            }
        });
    }
</script>

The problem is when I use gapi.auth.signOut() to sign out... it logs me out, but logs in the same user again to Google+ on refresh. How do I allow other people to login to my site. How do I completely logout people from Google. I am new to Javascript...an example would help.

like image 518
Mallik Kumar Avatar asked Nov 10 '22 15:11

Mallik Kumar


1 Answers

The answer as I figured out is simple. The first time the user logs in, he is not logged into my site . So obviously the user has to log in. The next time the page refreshes...Google+ checks if the user is already logged into any of Google services such as gmail etc.. and keeps the user logged in if the answer is true. To check whether this works...log out of any of your Google service(gmail etc) and try logging in with Google+. It will ask for credentials. So once the user logs in to Google+, he/she need not keep signing in every time.

like image 61
Mallik Kumar Avatar answered Nov 15 '22 04:11

Mallik Kumar