0

I have the following piece of code where the user is stored in the cookie. The expiry time for the cookie is also set. The user page is still showing even after the cookie has expired.

             var expired = new Date();
             expired.setTime(expired.getTime() + (60*1000));
             $cookieStore.put('user', user.username, {expires : expired });
iamtheDanger
  • 35
  • 2
  • 4
  • 9
  • when cookie get expired its more like deleted.. so if you try to read the cookie it will return null.. thus you can decide to redirect – Fisherman May 18 '15 at 12:07
  • Could you find a solution ? – sebastienbarbier May 19 '15 at 08:59
  • yes i was able to find the solution with a minor tweak in the code mentioned by @Fisherman. `$timeout(function() { $cookieStore.remove('user'); if (!$cookieStore.get('user')) { $window.location.href = '/login'; } }, 60000);` After 60 seconds the cookie expires and on refresh the page is redirected to login. – iamtheDanger May 19 '15 at 11:15

2 Answers2

0

What about on page loading, you get the expiration date and use a setTimeout() to refresh it or redirect your user on an other page ?

Some ressources about settimeout :

Best would be to display 1 minutes before expiration date a popup inviting user to update it or perform an action.

Community
  • 1
  • 1
sebastienbarbier
  • 6,542
  • 3
  • 29
  • 55
0
     var expired = new Date();
             expired.setTime(expired.getTime() + (60*1000));
             $cookieStore.put('user', user.username, {expires : expired });
             $timeout(function(){
                 if(!$cookieStore.get('user')){
                 $window.location.href = 'redirectlink';
}


},60);
Fisherman
  • 5,949
  • 1
  • 29
  • 35