角度用户闲置服务问题

时间:2019-03-09 02:50:43

标签: javascript angular typescript angular6

我正在使用user-idle-service(https://www.npmjs.com/package/angular-user-idle)获取角度信息。我将其用于我的angular 6前端应用程序,但是遇到一些问题,这些问题导致我的用户在活动时过早地被踢出去。

我在用户登录到应用程序后立即启动该库。它在大多数情况下都有效,但是有时用户在浏览器中处于活动状态时会被过早踢出。我仅将其用于基于浏览器的用途。

在服务类中,我触发以下内容以及其下方的配置。我正在使用的库中有任何明显错误的东西吗? :

constructor(private http: Http,
            private router: Router,
            private cookieService: CookieService,
            private userIdle: UserIdleService) {
    this.userIdle.setCustomActivityEvents(merge(
        fromEvent(window, 'mousemove'),
        fromEvent(window, 'resize'),
        fromEvent(document, 'keydown'),
        fromEvent(document, 'touchstart'),
        fromEvent(document, 'touchend'),
        fromEvent(window, 'scroll')
    ));
}

login(credentials: Credentials): Observable<any> {
    return this.http.post<any>(this.loginUrl, JSON.stringify(credentials), {
        headers: new HttpHeaders({'Content-Type': 'application/json'})
    })
        .pipe(map(userDetails => {
            this.setUserDetailsAndExtendExpirationInLocalStorage(userDetails);
            this.startUserIdle();
            this.goToHome();
        }));
}

startUserIdle() {
    this.userIdle.startWatching();
    this.subscriptions.push((this.userIdle.onTimerStart().subscribe()));
    this.subscriptions.push((this.userIdle.onTimeout().subscribe(() => {
        this.logout();
    })));
    this.subscriptions.push(this.userIdle.ping$.subscribe(() => {
        this.refreshToken();
    }));
}

refreshToken() {
    return this.http.get<any>(this.refreshUrl, {
        headers: new HttpHeaders({'Content-Type': 'application/json'})
    }).subscribe(() => {
        // do stuff
    }, () => {
        this.logout();
    });

}

logout() {
    this.goToLogin();
    this.removeUserDetails();
    this.removeSessionToken();
    this.userIdle.resetTimer();
    this.userIdle.stopTimer();
    this.userIdle.stopWatching();
    this.subscriptions.forEach(subscription => subscription.unsubscribe());
    this.setIsLoggedIn(false);
}

同样在我的app.module.ts文件中,我将其配置如下:

UserIdleModule.forRoot({idle: 900, timeout: 1, ping: 840})

0 个答案:

没有答案