This is a fork of the official ng2-idle repo, due to the maintainer not being active and updating it to support rxjs6 and angular6.A module for responding to idle users in Angular 4+ applications. This is a rewrite of the ng-idle module; however if you are using Angular 1, you must use that module.
LicenseAuthored by Mike Grabski @HackedByChinese firstname.lastname@example.org
See LICENSE for licensing details.
DemoVisit https://hackedbychinese.github.io/ng2-idle to view a simple example with quick start instructions.
@ng-idleis shipped via npm. You can install the package using the following command:
npm install --save @ng2-idle/core
Integrating and configuring the package into your application requires a few more steps. Please visit @ng-idle-example for source and instructions on how to get going.
Design ConsiderationsThe primary application of this module is to detect when users are idle. It can also be used to warn users of an impending timeout, and then time them out. The core of this module is the
Idleservice which does its best - based on your configuration - to detect when a user is active or idle and pass that information on to your application so it can respond appropriately.
ModularizationThe core functionality can be found in the
@ng-idle/corepackage via npm.
Additional modules to extend functionality:
Extensible Keepalive IntegrationIn a common use case where it is used for session management, you may need to signal to the server periodically that the user is still logged in and active. If you need that functionality,
@ng-idlecan optionally integrate with
@ng-idle/keepaliveto ping while the user is active, and stop once they go idle or time out. When the user resumes activity or the idle state is reset, it will ping immediately and then resume pinging. Please note that keepalive integration is optional, and you must install and configure
@ng-idle/keepaliveseparately to get this functionality. You can implement your own by extending
KeepaliveSvcand configuring it as a provider in your application for the
Extensible InterruptsAn interrupt is any source of input (typically from the user, but could be things like other tabs or an event) that can be used to signal to
Idlethat the idle watch should be interrupted or reset. Unlike
ng-idle, these sources are not hardcoded; you can extend
InterruptSourceor any of the built-in sources to suit your purposes. This feature is also useful to handle input noise that may plague your particular use case. It can also be used to target specific elements on a page rather than the whole document or window. The following sources come built into this package:
InterruptSource(abstract): A base type you can implement to make your own source.
EventTargetInterruptSource: Any object that implements
EventTarget, such as an
Window. Takes in the object that is the source and a space delimited string containing the events that cause an interrupt.
DocumentInterruptSource: Looks for events (in a space delimited string) that bubble up to the
WindowInterruptSource: Looks for events (in a space delimited string) that bubble up to the
StorageInterruptSource: Looks only for the
Windowobject. Obligatory for
NOTE: You must configure source(s) yourself when you initialize the application. By default, no interrupts are configured. You can use a configuration analogous to the
ng-idledefault by importing
DEFAULT_INTERRUPTSOURCESand passing that reference to
Extensible ExpiryAnother feature ported from
ng-idleis the ability to store an expiry value in some store where multiple tabs or windows running the same application can write to. Commonly, this store is the
localStorage, but could be cookies or whatever you want. The purpose of this expiry and the expiry store is twofold: First, to prevent a window from not timing out if it sleeps or pauses longer than the configured timeout period. Second, it can be used so that activity in one tab or window prevents other tabs or windows in the same application from timing out.
By default, a
LocalStorageExpirytype is provided, which will just keep track of the expiry in the localStorage. It will fulfill all purposes mentioned above. If you don't want to support multiple tabs or windows, you can use
SimpleExpiry. In other words,
SimpleExpirydoes not coordinate last activity between tabs or windows. If you want to store the expiry value in another store, like cookies, you'll need to use or create an implementation that supports that. You can create your own by extending
SimpleExpiryand configuring it as a provider for the
Multiple Idle Instance SupportThe dependency injector in Angular 4+ supports a hierarchical injection strategy. This allows you to create an instance of
Idleat whatever scope you need, and there can be more than one instance. This allows you two have two separate watches, for example, on two different elements on the page.
If you use the default expiry (
LocalStorageExpiry), you will need to define a name for each idle with
Idle.setIdleName('yourIdleName'), otherwise the same key will be used in the localStorage and this feature will not work as expected.
Example Use CaseFor example, consider an email application. For increased security, the application may wish to determine when the user is inactive and log them out, giving them a chance to extend their session if they are still at the computer and just got distracted. Additionally, for even better security the server may issue the user's session a security token that expires after 5 minutes of inactivity. The user may take much more time than that to type out their email and send it. It would be frustrating to find you are logged out when you were actively using the software!
@ng-idle/corecan detect that the user is clicking, typing, touching, scrolling, etc. and know that the user is still active. It can work with
@ng-idle/keepaliveto ping the server every few minutes to keep them logged in. In this case, as long as the user is doing something, they stay logged in. If they step away from the computer, we can present a warning dialog, and then after a countdown, log them out.
DevelopingNote This project was developed using NodeJS 5.5 and NPM 3.3.12. You may experience problems using older versions. Try NVM or similar to manage different versions of Node concurrently.
Once you have cloned the repository, install all packages. If you are using
If you are using Yarn:
You can now build and run tests.
You can also continuously run tests as you make changes.
npm run watch:test
If you wish to prepare a branch for a pull request, run this command and fix any errors:
npm run build