colinskow / ng-superlogin

AngularJS bindings for the SuperLogin project

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Impossible to logout properly.

ronycohen opened this issue · comments

Hello,

When I try to logout the server answers :

image

I have a session in the localStorage.
{"issued":1449071790342,"expires":1449762990342,"provider":"local","ip":"::ffff:127.0.0.1","token":"_","password":"_","user_id":"**_","roles":["roles","admin"],"userDBs":{"appglobal":"https://__:__@__.cloudant.com/GLOBALNAME","__":"https://_**********:*****_@_.cloudant.com/DBNAME"},"serverTimeDiff":-1673}

Did I missed something ?

            logOut : function(){
                console.log("superlogin logOut");
                var deferred = $q.defer();
       superlogin.logout("Bye !")
                        .then(function(res){
                            console.log("logout",res);
                        })
                        .catch(function(e){
                            console.error(e);
                        });

app-1 POST /auth/login 200 4185.147 ms - 659
app-1 OPTIONS /auth/logout 200 0.836 ms - 4
app-1 { error: 'unauthorized', status: 401 }

{"error":"unauthorized","status":401}
image

logout: function(msg) {
              return $http.post(superloginSession.getConfig().baseUrl + 'logout', {})   <=== HERE 
                  .then(function(res) {
                    console.log("logout Superlogin", res);
                    superloginSession.deleteSession();
                    $rootScope.$broadcast('sl:logout', msg || 'Logged out');
                    return $q.when(res.data);
                  }, function(err) {
                    console.error("logout Superlogin err", err);
                    superloginSession.deleteSession();
                    $rootScope.$broadcast('sl:logout', msg || 'Logged out');
                    return $q.reject(err.data);
                  });
            },

Do we need to add the session ID there ? (<====).

The $http interceptor should automatically insert the session id into the request, so there is no need to do it manually. Are you getting this same error with SuperLogin Demo?

Long time no use the Demo.
I can retry with it.

It works perfectly for me. If SuperLogin is throwing an unauthorized error, it is because the session has already been logged out somehow. Are you using the latest version of SuperLogin on your server?

I have the "version": "0.5.0".
I maybe have something which impact http interceptions in my ionic app.

Is there a way to force the session id ?

I think it's probably Ionic which block interceptor.
The session hasn't been logged out because I still have an existing access on Remote DBs.

SomeHow It doesn't get the session :

The session is null.

from superloginInterceptor :

service.request = function(request) {
                    var session = superloginSession.getSession();
                    if(session && session.token) {
                        superloginSession.checkRefresh();
                    }
                    if(checkEndpoint(request.url, endpoints)) {

//HERE !!!!!!!!!!!!!!!!!
 console.log("Call superloginInterceptor checkEndpoint", request.url, endpoints, session);

                        if(session && session.token) {
                            request.headers.Authorization = 'Bearer ' + session.token + ':' + session.password;
                        }
                    }
                    return request;
                };

image

I can see that The Superlogin interceptor checkEndpoint is called on each internal ionic view call.

image

Is your endpoint properly configured in ng-superlogin?

I guess Yes :
endpoints: ['api.*********.net/superlogin'],
// Set this to true if you do not want the URL bar host automatically added to the list
noDefaultEndpoint: false,

I'm able to sign-in and sign-up properly.

Do you know a way to debug it ? I mean I know I don't give enough details...

logout: function(msg) {
          return $http.post(superloginSession.getConfig().baseUrl + 'logout', {})
            .then(function(res) {
              superloginSession.deleteSession();
              $rootScope.$broadcast('sl:logout', msg || 'Logged out');
              return $q.when(res.data);
            }, function(err) {
              // Log the headers and make sure 'Authorization' is there.
              console.log(err.headers('Authorization'));
              superloginSession.deleteSession();
              $rootScope.$broadcast('sl:logout', msg || 'Logged out');
              return $q.reject(err.data);
            });

Thank you Colin !
I try with this function

You may need to use err.config.headers('Authorization')

Hi,

// Log the headers and make sure 'Authorization' is there.
console.log(err.headers('Authorization'));

render a null value.

and err.config.headers is not a function.

Alright, then if it's working properly for the Signin and the Signup.
But not for the logout and the refresh.
This means that I lost this value somewhere and the http interceptions can't get anything.

Here is my client config :

        var superLoginConfig = {
            baseUrl: 'https://api.myapp.net/superlogin/auth/',
            endpoints: ['api.myapp.net/superlogin'],
            // Set this to true if you do not want the URL bar host automatically added to the list
            noDefaultEndpoint: true,
            storage: 'local',
            providers: ['facebook','google'],
            checkExpired: 'startup',
            refreshThreshold: 0.5
        };
        superloginProvider.configure(superLoginConfig);
    }]);

Do you how I can track this 'Authorization' value within SuperLogin after the Login in order to identify how I loose it ?

When I define noDefaultEndpoint to true, I don't have superloginInterceptor logs.

Please, I just need a way to force the an endpoint...

Try this to debug:

    service.request = function(request) {
      var session = superloginSession.getSession();
      if(session && session.token) {
        superloginSession.checkRefresh();
      }
      if(checkEndpoint(request.url, endpoints)) {
        if(session && session.token) {
          // DEBUG AUTH HEADER
          console.log('Setting authorization header');
          request.headers.Authorization = 'Bearer ' + session.token + ':' + session.password;
        }
      }
      return request;
    };

ok I test now

The checkEndpoint(request.url, endpoints) always return a false for me.

On logout :
parser.host = api.myapp.net
endpoints[i] = api.myapp.net/superlogin

function checkEndpoint(url, endpoints) {
                    parser.href = url;
                    console.debug("checkEndpoint", parser, endpoints);

                    for(var i=0; i<endpoints.length; i++) {
                        console.debug("parser.host",parser.host); // ==>  api.myapp.net
                        console.debug("endpoints[i]",endpoints[i]); // ==> api.myapp.net/superlogin

                        if(parser.host === endpoints[i]) {
                            return true;
                        }
                    }
                    return false;
                }

So I changed the if.

==>> if(endpoints[i].indexOf(parser.host) > -1) {
And now it's working !! :)

Thank you so much Colin,

It a mistake I made but defining :
endpoints: ['api.myapp.net/superlogin'],
instead of endpoints: ['api.myapp.net],

Your endpoint needs to be simply api.myapp.net. You need to lose the path.

thank you !
Yes I changed it.