0

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.

biju m
  • 79
  • 1
  • 12

1 Answers1

1

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());
}
Krenom
  • 1,894
  • 1
  • 13
  • 20
  • Thanks a lot for the directions. It actually worked for me. Just one more question. How can i asynchronously check every 2 minutes for the session time out. Is there any way i can do the same. – biju m Oct 31 '21 at 13:43
  • In what way do you mean that? You want something checking every 2m separately rather than having the service notify you? Intervals and timeouts are async, so if you have some idle warning service, it would be as simple as an interval `setInterval(() => {console.log('User idle for (mins): ', this.idleService.idleMinutes());}, 1000 * 60 * 2)` if you have that idleMinutes method I threw in. – Krenom Nov 01 '21 at 07:06