@acontplus/ng-auth 1.0.3 → 1.1.1
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.
- package/README.md +198 -38
- package/fesm2022/acontplus-ng-auth.mjs +265 -108
- package/fesm2022/acontplus-ng-auth.mjs.map +1 -1
- package/index.d.ts +98 -58
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# @acontplus/ng-auth
|
|
2
2
|
|
|
3
|
-
Acontplus Angular Authentication Module providing comprehensive authentication
|
|
3
|
+
Acontplus Angular Authentication Module providing comprehensive authentication
|
|
4
|
+
and authorization features for Angular applications.
|
|
4
5
|
|
|
5
6
|
## Installation
|
|
6
7
|
|
|
@@ -12,35 +13,188 @@ npm install @acontplus/ng-auth
|
|
|
12
13
|
|
|
13
14
|
- **Auth Guard**: Route protection with automatic redirect to login page
|
|
14
15
|
- **Auth Token Service**: JWT token management and authentication state handling
|
|
16
|
+
- **URL Redirection Strategy**: Automatically redirects users back to their
|
|
17
|
+
intended destination after login
|
|
18
|
+
- **Session Expiry Handling**: Manages session expiry during HTTP requests with
|
|
19
|
+
automatic redirection
|
|
15
20
|
- **CSRF Protection**: Built-in CSRF token management for secure API requests
|
|
16
|
-
- **Token Repository**: Secure token storage and retrieval with local storage
|
|
17
|
-
|
|
21
|
+
- **Token Repository**: Secure token storage and retrieval with local storage
|
|
22
|
+
support
|
|
23
|
+
- **Authentication Use Cases**: Login, register, refresh token, and logout
|
|
24
|
+
functionality
|
|
18
25
|
- **Domain Models**: User domain models and value objects
|
|
19
|
-
- **Clean Architecture**: Organized in domain, application, data, and
|
|
20
|
-
|
|
26
|
+
- **Clean Architecture**: Organized in domain, application, data, and
|
|
27
|
+
presentation layers
|
|
28
|
+
- **Angular Integration**: Seamless integration with Angular Router and HTTP
|
|
29
|
+
client
|
|
21
30
|
- **Type Safety**: Full TypeScript support with comprehensive type definitions
|
|
22
31
|
|
|
23
|
-
##
|
|
32
|
+
## URL Redirection Strategy
|
|
33
|
+
|
|
34
|
+
The module includes a comprehensive URL redirection strategy that ensures users
|
|
35
|
+
are returned to their intended destination after authentication, even when
|
|
36
|
+
sessions expire.
|
|
37
|
+
|
|
38
|
+
### Components
|
|
24
39
|
|
|
25
|
-
|
|
40
|
+
1. **UrlRedirectService** - Manages URL storage and redirection logic
|
|
41
|
+
2. **Enhanced Auth Guard** - Captures URLs before redirecting to login
|
|
42
|
+
3. **Enhanced Login Use Case** - Redirects to stored URLs after successful
|
|
43
|
+
authentication
|
|
44
|
+
4. **Auth Redirect Interceptor** - Handles session expiry during HTTP requests
|
|
45
|
+
(optional)
|
|
46
|
+
|
|
47
|
+
### Basic Setup
|
|
26
48
|
|
|
27
49
|
```typescript
|
|
50
|
+
// app.config.ts
|
|
28
51
|
import { authGuard } from '@acontplus/ng-auth';
|
|
29
52
|
|
|
30
53
|
const routes: Routes = [
|
|
31
54
|
{
|
|
32
55
|
path: 'dashboard',
|
|
33
56
|
component: DashboardComponent,
|
|
34
|
-
canActivate: [authGuard]
|
|
57
|
+
canActivate: [authGuard], // Automatically captures URL if not authenticated
|
|
35
58
|
},
|
|
36
|
-
{
|
|
37
|
-
path: 'admin',
|
|
38
|
-
component: AdminComponent,
|
|
39
|
-
canActivate: [authGuard]
|
|
40
|
-
}
|
|
41
59
|
];
|
|
42
60
|
```
|
|
43
61
|
|
|
62
|
+
### Complete Setup (with session expiry handling)
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
// app.config.ts
|
|
66
|
+
import { authRedirectInterceptor } from '@acontplus/ng-auth';
|
|
67
|
+
|
|
68
|
+
export const appConfig: ApplicationConfig = {
|
|
69
|
+
providers: [
|
|
70
|
+
provideHttpClient(
|
|
71
|
+
withInterceptors([
|
|
72
|
+
authRedirectInterceptor, // Handles session expiry during API calls
|
|
73
|
+
]),
|
|
74
|
+
),
|
|
75
|
+
],
|
|
76
|
+
};
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### How It Works
|
|
80
|
+
|
|
81
|
+
**Scenario 1 - Route Protection:**
|
|
82
|
+
|
|
83
|
+
```
|
|
84
|
+
User → /dashboard → Auth Guard → Store URL → Login → Success → Redirect to /dashboard
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
**Scenario 2 - Session Expiry (with interceptor):**
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
User on /reports → API Call → 401 Error → Store URL → Login → Success → Redirect to /reports
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Manual Usage
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
import { UrlRedirectService } from '@acontplus/ng-auth';
|
|
97
|
+
|
|
98
|
+
@Component({...})
|
|
99
|
+
export class MyComponent {
|
|
100
|
+
constructor(private urlRedirectService: UrlRedirectService) {}
|
|
101
|
+
|
|
102
|
+
navigateToForm() {
|
|
103
|
+
// Store current URL before potentially losing session
|
|
104
|
+
this.urlRedirectService.storeCurrentUrlIfAllowed();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Usage
|
|
110
|
+
|
|
111
|
+
## Usage
|
|
112
|
+
|
|
113
|
+
### Authentication Service
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
import { AuthTokenService } from '@acontplus/ng-auth';
|
|
117
|
+
|
|
118
|
+
@Component({...})
|
|
119
|
+
export class MyComponent {
|
|
120
|
+
constructor(private authService: AuthTokenService) {}
|
|
121
|
+
|
|
122
|
+
isUserAuthenticated(): boolean {
|
|
123
|
+
return this.authService.isAuthenticated();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Using Authentication Use Cases
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
import { LoginUseCase, LogoutUseCase } from '@acontplus/ng-auth';
|
|
132
|
+
|
|
133
|
+
@Component({...})
|
|
134
|
+
export class AuthComponent {
|
|
135
|
+
constructor(
|
|
136
|
+
private loginUseCase: LoginUseCase,
|
|
137
|
+
private logoutUseCase: LogoutUseCase
|
|
138
|
+
) {}
|
|
139
|
+
|
|
140
|
+
async login(credentials: LoginCredentials) {
|
|
141
|
+
try {
|
|
142
|
+
// The LoginUseCase automatically handles URL redirection after success
|
|
143
|
+
const result = await this.loginUseCase.execute(credentials);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
// Handle login error
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Login UI Component
|
|
152
|
+
|
|
153
|
+
The library includes a comprehensive login UI component with Material Design
|
|
154
|
+
styling.
|
|
155
|
+
|
|
156
|
+
### Basic Usage
|
|
157
|
+
|
|
158
|
+
```html
|
|
159
|
+
<acp-login title="Welcome Back" [showRegisterButton]="true"> </acp-login>
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Component Features
|
|
163
|
+
|
|
164
|
+
- **Dual Mode Support**: Toggle between login and signup modes
|
|
165
|
+
- **Material Design**: Built with Angular Material components
|
|
166
|
+
- **Responsive Layout**: Works on desktop and mobile devices
|
|
167
|
+
- **Theme Integration**: Automatically inherits your app's theme colors
|
|
168
|
+
- **Validation**: Built-in form validation with error messaging
|
|
169
|
+
- **Customizable**: Support for additional form fields and custom footer content
|
|
170
|
+
|
|
171
|
+
## API Reference
|
|
172
|
+
|
|
173
|
+
### Core Services
|
|
174
|
+
|
|
175
|
+
- **AuthTokenService**: JWT token management and authentication state
|
|
176
|
+
- **UrlRedirectService**: URL storage and redirection management
|
|
177
|
+
- **CsrfService**: CSRF token management for secure API requests
|
|
178
|
+
- **TokenRepository**: Secure token storage and retrieval
|
|
179
|
+
|
|
180
|
+
### Guards and Interceptors
|
|
181
|
+
|
|
182
|
+
- **authGuard**: Route protection with automatic URL capture
|
|
183
|
+
- **authRedirectInterceptor**: Session expiry handling during HTTP requests
|
|
184
|
+
(optional)
|
|
185
|
+
|
|
186
|
+
### Use Cases
|
|
187
|
+
|
|
188
|
+
- **LoginUseCase**: Handles user login with automatic redirection
|
|
189
|
+
- **RegisterUseCase**: Handles user registration
|
|
190
|
+
- **LogoutUseCase**: Handles user logout
|
|
191
|
+
- **RefreshTokenUseCase**: Handles token refresh
|
|
192
|
+
|
|
193
|
+
## Architecture
|
|
194
|
+
|
|
195
|
+
This library follows Clean Architecture principles with clear separation of
|
|
196
|
+
concerns organized in domain, application, data, and presentation layers.
|
|
197
|
+
|
|
44
198
|
### Authentication Service
|
|
45
199
|
|
|
46
200
|
```typescript
|
|
@@ -71,7 +225,7 @@ export class LoginComponent {
|
|
|
71
225
|
|
|
72
226
|
async login(credentials: LoginCredentials) {
|
|
73
227
|
const csrfToken = await this.csrfService.getCsrfToken();
|
|
74
|
-
|
|
228
|
+
|
|
75
229
|
// Include CSRF token in your login request
|
|
76
230
|
const response = await this.http.post('/api/login', {
|
|
77
231
|
...credentials,
|
|
@@ -116,11 +270,9 @@ import { NgModule } from '@angular/core';
|
|
|
116
270
|
import { authProviders } from '@acontplus/ng-auth';
|
|
117
271
|
|
|
118
272
|
@NgModule({
|
|
119
|
-
providers: [
|
|
120
|
-
...authProviders
|
|
121
|
-
]
|
|
273
|
+
providers: [...authProviders],
|
|
122
274
|
})
|
|
123
|
-
export class AppModule {
|
|
275
|
+
export class AppModule {}
|
|
124
276
|
```
|
|
125
277
|
|
|
126
278
|
## Running unit tests
|
|
@@ -129,7 +281,8 @@ Run `nx test ng-auth` to execute the unit tests.
|
|
|
129
281
|
|
|
130
282
|
## Login Component
|
|
131
283
|
|
|
132
|
-
The `LoginComponent` provides a flexible, themeable authentication UI component
|
|
284
|
+
The `LoginComponent` provides a flexible, themeable authentication UI component
|
|
285
|
+
with support for custom fields and dynamic content.
|
|
133
286
|
|
|
134
287
|
### Basic Usage
|
|
135
288
|
|
|
@@ -138,12 +291,9 @@ import { LoginComponent } from '@acontplus/ng-auth/ui/login';
|
|
|
138
291
|
|
|
139
292
|
@Component({
|
|
140
293
|
template: `
|
|
141
|
-
<acp-login
|
|
142
|
-
title="Welcome Back"
|
|
143
|
-
[showRegisterButton]="true">
|
|
144
|
-
</acp-login>
|
|
294
|
+
<acp-login title="Welcome Back" [showRegisterButton]="true"> </acp-login>
|
|
145
295
|
`,
|
|
146
|
-
imports: [LoginComponent]
|
|
296
|
+
imports: [LoginComponent],
|
|
147
297
|
})
|
|
148
298
|
export class AuthPageComponent {}
|
|
149
299
|
```
|
|
@@ -177,7 +327,8 @@ export class AuthPageComponent {
|
|
|
177
327
|
```html
|
|
178
328
|
<acp-login
|
|
179
329
|
[additionalSigninControls]="signinExtras"
|
|
180
|
-
[additionalSignupControls]="signupExtras"
|
|
330
|
+
[additionalSignupControls]="signupExtras"
|
|
331
|
+
>
|
|
181
332
|
</acp-login>
|
|
182
333
|
```
|
|
183
334
|
|
|
@@ -193,9 +344,7 @@ Use content projection slots for custom field templates:
|
|
|
193
344
|
<mat-label>Company</mat-label>
|
|
194
345
|
<mat-select formControlName="companyId">
|
|
195
346
|
@for (company of companies; track company.id) {
|
|
196
|
-
<mat-option [value]="company.id">
|
|
197
|
-
{{ company.name }}
|
|
198
|
-
</mat-option>
|
|
347
|
+
<mat-option [value]="company.id"> {{ company.name }} </mat-option>
|
|
199
348
|
}
|
|
200
349
|
</mat-select>
|
|
201
350
|
</mat-form-field>
|
|
@@ -207,9 +356,7 @@ Use content projection slots for custom field templates:
|
|
|
207
356
|
<mat-label>Company</mat-label>
|
|
208
357
|
<mat-select formControlName="companyId" required>
|
|
209
358
|
@for (company of companies; track company.id) {
|
|
210
|
-
<mat-option [value]="company.id">
|
|
211
|
-
{{ company.name }}
|
|
212
|
-
</mat-option>
|
|
359
|
+
<mat-option [value]="company.id"> {{ company.name }} </mat-option>
|
|
213
360
|
}
|
|
214
361
|
</mat-select>
|
|
215
362
|
</mat-form-field>
|
|
@@ -225,7 +372,13 @@ Use content projection slots for custom field templates:
|
|
|
225
372
|
|
|
226
373
|
<mat-form-field class="w-100">
|
|
227
374
|
<mat-label>Validation PIN</mat-label>
|
|
228
|
-
<input
|
|
375
|
+
<input
|
|
376
|
+
matInput
|
|
377
|
+
type="text"
|
|
378
|
+
placeholder="Enter PIN"
|
|
379
|
+
formControlName="validationPin"
|
|
380
|
+
required
|
|
381
|
+
/>
|
|
229
382
|
</mat-form-field>
|
|
230
383
|
</div>
|
|
231
384
|
</acp-login>
|
|
@@ -252,13 +405,17 @@ Customize the footer with dynamic content:
|
|
|
252
405
|
|
|
253
406
|
### Theme Color Inheritance
|
|
254
407
|
|
|
255
|
-
The component uses CSS custom properties to inherit colors from the parent app's
|
|
408
|
+
The component uses CSS custom properties to inherit colors from the parent app's
|
|
409
|
+
theme:
|
|
256
410
|
|
|
257
411
|
- Background gradient: Uses `--mdc-theme-primary` and `--mdc-theme-secondary`
|
|
258
412
|
- Title color: Uses `--mdc-theme-on-surface`
|
|
259
|
-
- Error alerts: Uses `--mdc-theme-error`, `--mdc-theme-error-container`,
|
|
413
|
+
- Error alerts: Uses `--mdc-theme-error`, `--mdc-theme-error-container`,
|
|
414
|
+
`--mdc-theme-on-error-container`
|
|
415
|
+
|
|
416
|
+
Fallback colors use Angular Material's Material Design 3 (M3) neutral color
|
|
417
|
+
tokens:
|
|
260
418
|
|
|
261
|
-
Fallback colors use Angular Material's Material Design 3 (M3) neutral color tokens:
|
|
262
419
|
- Primary: #5c5f5c (M3 neutral primary)
|
|
263
420
|
- Secondary: #79747e (M3 neutral secondary)
|
|
264
421
|
- On surface: #1c1b1f (M3 on-surface)
|
|
@@ -282,7 +439,10 @@ To customize colors, define these CSS variables in your app's global styles:
|
|
|
282
439
|
### Component Inputs
|
|
283
440
|
|
|
284
441
|
- `title: string` - The title displayed in the card header (default: 'Login')
|
|
285
|
-
- `showRegisterButton: boolean` - Whether to show the register button (default:
|
|
286
|
-
|
|
287
|
-
- `
|
|
442
|
+
- `showRegisterButton: boolean` - Whether to show the register button (default:
|
|
443
|
+
true)
|
|
444
|
+
- `additionalSigninControls: Record<string, AbstractControl>` - Additional
|
|
445
|
+
controls for signin form
|
|
446
|
+
- `additionalSignupControls: Record<string, AbstractControl>` - Additional
|
|
447
|
+
controls for signup form
|
|
288
448
|
- `footerContent: TemplateRef<any> | null` - Custom footer template
|