Google + Signin using Google javascript client lib

2019-07-14 22:32发布

I am doing googlre sign in in my app using

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

I am able to authorize the user ,then i gave a link to gign out the user using this api

gapi.auth.signOut();

it is also signing out the user, but when i refresh the page then it is not asking to sign in again. it is directly loading user account, i want to make it to ask user to again login. Can any body tell me how to do it.

2条回答
Viruses.
2楼-- · 2019-07-14 22:44

I tried with @class answer, it still did not work with signOut(), there is no error when we call the gapi.auth.authorize. But I found another way, try to access the link https://accounts.google.com/o/oauth2/revoke?token=authResult['access_token'], this will make you like signout, user need to reaccept the autherize. Althought I am little confused now, which way we should use to signOut();

查看更多
我只想做你的唯一
3楼-- · 2019-07-14 22:45

If you are running on localhost, signout will not work.

What you are doing looks like good to me. Perhaps you are missing a check for the user being signed out in your handleAuthResult method. This could be happening because the callback method is going to be triggered even when the user is not signed in. To check this, you should be ensuring that you are getting an access token in your callback before changing the sign in status for your users. Some code to help that will also initialize the Google+ JavaScript API client:

handleAuthResult: function(authResult) {
  gapi.client.load('plus','v1', function(){
    $('#authResult').html('Auth Result:<br/>');
    for (var field in authResult) {
      $('#authResult').append(' ' + field + ': ' +
          authResult[field] + '<br/>');
    }
    if (authResult['access_token']) {
      $('#authOps').show('slow');
      $('#gConnect').hide();
      helper.profile();
      helper.people();
    } else if (authResult['error']) {
      // There was an error, which means the user is not signed in.
      // As an example, you can handle by writing to the console:
      console.log('There was an error: ' + authResult['error']);
      $('#authResult').append('Logged out');
      $('#authOps').hide('slow');
      $('#gConnect').show();
    }
    console.log('authResult', authResult);
  });
},

You can see the demo code working here. If you open your browser's JavaScript console, you will notice the following error message when the user is signed out:

authResult 
...
Object {error: "user_signed_out", 
cookie_policy: "single_host_origin"
error: "user_signed_out"
expires_at: "1388530488"
expires_in: "86400"
g-oauth-window: undefined
g_user_cookie_policy: "single_host_origin"
issued_at: "1388444088"
response_type: "code token id_token gsession"
scope: "https://www.googleapis.com/auth/plus.login 
session_state: "707059cda8acedf0acf31d83c713e9f16f232610..25c4"
status: Object
google_logged_in: true
method: null
signed_in: false
...

If the user is automatically / repeatedly signing out of the site, it's most likely due to a known issue where the cookie store is corrupted. If you are seeing this issue, either open the target page in an incognito window or delete the current cookies, e.g. run the following javascript in the developer console:

var cookies = document.cookie.split(";");

for (var i = 0; i < cookies.length; i++) {
    var cookie = cookies[i];
    var eqPos = cookie.indexOf("=");
    var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
    document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
查看更多
登录 后发表回答