Home > OS >  Get User Idle time(minutes) in Angular-9
Get User Idle time(minutes) in Angular-9

Time:10-31

I am trying to implement session time out logic in my application. Application session is 60mins and I need to show user a message to extend if active session exceeds 60 minutes. Based on the session expiry minutes and user idle minutes I need to calculate the session expiry minutes.

I could see there are many implementation to get the user idle time out. However, I could not see any implementation to get the user idle minutes.

I am following the below implementation https://www.chunho-ling.com/2021/07/05/angular-idle-timeout-checking/

Can anyone shed some light on getting the idle minutes.

CodePudding user response:

I skimmed, but in general it looks like the implementation you linked should more or less do the trick.

The basics of it are:

  1. A singleton service for the app that does three things:

a) Listen to (all/specific) user events such as mouse clicks, etc. at the document level

b) Keep a timeout timer running that gets reset whenever the above events are triggered.

c) Notify something when that timeout goes off

  1. Listen to whatever notification is sent by the service, and action it (i.e. logout) - or have the service itself do it, I'm not the architecture police.

Your rather more specific question just involves saving a timestamp when those triggers fire and having that available to compare against.

private onUserEvent(): void {
  this.resetTimer();
  this.lastUserAction = new Date();
}

Then whatever you so desire to get your minutes since last action comparing the last action date vs current date (this should do it)

public get idleMinutes(): number {
  return this.getMinutesBetween(this.lastUserAction, new Date());
}
  • Related