loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Expired accessToken are not removed from the table automatically.

Open ghost opened this issue 8 years ago • 3 comments

When I use MySQL data source, expired accessToken are not removed from the table automatically.

ghost avatar Sep 03 '18 14:09 ghost

Is this a database connector issue or a usage issue ? normally accessTokens are destroyed on logout or when trying to use an expired accessToken on a API call. (see functions User.logout and AccessToken.prototype.validate) With a web frontend it is not guaranteed that the user logs out or uses the accessToken until it expires. You could do a validate on the related accessTokens in the User.afterRemote('login',function (ctx, token, next){...}) so expired tokens are destroyed after each login

cleas avatar Sep 25 '18 12:09 cleas

Thank you for your reply. I did not connect other data source but I do not think it is a database connector issue. So I have to write the code of destroying the expired accessToken in the User.afterRemote('login'), how to write the validation condition?

ghost avatar Sep 28 '18 00:09 ghost

The validate function is already implemented in accessToken.

 User.afterRemote('login', function (ctx, token, next) {
    User.findById(token.userId,function(err,user){
      if(!err && user) {
        var destroyedCount = 0;
        var validCount = 0;
        user.accessTokens(function (err, accessTokens) {
          accessTokens.forEach(function (accessToken) {
            accessToken.validate(function (err, isValid){
              if(isValid) validCount++;
              else
                destroyedCount++;
            })
          });
          console.log('AccessTokens: valid: %d, destroyed: %d',validCount,destroyedCount);
        });
      }
    });
    next();
  });

cleas avatar Sep 28 '18 07:09 cleas