ngx-idle-service
TypeScript icon, indicating that this package has built-in type declarations

12.2.21 • Public • Published

NGX idle Service

Angular service to handle user's session is idle.

Installation

npm install ngx-idle-service --save

# or

yarn add ngx-idle-service

Usage

Add the idle service to your app.module.ts as a provider:

import { IdleService } from 'ngx-idle-service';

@NgModule({
  ...
  imports: [
    ...
    NgIdleModule.forRoot(),
    ...
  ],
  ...
})

export class AppModule {
}

Then, import and inject it into a constructor:

constructor(private idleService: IdleService)
{
    this.idleService.setInterrupts(DEFAULT_INTERRUPTSOURCES);
    this.idleService.setIdle(300);
    this.idleService.setTimeout(60);

    this.idleService.onInterrupt.subscribe((args: any) =>
      console.warn('[SESSION] onInterrupt', args)
    );

    this.idleService.onIdleStart.subscribe(() =>
      console.warn('[SESSION] onIdleStart')
    );

    this.idleService.onIdleEnd.subscribe(() =>
      console.warn('[SESSION] onIdleEnd')
    );

    this.idleService.onTimeoutWarning.subscribe((countdown: number) =>
      console.warn('[SESSION] onTimeoutWarning', countdown)
    );

    this.idleService.onTimeout.subscribe(() => {
      console.warn('[SESSION] onTimeout');
    });
  }

That's it!

Angular 14+

  1. Angular 14 introduced support for standalone components. If you are using just standalone components, you can import the service directly into the component
import { IdleService } from 'ngx-idle-service';
import { Component } from '@angular/core';

@Component({
  selector: 'my-component',
  template: `<h1>Hello World</h1>`,
  providers: [IdleService]
})
export class HelloComponent {
  constructor(private idleService: IdleService) {
    this.idleService.setInterrupts(DEFAULT_INTERRUPTSOURCES);
    this.idleService.setIdle(300);
    this.idleService.setTimeout(60);

    this.idleService.onInterrupt.subscribe((args: any) =>
      console.warn('[SESSION] onInterrupt', args)
    );

    this.idleService.onIdleStart.subscribe(() =>
      console.warn('[SESSION] onIdleStart')
    );

    this.idleService.onIdleEnd.subscribe(() =>
      console.warn('[SESSION] onIdleEnd')
    );

    this.idleService.onTimeoutWarning.subscribe((countdown: number) =>
      console.warn('[SESSION] onTimeoutWarning', countdown)
    );

    this.idleService.onTimeout.subscribe(() => {
      console.warn('[SESSION] onTimeout');
    });
  }
}
  1. You can also use inject() method in v14+ to inject the service into the component
import { idleService } from 'ngx-idle-service';
import { Component, inject } from '@angular/core';

@Component({
  selector: 'my-component',
  template: `<h1>Hello World</h1>`,
  providers: [IdleService]
})
export class HelloComponent {
  idleService = inject(IdleService);

  constructor() {
    this.idleService.setInterrupts(DEFAULT_INTERRUPTSOURCES);
    this.idleService.setIdle(300);
    this.idleService.setTimeout(60);

    this.idleService.onInterrupt.subscribe((args: any) =>
      console.warn('[SESSION] onInterrupt', args)
    );

    this.idleService.onIdleStart.subscribe(() =>
      console.warn('[SESSION] onIdleStart')
    );

    this.idleService.onIdleEnd.subscribe(() =>
      console.warn('[SESSION] onIdleEnd')
    );

    this.idleService.onTimeoutWarning.subscribe((countdown: number) =>
      console.warn('[SESSION] onTimeoutWarning', countdown)
    );

    this.idleService.onTimeout.subscribe(() => {
      console.warn('[SESSION] onTimeout');
    });
  }
}

Supported Versions

ViewEngine support has been removed on 13.x.x. For Angular versions 13.x.x or later use the latest version of the library. For versions <=12.x.x, use 12.0.3 version

Angular Version Supported Version
16.x.x 16.x.x
15.x.x 15.x.x
14.x.x 14.x.x
13.x.x 13.x.x
<=12.x.x (View Engine) 12.x.x

FAQ

General tips

Checking out the following resources usually solves most of the problems people seem to have with this service:

I am always getting a "token missing" or "no provider" error.

Package managers are a well known source of frustration. If you have "token missing" or "no provider" errors, a simple re-installation of your node modules might suffice:

rm -rf node_modules
yarn # or `npm install`

I have a problem with framework X or library Y. What can I do?

Please be aware that we cannot help you with problems that are out of scope. For example, we cannot debug a Symfony or Springboot application for you. In that case, you are better off asking the nice folks over at StackOverflow for help.

Do you support Angular Universal?

There is an issue for that. Check out this comment for more information about future support.

Opening issues

Please make sure to check out our FAQ before you open a new issue. Also, try to give us as much information as you can when you open an issue. Maybe you can even supply a test environment or test cases, if necessary?

Contributing

We are happy to accept pull requests or test cases for things that do not work. Feel free to submit one of those.

However, we will only accept pull requests that pass all tests and include some new ones (as long as it makes sense to add them, of course).

Author

This cookie service is brought to you by 7leads GmbH. We built it for one of our apps, because the other cookie packages we found were either not designed "the Angular way" or caused trouble during AOT compilation.

Contributors

Thanks to all contributors:

License

MIT

Package Sidebar

Install

npm i ngx-idle-service

Weekly Downloads

9

Version

12.2.21

License

MIT

Unpacked Size

411 kB

Total Files

46

Last publish

Collaborators

  • mlicciardi