-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathidle.service.ts
More file actions
183 lines (162 loc) · 5.49 KB
/
Copy pathidle.service.ts
File metadata and controls
183 lines (162 loc) · 5.49 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { Injectable, inject, NgZone, OnDestroy, effect } from '@angular/core';
import { AuthService } from './auth.service';
import { Router } from '@angular/router';
import { fromEvent, merge, Subscription, throttleTime } from 'rxjs';
import { MatDialog, MatDialogRef } from '@angular/material/dialog';
import { InactivityDialogComponent } from '../../layout/inactivity-dialog.component';
/**
* Service responsible for monitoring user activity and managing session timeouts.
*
* When a user is authenticated, it listens for global activity events (mouse, keyboard, etc.)
* and maintains an idle timer. If the user is inactive for a set period, it displays a
* warning dialog and eventually logs the user out.
*/
@Injectable({
providedIn: 'root',
})
export class IdleService implements OnDestroy {
private readonly authService = inject(AuthService);
private readonly router = inject(Router);
private readonly ngZone = inject(NgZone);
private readonly dialog = inject(MatDialog);
private idleSubscription?: Subscription;
private timeoutId: ReturnType<typeof setTimeout> | null = null;
private dialogRef: MatDialogRef<InactivityDialogComponent> | null = null;
// Configuration
/** Total time of inactivity allowed before forced logout (15 minutes) */
private readonly IDLE_TIMEOUT = 15 * 60 * 1000;
/** Time before final logout when the warning dialog should be shown (2 minutes) */
private readonly WARNING_TIME = 2 * 60 * 1000;
constructor() {
// Automatically start/stop monitoring based on authentication state
effect(() => {
if (this.authService.isAuthenticated()) {
this.startMonitoring();
} else {
this.stopMonitoring();
}
});
}
/**
* Initializes activity monitoring using global window events.
*
* Events are throttled and monitoring runs outside the Angular zone
* to avoid unnecessary change detection cycles.
*/
private startMonitoring(): void {
this.stopMonitoring();
this.ngZone.runOutsideAngular(() => {
const activityEvents$ = merge(
fromEvent(window, 'mousemove'),
fromEvent(window, 'keydown'),
fromEvent(window, 'click'),
fromEvent(window, 'scroll'),
fromEvent(window, 'touchstart'),
).pipe(throttleTime(5000));
this.idleSubscription = activityEvents$.subscribe(() => {
// Only reset the timer if the warning dialog is not currently open
if (!this.dialogRef) {
this.resetTimer();
}
});
});
this.resetTimer();
}
/**
* Stops monitoring and performs cleanup of subscriptions and timers.
*/
private stopMonitoring(): void {
this.clearTimers();
this.idleSubscription?.unsubscribe();
this.closeDialog();
}
/**
* Clears the current inactivity timeout timer.
*/
private clearTimers(): void {
if (this.timeoutId) {
clearTimeout(this.timeoutId);
this.timeoutId = null;
}
}
/**
* Resets the inactivity timer to wait until the next warning threshold.
*/
private resetTimer(): void {
this.clearTimers();
const timeToWarning = this.IDLE_TIMEOUT - this.WARNING_TIME;
this.timeoutId = setTimeout(() => {
this.showInactivityWarning();
}, timeToWarning);
}
/**
* Opens the inactivity warning dialog and sets a final logout timer.
*/
private showInactivityWarning(): void {
this.ngZone.run(() => {
if (this.dialogRef) return;
this.dialogRef = this.dialog.open(InactivityDialogComponent, {
disableClose: true,
width: '400px',
});
this.dialogRef.afterClosed().subscribe((shouldExtend: boolean) => {
this.dialogRef = null;
if (shouldExtend) {
this.resetTimer();
} else {
this.logoutDueToInactivity();
}
});
// Also set a hard logout timeout if the user fails to respond to the dialog
this.timeoutId = setTimeout(() => {
this.logoutDueToInactivity();
}, this.WARNING_TIME);
});
}
/**
* Forces a user logout and redirects to the login page due to inactivity.
*/
private logoutDueToInactivity(): void {
this.ngZone.run(() => {
if (this.authService.isAuthenticated()) {
console.warn('Session expired due to inactivity.');
this.closeDialog();
this.authService.logout();
this.router.navigate(['/login'], { queryParams: { reason: 'inactivity' } });
}
});
}
/**
* Safely closes the warning dialog if it is open.
*/
private closeDialog(): void {
if (this.dialogRef) {
this.dialogRef.close();
this.dialogRef = null;
}
}
/**
* Angular lifecycle hook to ensure cleanup when the service is destroyed.
*/
ngOnDestroy(): void {
this.stopMonitoring();
}
}