ngx-auto-logout 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/README.md +247 -0
  2. package/dist/README.md +247 -0
  3. package/dist/bundles/ngx-auto-logout.umd.js +864 -0
  4. package/dist/bundles/ngx-auto-logout.umd.js.map +1 -0
  5. package/dist/bundles/ngx-auto-logout.umd.min.js +16 -0
  6. package/dist/bundles/ngx-auto-logout.umd.min.js.map +1 -0
  7. package/dist/esm2015/lib/auto-logout.component.js +351 -0
  8. package/dist/esm2015/lib/auto-logout.module.js +26 -0
  9. package/dist/esm2015/lib/auto-logout.service.js +384 -0
  10. package/dist/esm2015/lib/models/auto-logout-config.interface.js +10 -0
  11. package/dist/esm2015/lib/models/auto-logout-config.token.js +7 -0
  12. package/dist/esm2015/lib/provide-auto-logout.js +11 -0
  13. package/dist/esm2015/ngx-auto-logout.js +5 -0
  14. package/dist/esm2015/public-api.js +10 -0
  15. package/dist/esm5/lib/auto-logout.component.js +196 -0
  16. package/dist/esm5/lib/auto-logout.module.js +30 -0
  17. package/dist/esm5/lib/auto-logout.service.js +391 -0
  18. package/dist/esm5/lib/models/auto-logout-config.interface.js +10 -0
  19. package/dist/esm5/lib/models/auto-logout-config.token.js +7 -0
  20. package/dist/esm5/lib/provide-auto-logout.js +11 -0
  21. package/dist/esm5/ngx-auto-logout.js +5 -0
  22. package/dist/esm5/public-api.js +10 -0
  23. package/dist/fesm2015/ngx-auto-logout.js +787 -0
  24. package/dist/fesm2015/ngx-auto-logout.js.map +1 -0
  25. package/dist/fesm5/ngx-auto-logout.js +643 -0
  26. package/dist/fesm5/ngx-auto-logout.js.map +1 -0
  27. package/dist/lib/auto-logout.component.d.ts +81 -0
  28. package/dist/lib/auto-logout.module.d.ts +8 -0
  29. package/dist/lib/auto-logout.service.d.ts +108 -0
  30. package/dist/lib/models/auto-logout-config.interface.d.ts +46 -0
  31. package/dist/lib/models/auto-logout-config.token.d.ts +7 -0
  32. package/dist/lib/provide-auto-logout.d.ts +6 -0
  33. package/dist/ngx-auto-logout.d.ts +4 -0
  34. package/dist/ngx-auto-logout.metadata.json +1 -0
  35. package/dist/package.json +41 -0
  36. package/dist/public-api.d.ts +6 -0
  37. package/package.json +53 -0
package/README.md ADDED
@@ -0,0 +1,247 @@
1
+ # ngx-auto-logout - Angular Library
2
+
3
+ > Angular 自动登出组件库 - 支持 Angular 9.x+,零第三方依赖(除RxJS)
4
+
5
+ ## ✨ 特性
6
+
7
+ - 🔄 **双模式支持**:固定时间模式 & 空闲超时模式
8
+ - ⏱️ **可配置时长**:超时时间、警告时间均可配置
9
+ - 🔔 **智能提醒**:倒计时警告 + 自定义回调
10
+ - ⏸️ **暂停/恢复**:支持临时暂停自动登出
11
+ - ➕ **延长会话**:用户可主动延长会话时间
12
+ - 💾 **持久化**:localStorage 持久化,刷新页面保持状态
13
+ - 🎯 **零依赖**:仅依赖 RxJS,无 Toastr 等UI库依赖
14
+ - 📦 **兼容性强**:支持 Angular 9.x - 20.x
15
+
16
+ ## 📦 安装
17
+
18
+ ```bash
19
+ npm install ngx-auto-logout
20
+ ```
21
+
22
+ ## 🚀 快速开始
23
+
24
+ ### 1. 导入模块
25
+
26
+ ```typescript
27
+ // app.module.ts (Angular 9-14)
28
+ import { AutoLogoutModule } from 'ngx-auto-logout';
29
+
30
+ @NgModule({
31
+ imports: [
32
+ AutoLogoutModule.forRoot({
33
+ mode: 'fixed',
34
+ timeout: 300,
35
+ warningTime: 30,
36
+ toastTime: 10,
37
+ onWarning: (seconds) => console.log(`警告: 还剩${seconds}秒`),
38
+ onTimeout: () => this.authService.logout()
39
+ })
40
+ ]
41
+ })
42
+ export class AppModule {}
43
+ ```
44
+
45
+ ```typescript
46
+ // app.config.ts (Angular 15+)
47
+ import { provideAutoLogout } from 'ngx-auto-logout';
48
+
49
+ export const appConfig: ApplicationConfig = {
50
+ providers: [
51
+ provideAutoLogout({
52
+ mode: 'fixed',
53
+ timeout: 300,
54
+ warningTime: 30,
55
+ onWarning: (seconds) => console.log(`警告: 还剩${seconds}秒`),
56
+ onTimeout: () => authService.logout()
57
+ })
58
+ ]
59
+ };
60
+ ```
61
+
62
+ ### 2. 在组件中使用
63
+
64
+ ```typescript
65
+ import { Component } from '@angular/core';
66
+ import { AutoLogoutService } from 'ngx-auto-logout';
67
+
68
+ @Component({...})
69
+ export class AppComponent {
70
+ constructor(private autoLogout: AutoLogoutService) {
71
+ // 订阅倒计时
72
+ this.autoLogout.getCountdown().subscribe(seconds => {
73
+ console.log('剩余时间:', seconds);
74
+ });
75
+ }
76
+
77
+ // 延长会话
78
+ extendSession() {
79
+ this.autoLogout.extendSession(1800); // 延长30分钟
80
+ }
81
+
82
+ // 暂停/恢复
83
+ togglePause() {
84
+ if (this.autoLogout.isPaused()) {
85
+ this.autoLogout.resume();
86
+ } else {
87
+ this.autoLogout.pause();
88
+ }
89
+ }
90
+ }
91
+ ```
92
+
93
+ ## 📖 API 文档
94
+
95
+ ### AutoLogoutConfig
96
+
97
+ ```typescript
98
+ interface AutoLogoutConfig {
99
+ mode: 'fixed' | 'idle'; // 模式
100
+ timeout: number; // 超时时间(秒)
101
+ warningTime?: number; // 警告时间(秒),默认30
102
+ maxExtendTimes?: number; // 最大延长次数,-1=无限制
103
+ onWarning?: (seconds: number) => void; // 警告回调
104
+ onTimeout?: () => void; // 超时回调
105
+ }
106
+ ```
107
+
108
+ ### AutoLogoutService
109
+
110
+ ```typescript
111
+ // 启动监控
112
+ startMonitoring(config?: Partial<AutoLogoutConfig>): void
113
+
114
+ // 停止监控
115
+ stopMonitoring(): void
116
+
117
+ // 延长会话
118
+ extendSession(seconds?: number): boolean
119
+
120
+ // 暂停
121
+ pause(): void
122
+
123
+ // 恢复
124
+ resume(): void
125
+
126
+ // 获取倒计时
127
+ getCountdown(): Observable<number>
128
+
129
+ // 格式化倒计时
130
+ formatCountdown(seconds: number): string
131
+
132
+ // 检查状态
133
+ isPaused(): boolean
134
+ canExtend(): boolean
135
+ getCurrentMode(): 'fixed' | 'idle'
136
+ ```
137
+
138
+ ## 🎯 使用场景
139
+
140
+ ### 场景1:办公系统(固定时间)
141
+
142
+ ```typescript
143
+ {
144
+ mode: 'fixed',
145
+ timeout: 300, // 5分钟
146
+ warningTime: 30,
147
+ maxExtendTimes: 3, // 最多延长3次
148
+ onWarning: (s) => this.showWarning(s),
149
+ onTimeout: () => this.authService.logout()
150
+ }
151
+ ```
152
+
153
+ ### 场景2:展示屏(空闲超时)
154
+
155
+ ```typescript
156
+ {
157
+ mode: 'idle',
158
+ timeout: 600, // 10分钟无操作
159
+ warningTime: 60,
160
+ maxExtendTimes: -1, // 无限延长
161
+ onWarning: (s) => this.showWarning(s),
162
+ onTimeout: () => this.resetToHome()
163
+ }
164
+ ```
165
+
166
+ ## 🔧 高级用法
167
+
168
+ ### 自定义警告UI
169
+
170
+ ```typescript
171
+ // 不使用内置Toast,完全自定义
172
+ {
173
+ onWarning: (seconds) => {
174
+ // 显示自定义警告对话框
175
+ this.dialog.open(WarningDialogComponent, {
176
+ data: { seconds }
177
+ });
178
+ },
179
+ onTimeout: () => {
180
+ // 自定义超时处理
181
+ this.saveUnsavedData();
182
+ this.router.navigate(['/login']);
183
+ }
184
+ }
185
+ ```
186
+
187
+ ### 动态调整配置
188
+
189
+ ```typescript
190
+ // 在不同页面使用不同配置
191
+ ngOnInit() {
192
+ if (this.route.snapshot.data['longTimeout']) {
193
+ this.autoLogout.startMonitoring({
194
+ timeout: 1800, // 表单页面30分钟
195
+ warningTime: 120
196
+ });
197
+ }
198
+ }
199
+ ```
200
+
201
+ ## 📝 注意事项
202
+
203
+ 1. **无UI依赖**:本库不提供UI组件,需自行实现倒计时显示
204
+ 2. **回调函数**:通过 `onWarning` 和 `onTimeout` 回调实现自定义行为
205
+ 3. **localStorage**:使用 localStorage 存储状态
206
+ 4. **兼容性**:支持 Angular 9.x - 20.x,RxJS 6.5+
207
+
208
+ ## 🐛 常见问题
209
+
210
+ ### Q: 如何显示倒计时?
211
+
212
+ A: 订阅 `getCountdown()` Observable,在模板中显示:
213
+
214
+ ```typescript
215
+ countdown$ = this.autoLogout.getCountdown();
216
+ ```
217
+
218
+ ```html
219
+ <div>{{ countdown$ | async | date:'mm:ss' }}</div>
220
+ ```
221
+
222
+ ### Q: 如何在登录时重置?
223
+
224
+ A: 调用 `clearLoginTime()`:
225
+
226
+ ```typescript
227
+ loginSuccess() {
228
+ this.autoLogout.clearLoginTime();
229
+ this.autoLogout.startMonitoring(config);
230
+ }
231
+ ```
232
+
233
+ ### Q: 支持哪些 Angular 版本?
234
+
235
+ A: 支持 Angular 9.x - 20.x,RxJS 6.5+
236
+
237
+ ## 📄 License
238
+
239
+ MIT
240
+
241
+ ## 👥 贡献
242
+
243
+ 欢迎提交 Issue 和 Pull Request!
244
+
245
+ ---
246
+
247
+ **Made with ❤️ by SmarterLab Team**
package/dist/README.md ADDED
@@ -0,0 +1,247 @@
1
+ # ngx-auto-logout - Angular Library
2
+
3
+ > Angular 自动登出组件库 - 支持 Angular 9.x+,零第三方依赖(除RxJS)
4
+
5
+ ## ✨ 特性
6
+
7
+ - 🔄 **双模式支持**:固定时间模式 & 空闲超时模式
8
+ - ⏱️ **可配置时长**:超时时间、警告时间均可配置
9
+ - 🔔 **智能提醒**:倒计时警告 + 自定义回调
10
+ - ⏸️ **暂停/恢复**:支持临时暂停自动登出
11
+ - ➕ **延长会话**:用户可主动延长会话时间
12
+ - 💾 **持久化**:localStorage 持久化,刷新页面保持状态
13
+ - 🎯 **零依赖**:仅依赖 RxJS,无 Toastr 等UI库依赖
14
+ - 📦 **兼容性强**:支持 Angular 9.x - 20.x
15
+
16
+ ## 📦 安装
17
+
18
+ ```bash
19
+ npm install ngx-auto-logout
20
+ ```
21
+
22
+ ## 🚀 快速开始
23
+
24
+ ### 1. 导入模块
25
+
26
+ ```typescript
27
+ // app.module.ts (Angular 9-14)
28
+ import { AutoLogoutModule } from 'ngx-auto-logout';
29
+
30
+ @NgModule({
31
+ imports: [
32
+ AutoLogoutModule.forRoot({
33
+ mode: 'fixed',
34
+ timeout: 300,
35
+ warningTime: 30,
36
+ toastTime: 10,
37
+ onWarning: (seconds) => console.log(`警告: 还剩${seconds}秒`),
38
+ onTimeout: () => this.authService.logout()
39
+ })
40
+ ]
41
+ })
42
+ export class AppModule {}
43
+ ```
44
+
45
+ ```typescript
46
+ // app.config.ts (Angular 15+)
47
+ import { provideAutoLogout } from 'ngx-auto-logout';
48
+
49
+ export const appConfig: ApplicationConfig = {
50
+ providers: [
51
+ provideAutoLogout({
52
+ mode: 'fixed',
53
+ timeout: 300,
54
+ warningTime: 30,
55
+ onWarning: (seconds) => console.log(`警告: 还剩${seconds}秒`),
56
+ onTimeout: () => authService.logout()
57
+ })
58
+ ]
59
+ };
60
+ ```
61
+
62
+ ### 2. 在组件中使用
63
+
64
+ ```typescript
65
+ import { Component } from '@angular/core';
66
+ import { AutoLogoutService } from 'ngx-auto-logout';
67
+
68
+ @Component({...})
69
+ export class AppComponent {
70
+ constructor(private autoLogout: AutoLogoutService) {
71
+ // 订阅倒计时
72
+ this.autoLogout.getCountdown().subscribe(seconds => {
73
+ console.log('剩余时间:', seconds);
74
+ });
75
+ }
76
+
77
+ // 延长会话
78
+ extendSession() {
79
+ this.autoLogout.extendSession(1800); // 延长30分钟
80
+ }
81
+
82
+ // 暂停/恢复
83
+ togglePause() {
84
+ if (this.autoLogout.isPaused()) {
85
+ this.autoLogout.resume();
86
+ } else {
87
+ this.autoLogout.pause();
88
+ }
89
+ }
90
+ }
91
+ ```
92
+
93
+ ## 📖 API 文档
94
+
95
+ ### AutoLogoutConfig
96
+
97
+ ```typescript
98
+ interface AutoLogoutConfig {
99
+ mode: 'fixed' | 'idle'; // 模式
100
+ timeout: number; // 超时时间(秒)
101
+ warningTime?: number; // 警告时间(秒),默认30
102
+ maxExtendTimes?: number; // 最大延长次数,-1=无限制
103
+ onWarning?: (seconds: number) => void; // 警告回调
104
+ onTimeout?: () => void; // 超时回调
105
+ }
106
+ ```
107
+
108
+ ### AutoLogoutService
109
+
110
+ ```typescript
111
+ // 启动监控
112
+ startMonitoring(config?: Partial<AutoLogoutConfig>): void
113
+
114
+ // 停止监控
115
+ stopMonitoring(): void
116
+
117
+ // 延长会话
118
+ extendSession(seconds?: number): boolean
119
+
120
+ // 暂停
121
+ pause(): void
122
+
123
+ // 恢复
124
+ resume(): void
125
+
126
+ // 获取倒计时
127
+ getCountdown(): Observable<number>
128
+
129
+ // 格式化倒计时
130
+ formatCountdown(seconds: number): string
131
+
132
+ // 检查状态
133
+ isPaused(): boolean
134
+ canExtend(): boolean
135
+ getCurrentMode(): 'fixed' | 'idle'
136
+ ```
137
+
138
+ ## 🎯 使用场景
139
+
140
+ ### 场景1:办公系统(固定时间)
141
+
142
+ ```typescript
143
+ {
144
+ mode: 'fixed',
145
+ timeout: 300, // 5分钟
146
+ warningTime: 30,
147
+ maxExtendTimes: 3, // 最多延长3次
148
+ onWarning: (s) => this.showWarning(s),
149
+ onTimeout: () => this.authService.logout()
150
+ }
151
+ ```
152
+
153
+ ### 场景2:展示屏(空闲超时)
154
+
155
+ ```typescript
156
+ {
157
+ mode: 'idle',
158
+ timeout: 600, // 10分钟无操作
159
+ warningTime: 60,
160
+ maxExtendTimes: -1, // 无限延长
161
+ onWarning: (s) => this.showWarning(s),
162
+ onTimeout: () => this.resetToHome()
163
+ }
164
+ ```
165
+
166
+ ## 🔧 高级用法
167
+
168
+ ### 自定义警告UI
169
+
170
+ ```typescript
171
+ // 不使用内置Toast,完全自定义
172
+ {
173
+ onWarning: (seconds) => {
174
+ // 显示自定义警告对话框
175
+ this.dialog.open(WarningDialogComponent, {
176
+ data: { seconds }
177
+ });
178
+ },
179
+ onTimeout: () => {
180
+ // 自定义超时处理
181
+ this.saveUnsavedData();
182
+ this.router.navigate(['/login']);
183
+ }
184
+ }
185
+ ```
186
+
187
+ ### 动态调整配置
188
+
189
+ ```typescript
190
+ // 在不同页面使用不同配置
191
+ ngOnInit() {
192
+ if (this.route.snapshot.data['longTimeout']) {
193
+ this.autoLogout.startMonitoring({
194
+ timeout: 1800, // 表单页面30分钟
195
+ warningTime: 120
196
+ });
197
+ }
198
+ }
199
+ ```
200
+
201
+ ## 📝 注意事项
202
+
203
+ 1. **无UI依赖**:本库不提供UI组件,需自行实现倒计时显示
204
+ 2. **回调函数**:通过 `onWarning` 和 `onTimeout` 回调实现自定义行为
205
+ 3. **localStorage**:使用 localStorage 存储状态
206
+ 4. **兼容性**:支持 Angular 9.x - 20.x,RxJS 6.5+
207
+
208
+ ## 🐛 常见问题
209
+
210
+ ### Q: 如何显示倒计时?
211
+
212
+ A: 订阅 `getCountdown()` Observable,在模板中显示:
213
+
214
+ ```typescript
215
+ countdown$ = this.autoLogout.getCountdown();
216
+ ```
217
+
218
+ ```html
219
+ <div>{{ countdown$ | async | date:'mm:ss' }}</div>
220
+ ```
221
+
222
+ ### Q: 如何在登录时重置?
223
+
224
+ A: 调用 `clearLoginTime()`:
225
+
226
+ ```typescript
227
+ loginSuccess() {
228
+ this.autoLogout.clearLoginTime();
229
+ this.autoLogout.startMonitoring(config);
230
+ }
231
+ ```
232
+
233
+ ### Q: 支持哪些 Angular 版本?
234
+
235
+ A: 支持 Angular 9.x - 20.x,RxJS 6.5+
236
+
237
+ ## 📄 License
238
+
239
+ MIT
240
+
241
+ ## 👥 贡献
242
+
243
+ 欢迎提交 Issue 和 Pull Request!
244
+
245
+ ---
246
+
247
+ **Made with ❤️ by SmarterLab Team**