я писал такой сервис
const MINUTES_UNTIL_AUTO_LOGOUT = 5; // in Minutes
const CHECK_INTERVALL = 1000; // in ms
const STORE_KEY = 'lastAction';
export class AutoLogoutService {
constructor(
private _auth: AuthenticationService,
private _ngZone: NgZone,
@Inject(PLATFORM_ID) private platformId: Object
) {
this.check();
this.initListener();
this.initInterval();
}
get lastAction() {
if (isPlatformBrowser(this.platformId)) {
return parseInt(localStorage.getItem(STORE_KEY), 10);
}
}
set lastAction(value) {
if (isPlatformBrowser(this.platformId)) {
localStorage.setItem(STORE_KEY, value.toString());
}
}
initListener() {
this._ngZone.runOutsideAngular(() => {
document.body.addEventListener('click', () => this.reset());
});
}
initInterval() {
this._ngZone.runOutsideAngular(() => {
setInterval(() => {
this.check();
}, CHECK_INTERVALL);
});
}
reset() {
this.lastAction =
Date.now();
}
check() {
const now =
Date.now();
const timeLeft = this.lastAction + MINUTES_UNTIL_AUTO_LOGOUT * 60 * 1000;
const diff = timeLeft - now;
const isTimeout = diff < 0;
this._ngZone.run(() => {
if (isTimeout) this._auth.logout().subscribe();
});
}
}
может найдете для себя что то полезное