Home Angular 13 Angular 13 Inactive User State Automatically Logout After 10 Seconds

Angular 13 Inactive User State Automatically Logout After 10 Seconds

by therichpost
5 comments
Angular 13 Inactive User State Automatically Logout After 10 Seconds

Hello friends, welcome back to my blog. Today in this blog post, I am going to tell you, Angular 13 Inactive User State Automatically Logout After 10 Seconds.

Working Demo

Angular 13 came and if you are new then you must check below two links:

  1. Angular 13 Demos
  2. AngularMaterial

Angular 13 Inactive User State Automatically Logout After 10 Seconds
Angular 13 Inactive User State Automatically Logout After 10 Seconds

Friends here is the code snippet and please use this carefully to avoid mistakes:

1. Firstly friends we need fresh angular 13 setup and for this we need to run below commands but if you already have angular 13 setup then you can avoid below commands. Secondly we should also have latest node version installed on our system:

npm install -g @angular/cli

ng new angulardemo

cd angulardemo

ng serve --o

//Here is the url, you need to run into your browser and see working angular test project
http://localhost:4200/

2. Now friends we need to run below command into our project terminal to get angular check user state modules:

npm install bn-ng-idle

ng serve --o

3. Now friends  we need to add below code inside project/src/app/app.module.ts file:

...
import { BnNgIdleService } from 'bn-ng-idle'; // import bn-ng-idle service


@NgModule({
  ...
  providers: [BnNgIdleService], // add it to the providers of your module
  ...

4. Now friends  we need to add below code inside project/src/app/app.component.ts file:

import { Component } from '@angular/core';
import { BnNgIdleService } from 'bn-ng-idle'; // import it to your component

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  constructor(private bnIdle: BnNgIdleService) {

  }

  // initiate it in your component OnInit
  ngOnInit(): void {
    //60 = 1 minute
    this.bnIdle.startWatching(60).subscribe((res) => {
      if (res) {
        console.log('session expired');
        // this.router.navigateByUrl('logout');
      }
    });
  }
}

Now we are done friends. If you have any kind of query or suggestion or requirement then feel free to comment below.

Note: Friends, In this post, I just tell the basic setup and things, you can change the code according to your requirements.
I will appreciate that if you will tell your views for this post. Nothing matters if your views will be good or bad because with your views, I will make my next posts more good and helpful.

Jassa

Thanks

You may also like

5 comments

Swamya June 29, 2022 - 2:27 am

How does pop up come?

Reply
therichpost June 29, 2022 - 4:20 am

you can popup alert for below line:
console.log(‘session expired’);

Reply
Swamya August 8, 2022 - 8:01 am

I want to show only the user when it login only

Reply
Sri ramya December 22, 2022 - 12:06 pm

It working superb.Thank you so much

Reply
Chetan December 1, 2023 - 10:53 am

Hi Can you paste popup code as well, I am not able to understand the code.

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.