Skip to content
This repository was archived by the owner on Mar 25, 2023. It is now read-only.

Commit 7fb7659

Browse files
authored
Merge pull request #240 from bwsw/228-inactivity-timeout-fix
(closes #228): Inactivity timeout fix
2 parents fe3d54c + 9cc5326 commit 7fb7659

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

src/app/app.component.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import { AuthService, ErrorService, LanguageService, LayoutService, Notification
1010
import { RouterUtilsService } from './shared/services/router-utils.service';
1111
import { StyleService } from './shared/services/style.service';
1212
import { ZoneService } from './shared/services/zone.service';
13+
import { StorageService } from './shared/services/storage.service';
14+
import { AsyncJobService } from './shared/services/async-job.service';
15+
import { CacheService } from './shared/services/cache.service';
1316

1417

1518
@Component({
@@ -35,6 +38,9 @@ export class AppComponent implements OnInit, AfterViewInit {
3538
private router: Router,
3639
private error: ErrorService,
3740
private languageService: LanguageService,
41+
private asyncJobService: AsyncJobService,
42+
private cacheService: CacheService,
43+
private storage: StorageService,
3844
private layoutService: LayoutService,
3945
private mdlDialogService: MdlDialogService,
4046
private notification: NotificationService,
@@ -58,13 +64,19 @@ export class AppComponent implements OnInit, AfterViewInit {
5864
this.loadSettings();
5965

6066
this.error.subscribe(e => this.handleError(e));
61-
this.auth.loggedIn.subscribe(loggedIn => {
62-
this.loggedIn = loggedIn;
67+
this.auth.loggedIn.subscribe(isLoggedIn => {
68+
this.loggedIn = isLoggedIn;
6369
this.updateAccount(this.loggedIn);
64-
if (loggedIn) {
70+
if (isLoggedIn) {
71+
this.auth.startInactivityCounter();
6572
this.loadSettings();
6673
this.zoneService.areAllZonesBasic().subscribe(basic => this.disableSecurityGroups = basic);
74+
} else {
75+
this.auth.clearInactivityTimer();
6776
}
77+
this.asyncJobService.completeAllJobs();
78+
this.cacheService.invalidateAll();
79+
this.storage.resetInMemoryStorage();
6880
});
6981

7082
this.layoutService.drawerToggled.subscribe(() => {

src/app/shared/services/auth.service.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ describe('Auth service session', () => {
215215
const inactivityTimeout = 10;
216216
const logout = spyOn(router, 'navigate').and.callThrough();
217217
const refresh = spyOn(authService, 'sendRefreshRequest');
218-
218+
authService.startInactivityCounter();
219219
authService.setInactivityTimeout(inactivityTimeout).subscribe();
220220

221221
tick(getRefreshInterval() * (inactivityTimeout - 1) * 60 / refreshInterval);

src/app/shared/services/auth.service.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@ import { BaseModelStub } from '../models';
88
import { AsyncJobService } from './async-job.service';
99

1010
import { BaseBackendService } from './base-backend.service';
11-
import { CacheService } from './cache.service';
1211
import { ConfigService } from './config.service';
1312
import { RouterUtilsService } from './router-utils.service';
1413
import { StorageService } from './storage.service';
1514
import { UserService } from './user.service';
1615

17-
import debounce = require('lodash/debounce');
18-
19-
2016
const DEFAULT_SESSION_REFRESH_INTERVAL = 60;
2117

2218
@Injectable()
@@ -33,7 +29,6 @@ export class AuthService extends BaseBackendService<BaseModelStub> {
3329

3430
constructor(
3531
protected asyncJobService: AsyncJobService,
36-
protected cacheService: CacheService,
3732
protected configService: ConfigService,
3833
protected storage: StorageService,
3934
protected router: Router,
@@ -43,33 +38,26 @@ export class AuthService extends BaseBackendService<BaseModelStub> {
4338
) {
4439
super();
4540
this.loggedIn = new BehaviorSubject<boolean>(!!this.userId);
41+
}
4642

47-
debounce(this.refreshSession, 1000, { leading: true });
48-
debounce(this.resetTimer, 1000, { leading: true });
49-
43+
public startInactivityCounter() {
5044
Observable.forkJoin(
5145
this.getInactivityTimeout(),
5246
this.getSessionRefreshInterval()
5347
)
5448
.subscribe(([inactivityTimeout, sessionRefreshInterval]) => {
5549
this.inactivityTimeout = inactivityTimeout;
5650
this.sessionRefreshInterval = sessionRefreshInterval;
57-
this.resetTimer();
51+
this.resetInactivityTimer();
5852
this.addEventListeners();
5953
});
60-
61-
this.loggedIn.subscribe(() => {
62-
this.asyncJobService.completeAllJobs();
63-
this.cacheService.invalidateAll();
64-
this.storage.resetInMemoryStorage();
65-
});
6654
}
6755

6856
public setInactivityTimeout(value: number): Observable<void> {
6957
return this.userService.writeTag('sessionTimeout', value.toString())
7058
.map(() => {
7159
this.inactivityTimeout = value;
72-
this.resetTimer();
60+
this.resetInactivityTimer();
7361
});
7462
}
7563

@@ -174,7 +162,7 @@ export class AuthService extends BaseBackendService<BaseModelStub> {
174162

175163
private refreshSession(): void {
176164
if (++this.numberOfRefreshes * this.sessionRefreshInterval >= this.inactivityTimeout * 60) {
177-
this.clearTimer();
165+
this.clearInactivityTimer();
178166
this.zone.run(() => this.router.navigate(['/logout'], this.routerUtilsService.getRedirectionQueryParams()));
179167
} else {
180168
this.sendRefreshRequest();
@@ -185,23 +173,23 @@ export class AuthService extends BaseBackendService<BaseModelStub> {
185173
const events = 'mousemove keydown DOMMouseScroll mousewheel mousedown touchstart touchmove scroll'.split(' ');
186174
const observables = events.map(event => Observable.fromEvent(document, event));
187175
this.zone.runOutsideAngular(() => {
188-
Observable.merge(...observables).subscribe(() => this.resetTimer());
176+
Observable.merge(...observables).subscribe(() => this.resetInactivityTimer());
189177
});
190178
}
191179

192-
private resetTimer(): void {
193-
this.clearTimer();
180+
private resetInactivityTimer(): void {
181+
this.clearInactivityTimer();
194182
this.numberOfRefreshes = 0;
195183
if (this.inactivityTimeout) {
196-
this.setTimer();
184+
this.setInactivityTimer();
197185
}
198186
}
199187

200-
private clearTimer(): void {
188+
public clearInactivityTimer(): void {
201189
clearInterval(this.refreshTimer);
202190
}
203191

204-
private setTimer(): void {
192+
private setInactivityTimer(): void {
205193
if (this.sessionRefreshInterval && this.inactivityTimeout) {
206194
this.refreshTimer = setInterval(this.refreshSession.bind(this), this.sessionRefreshInterval * 1000);
207195
}

src/app/shared/services/user.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class UserService extends BaseBackendService<User> {
4747
}
4848

4949
public removeTag(key: string): Observable<void> {
50-
let userId = this.storageService.read('userId');
50+
const userId = this.storageService.read('userId');
5151
if (!userId) {
5252
return;
5353
}

0 commit comments

Comments
 (0)