In my work with Angular, I encountered the need to log out users who are inactive. While I found a solution that worked for a particular page, it was not scalable as I have multiple pages where this feature is needed. Is there a way to globally monitor user inactivity in Angular?
I attempted to implement the following code, which successfully logs out inactive users but only on a specific page:
constructor() {
this.setTimeout();
this.userInactive.subscribe(() => console.log('user has been inactive for 3s'));
}
userActivity:any;
userInactive: Subject<any> = new Subject();
setTimeout() {
this.userActivity = setTimeout(() => this.userInactive.next(undefined), 3000);
}
@HostListener('window:mousemove') refreshUserState() {
clearTimeout(this.userActivity);
this.setTimeout();
}