/
session-timeout-warning.effects.ts
84 lines (80 loc) · 3.08 KB
/
session-timeout-warning.effects.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { Inject, Injectable, Optional } from "@angular/core";
import { Observable, of } from "rxjs";
import { exhaustMap, map, takeUntil } from "rxjs/operators";
import {
STARK_SESSION_SERVICE,
StarkSessionActionTypes,
StarkSessionService,
StarkSessionTimeoutCountdownFinish,
StarkSessionTimeoutCountdownStart
} from "@nationalbankbelgium/stark-core";
import { StarkSessionTimeoutWarningDialogComponent } from "../components/session-timeout-warning-dialog/session-timeout-warning-dialog.component";
import { MatDialog } from "@angular/material/dialog";
import { Actions, Effect, EffectNotification, ofType, OnRunEffects } from "@ngrx/effects";
import { STARK_SESSION_UI_CONFIG, StarkSessionUiConfig } from "../entities";
@Injectable()
export class StarkSessionTimeoutWarningDialogEffects implements OnRunEffects {
/**
* Show a warning dialog when the Timeout countdown stars (the user activity tracking is paused until the user closes the dialog)
*/
public constructor(
public actions$: Actions,
@Inject(STARK_SESSION_SERVICE) public sessionService: StarkSessionService,
@Optional()
@Inject(STARK_SESSION_UI_CONFIG)
public starkSessionUiConfig: StarkSessionUiConfig,
@Inject(MatDialog) public dialogService: MatDialog
) {}
/**
* This method is used to display a warning session timeout dialog.
* When the dialog is closed, if the "keep-logged" string is received as a result, it means that the user should keep logged
* and we resume the user activity tracking.
*/
@Effect({ dispatch: false })
public starkSessionTimeoutWarning$(): Observable<void> {
return this.actions$.pipe(
ofType<StarkSessionTimeoutCountdownStart>(StarkSessionActionTypes.SESSION_TIMEOUT_COUNTDOWN_START),
map((action: StarkSessionTimeoutCountdownStart) => {
this.sessionService.pauseUserActivityTracking();
this.dialogService
.open<StarkSessionTimeoutWarningDialogComponent>(StarkSessionTimeoutWarningDialogComponent, { data: action.countdown })
.afterClosed()
.subscribe((result: string) => {
if (result && result === "keep-logged") {
this.sessionService.resumeUserActivityTracking();
}
});
})
);
}
/**
* This method is used to close the dialog if the session timeout countdown has finished
*/
@Effect({ dispatch: false })
public starkSessionTimeoutWarningClose$(): Observable<void> {
return this.actions$.pipe(
ofType<StarkSessionTimeoutCountdownFinish>(StarkSessionActionTypes.SESSION_TIMEOUT_COUNTDOWN_FINISH),
map(() => {
this.dialogService.closeAll();
})
);
}
/**
* This method will be triggered before the session is open to determine if we should use the session timeout warning effect or not.
*/
public ngrxOnRunEffects(resolvedEffects$: Observable<EffectNotification>): Observable<EffectNotification> {
if (this.starkSessionUiConfig && this.starkSessionUiConfig.timeoutWarningDialogDisabled === true) {
return this.actions$.pipe(
exhaustMap(() => {
return resolvedEffects$.pipe(takeUntil(of("stop")));
})
);
} else {
return this.actions$.pipe(
exhaustMap(() => {
return resolvedEffects$;
})
);
}
}
}