moribvndvs / ng2-idle

Responding to idle users in Angular (not AngularJS) applications.
https://moribvndvs.github.io/ng2-idle
Apache License 2.0
315 stars 128 forks source link

Custom Expiry not functioning as expected #27

Closed markmaynard closed 6 years ago

markmaynard commented 7 years ago

I'm submitting a ... (check one with "x")

[X] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/HackedByChinese/ng2-idle/blob/master/CONTRIBUTING.md#getting-help

Current behavior Created a cookie based IdleExpiry that stores lastValue as a cookie. Activity in one tab does not prevent timeout in another tab. I was able to get something close to what I wanted by modifying the toggleState method in Idle and inserting the following at the top of the function: if (!this.expiry.isExpired()) { return; }

This checks the cookie and prevents expiration.

Expected behavior Activity in one tab prevents timeout in all tabs.

Minimal reproduction of the problem with instructions Replace SimpleExpiry with the following class `@Injectable() export class CookieExpiry extends IdleExpiry { private lastValue: Date = undefined;

constructor( private cookieService: CookieService ) { super(); }

/*

What is the motivation / use case for changing the behavior? Allow activity on one tab to keep other tabs from expiring. Since all tabs are using the same session cookie the "out-of-focus" tab is expiring session for all tabs.

Please tell us about your environment: Ubuntu 16.04 NPM Webpack-dev server

markmaynard commented 7 years ago

Sorry to bug you but I see responses on other issues, is there something my issue is missing?

moribvndvs commented 7 years ago

@markmaynard Sorry, I've been busy at work and haven't had time to reproduce the issue. If you think you've got a handle on what needs to change to get it working, feel free to submit a pull request. I might be able to take a look this weekend.

markmaynard commented 7 years ago

I thought I had a fix but I was incorrect. The problem seems to be that activity sets a new timeout time in my cookie, but none of the other tabs actually check the cookie to see that this timeout has been extended. What I think need to happen is that when a timeout occurs on a tab, it first checks the cookie(or other shared storage) to see if is time is greater than the time in the cookie. If not it sets a new timer for the difference between stored_time and now.

moribvndvs commented 7 years ago

Ah, right. So, evergreen browsers (except Edge) support an onChanged event for cookies. That would be ideal (and most analogous to how I handle it via storage in the original ng-idle). However, we'd still need a way to handle browsers that don't support this. It may make sense to create a custom interrupt source that triggers a function at intervals (say, 1 second) to see if the cookie value has changed. If it has, trigger the interrupt. This, in concert with your CookieExpiry should handle activity from other tabs.

roupify commented 7 years ago

If localStorage is an option for you, a new release of ng2-idle has been made last week on tag v2.0.0-beta.6.

markmaynard commented 7 years ago

I have a cookie storage version that works fine in that it is a centralized store for the timeout value. The problem I ran into is that the idle timeout is set by all the open tabs but the interval in the watch function does not check the the updated values. The change I PRed should fix that.

Splaktar commented 6 years ago

https://github.com/HackedByChinese/ng2-idle/pull/37 was merged. Should this issue be closed now?

markmaynard commented 6 years ago

Yes, sorry, closing now.