2

I'm new to Angular 6 and I did one pilot application with angular and I'm storing login credentials in local storage for the user authentication. now I want to call localStorage.clear() method when the user has not performed any action on my application in the delay of 30 minutes. Is it correct process to do? or else any other possibilities?

mepraveenk
  • 226
  • 1
  • 11
  • I think using .setTimeout would do the work indeed. – MrLine Feb 21 '19 at 07:33
  • You can have a look at [ng-idle](https://www.npmjs.com/package/@ng-idle/core) – Satpal Feb 21 '19 at 07:37
  • 1
    @Satpal Thank you for your information. that was a good module to fill my requirement.I have to implement on my application. – mepraveenk Feb 21 '19 at 07:44
  • 1
    You could add an `@HostListener` on mouse move that set a `this.variable = new Date()`. Then add a setTimetout of 30 mins that check that variable. If the time between now and the date stored in `this.variable` is >= 30, then clear the localStorage. – Jacopo Sciampi Feb 21 '19 at 07:48

2 Answers2

1

You can't. When do items in HTML5 local storage expire? The only thing you can do is set the delete statement in a timeout of 1 hour. This requires the user to stay on your page or the timeout won't be executed.

You can also set an expiration field. When the user revisits your site, check the expiration and delete the storage on next visit as the first thing you do.

SVK
  • 2,107
  • 2
  • 20
  • 41
0

No, this is not a good professional Practice. There is no sense to remove the user's credential from local storage. you should clear when the user is gonna log out. But at some points, it could be achieved to make a session log out automatically.

setTimeout(logout(){ alert("Hello"); }, 1800000);
logout(){
     localstorage.clear('user');
}

You can achieve by this.