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

Commit 9ff21d1

Browse files
committed
fix(settings): Inactivity timeout fixed for user that does not refresh the page after logging in.
1 parent 3044e1f commit 9ff21d1

File tree

4 files changed

+29
-23
lines changed

4 files changed

+29
-23
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: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ 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';
@@ -33,7 +32,6 @@ export class AuthService extends BaseBackendService<BaseModelStub> {
3332

3433
constructor(
3534
protected asyncJobService: AsyncJobService,
36-
protected cacheService: CacheService,
3735
protected configService: ConfigService,
3836
protected storage: StorageService,
3937
protected router: Router,
@@ -45,31 +43,27 @@ export class AuthService extends BaseBackendService<BaseModelStub> {
4543
this.loggedIn = new BehaviorSubject<boolean>(!!this.userId);
4644

4745
debounce(this.refreshSession, 1000, { leading: true });
48-
debounce(this.resetTimer, 1000, { leading: true });
46+
debounce(this.resetInactivityTimer, 1000, { leading: true });
47+
}
4948

49+
public startInactivityCounter() {
5050
Observable.forkJoin(
5151
this.getInactivityTimeout(),
5252
this.getSessionRefreshInterval()
5353
)
5454
.subscribe(([inactivityTimeout, sessionRefreshInterval]) => {
5555
this.inactivityTimeout = inactivityTimeout;
5656
this.sessionRefreshInterval = sessionRefreshInterval;
57-
this.resetTimer();
57+
this.resetInactivityTimer();
5858
this.addEventListeners();
5959
});
60-
61-
this.loggedIn.subscribe(() => {
62-
this.asyncJobService.completeAllJobs();
63-
this.cacheService.invalidateAll();
64-
this.storage.resetInMemoryStorage();
65-
});
6660
}
6761

6862
public setInactivityTimeout(value: number): Observable<void> {
6963
return this.userService.writeTag('sessionTimeout', value.toString())
7064
.map(() => {
7165
this.inactivityTimeout = value;
72-
this.resetTimer();
66+
this.resetInactivityTimer();
7367
});
7468
}
7569

@@ -174,7 +168,7 @@ export class AuthService extends BaseBackendService<BaseModelStub> {
174168

175169
private refreshSession(): void {
176170
if (++this.numberOfRefreshes * this.sessionRefreshInterval >= this.inactivityTimeout * 60) {
177-
this.clearTimer();
171+
this.clearInactivityTimer();
178172
this.zone.run(() => this.router.navigate(['/logout'], this.routerUtilsService.getRedirectionQueryParams()));
179173
} else {
180174
this.sendRefreshRequest();
@@ -185,23 +179,23 @@ export class AuthService extends BaseBackendService<BaseModelStub> {
185179
const events = 'mousemove keydown DOMMouseScroll mousewheel mousedown touchstart touchmove scroll'.split(' ');
186180
const observables = events.map(event => Observable.fromEvent(document, event));
187181
this.zone.runOutsideAngular(() => {
188-
Observable.merge(...observables).subscribe(() => this.resetTimer());
182+
Observable.merge(...observables).subscribe(() => this.resetInactivityTimer());
189183
});
190184
}
191185

192-
private resetTimer(): void {
193-
this.clearTimer();
186+
private resetInactivityTimer(): void {
187+
this.clearInactivityTimer();
194188
this.numberOfRefreshes = 0;
195189
if (this.inactivityTimeout) {
196-
this.setTimer();
190+
this.setInactivityTimer();
197191
}
198192
}
199193

200-
private clearTimer(): void {
194+
public clearInactivityTimer(): void {
201195
clearInterval(this.refreshTimer);
202196
}
203197

204-
private setTimer(): void {
198+
private setInactivityTimer(): void {
205199
if (this.sessionRefreshInterval && this.inactivityTimeout) {
206200
this.refreshTimer = setInterval(this.refreshSession.bind(this), this.sessionRefreshInterval * 1000);
207201
}

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)