在Angular 2中将会话与ng-idle超时集成

时间:2019-04-04 18:47:05

标签: angular

我正在开发一个有角度的客户端应用程序,该应用程序通过后端REST服务对用户进行身份验证。我需要添加一项功能,该功能可以在给定的时间内管理已登录用户的会话,同时还要监视正在使用该应用程序的用户的空闲状态。目前,我只捕获了超时的用户会话,并重定向到登录页面。 我已完成的实现的代码段如下:

app.components.ts:

c

session.service.ts

import { Component } from '@angular/core';
import {Idle, DEFAULT_INTERRUPTSOURCES} from '@ng-idle/core';
import {Keepalive} from '@ng-idle/keepalive';
import {Router, ActivatedRoute} from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
   })
  export class AppComponent {
title = 'app';
    constructor(private router: Router) {
setInterval((() => this.endSession()), 300000);
    }
endSession() {
console.log('trying to log out....');
localStorage.removeItem('session');
this.router.navigate(['/auth']);
}
}

auth.service.ts

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';

  @Injectable({
    providedIn: 'root'
  })

 export class SessionTimeoutService {
 constructor(private router: Router) {}
 isValid = true;
 sessionIsValid() {
 return this.isValid;
   }
}

0 个答案:

没有答案