C
C
Costa Concordia2016-09-06 13:55:36
JavaScript
Costa Concordia, 2016-09-06 13:55:36

How to monitor cookie expiration without reloading the page?

I write a token in cookies, and give it the same expiration date as on the server, but it is reset only after the page is reloaded, but do I need to always check the expiration date by default? How to be? Code after the config in the
.run() block

var expireDate = new Date();
    expireDate.setTime(expireDate.getTime() + (4 * 60 * 1000));

During authorization, I receive a token and write it to cookies, I pass the expiration date as the third parameter:
$cookies.put('token', token, {'expires': expireDate});

I check if there is a token, use it for its intended purpose and attach it to requests, if not, I do logout():
if($cookies.get('token')){
      $rootScope.token = $cookies.get('token');
      $rootScope.isLogin = true;
      console.log($rootScope.token);
    } else{
      $rootScope.logout();
      console.log('Токен просрочен');
    }

Everything works, the token expires in 4 minutes, but all this matches only after the page is reloaded, how to make the condition monitor the state of the token not only after the page is reloaded, but also in default mode?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeniy Odinets, 2016-09-06
@evgeniy2194

Check the information on the server on each request, and if the token is not active - return 401 and redirect to logout

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question