@acontplus/ng-auth 1.0.0 → 1.0.2

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 ADDED
@@ -0,0 +1,288 @@
1
+ # @acontplus/ng-auth
2
+
3
+ Acontplus Angular Authentication Module providing comprehensive authentication and authorization features for Angular applications.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @acontplus/ng-auth
9
+ ```
10
+
11
+ ## Features
12
+
13
+ - **Auth Guard**: Route protection with automatic redirect to login page
14
+ - **Auth Token Service**: JWT token management and authentication state handling
15
+ - **CSRF Protection**: Built-in CSRF token management for secure API requests
16
+ - **Token Repository**: Secure token storage and retrieval with local storage support
17
+ - **Authentication Use Cases**: Login, register, refresh token, and logout functionality
18
+ - **Domain Models**: User domain models and value objects
19
+ - **Clean Architecture**: Organized in domain, application, data, and presentation layers
20
+ - **Angular Integration**: Seamless integration with Angular Router and HTTP client
21
+ - **Type Safety**: Full TypeScript support with comprehensive type definitions
22
+
23
+ ## Usage
24
+
25
+ ### Route Protection with Auth Guard
26
+
27
+ ```typescript
28
+ import { authGuard } from '@acontplus/ng-auth';
29
+
30
+ const routes: Routes = [
31
+ {
32
+ path: 'dashboard',
33
+ component: DashboardComponent,
34
+ canActivate: [authGuard]
35
+ },
36
+ {
37
+ path: 'admin',
38
+ component: AdminComponent,
39
+ canActivate: [authGuard]
40
+ }
41
+ ];
42
+ ```
43
+
44
+ ### Authentication Service
45
+
46
+ ```typescript
47
+ import { AuthTokenService } from '@acontplus/ng-auth';
48
+
49
+ @Component({...})
50
+ export class MyComponent {
51
+ constructor(private authService: AuthTokenService) {}
52
+
53
+ isUserAuthenticated(): boolean {
54
+ return this.authService.isAuthenticated();
55
+ }
56
+
57
+ getAuthToken(): string | null {
58
+ return this.authService.getToken();
59
+ }
60
+ }
61
+ ```
62
+
63
+ ### CSRF Protection
64
+
65
+ ```typescript
66
+ import { CsrfService } from '@acontplus/ng-auth';
67
+
68
+ @Component({...})
69
+ export class LoginComponent {
70
+ constructor(private csrfService: CsrfService) {}
71
+
72
+ async login(credentials: LoginCredentials) {
73
+ const csrfToken = await this.csrfService.getCsrfToken();
74
+
75
+ // Include CSRF token in your login request
76
+ const response = await this.http.post('/api/login', {
77
+ ...credentials,
78
+ csrfToken
79
+ });
80
+ }
81
+ }
82
+ ```
83
+
84
+ ### Using Authentication Use Cases
85
+
86
+ ```typescript
87
+ import { LoginUseCase, LogoutUseCase } from '@acontplus/ng-auth';
88
+
89
+ @Component({...})
90
+ export class AuthComponent {
91
+ constructor(
92
+ private loginUseCase: LoginUseCase,
93
+ private logoutUseCase: LogoutUseCase
94
+ ) {}
95
+
96
+ async login(credentials: LoginCredentials) {
97
+ try {
98
+ const result = await this.loginUseCase.execute(credentials);
99
+ // Handle successful login
100
+ } catch (error) {
101
+ // Handle login error
102
+ }
103
+ }
104
+
105
+ async logout() {
106
+ await this.logoutUseCase.execute();
107
+ // Handle logout
108
+ }
109
+ }
110
+ ```
111
+
112
+ ### Module Setup
113
+
114
+ ```typescript
115
+ import { NgModule } from '@angular/core';
116
+ import { authProviders } from '@acontplus/ng-auth';
117
+
118
+ @NgModule({
119
+ providers: [
120
+ ...authProviders
121
+ ]
122
+ })
123
+ export class AppModule { }
124
+ ```
125
+
126
+ ## Running unit tests
127
+
128
+ Run `nx test ng-auth` to execute the unit tests.
129
+
130
+ ## Login Component
131
+
132
+ The `LoginComponent` provides a flexible, themeable authentication UI component with support for custom fields and dynamic content.
133
+
134
+ ### Basic Usage
135
+
136
+ ```typescript
137
+ import { LoginComponent } from '@acontplus/ng-auth/ui/login';
138
+
139
+ @Component({
140
+ template: `
141
+ <acp-login
142
+ title="Welcome Back"
143
+ [showRegisterButton]="true">
144
+ </acp-login>
145
+ `,
146
+ imports: [LoginComponent]
147
+ })
148
+ export class AuthPageComponent {}
149
+ ```
150
+
151
+ ### Flexible Form Fields
152
+
153
+ The component supports additional form fields through two approaches:
154
+
155
+ #### 1. Additional Form Controls via Inputs
156
+
157
+ Pass additional form controls programmatically:
158
+
159
+ ```typescript
160
+ import { FormControl, Validators } from '@angular/forms';
161
+
162
+ @Component({...})
163
+ export class AuthPageComponent {
164
+ signinExtras = {
165
+ companyId: new FormControl('', Validators.required),
166
+ rememberMe: new FormControl(false)
167
+ };
168
+
169
+ signupExtras = {
170
+ companyId: new FormControl('', Validators.required),
171
+ role: new FormControl('', Validators.required),
172
+ validationPin: new FormControl('', [Validators.required, Validators.pattern(/^\d{6}$/)])
173
+ };
174
+ }
175
+ ```
176
+
177
+ ```html
178
+ <acp-login
179
+ [additionalSigninControls]="signinExtras"
180
+ [additionalSignupControls]="signupExtras">
181
+ </acp-login>
182
+ ```
183
+
184
+ #### 2. Content Projection for Custom Templates
185
+
186
+ Use content projection slots for custom field templates:
187
+
188
+ ```html
189
+ <acp-login>
190
+ <!-- Additional fields for signin form -->
191
+ <div signin-fields>
192
+ <mat-form-field class="w-100">
193
+ <mat-label>Company</mat-label>
194
+ <mat-select formControlName="companyId">
195
+ @for (company of companies; track company.id) {
196
+ <mat-option [value]="company.id">
197
+ {{ company.name }}
198
+ </mat-option>
199
+ }
200
+ </mat-select>
201
+ </mat-form-field>
202
+ </div>
203
+
204
+ <!-- Additional fields for signup form -->
205
+ <div signup-fields>
206
+ <mat-form-field class="w-100">
207
+ <mat-label>Company</mat-label>
208
+ <mat-select formControlName="companyId" required>
209
+ @for (company of companies; track company.id) {
210
+ <mat-option [value]="company.id">
211
+ {{ company.name }}
212
+ </mat-option>
213
+ }
214
+ </mat-select>
215
+ </mat-form-field>
216
+
217
+ <mat-form-field class="w-100">
218
+ <mat-label>Role</mat-label>
219
+ <mat-select formControlName="role" required>
220
+ <mat-option value="admin">Administrator</mat-option>
221
+ <mat-option value="user">User</mat-option>
222
+ <mat-option value="manager">Manager</mat-option>
223
+ </mat-select>
224
+ </mat-form-field>
225
+
226
+ <mat-form-field class="w-100">
227
+ <mat-label>Validation PIN</mat-label>
228
+ <input matInput type="text" placeholder="Enter PIN" formControlName="validationPin" required />
229
+ </mat-form-field>
230
+ </div>
231
+ </acp-login>
232
+ ```
233
+
234
+ ### Dynamic Footer Content
235
+
236
+ Customize the footer with dynamic content:
237
+
238
+ ```html
239
+ <ng-template #customFooter>
240
+ <div class="row mt-3">
241
+ <div class="col-12 text-center">
242
+ <a mat-button color="primary" href="/terms">Terms of Service</a>
243
+ <a mat-button color="primary" href="/privacy">Privacy Policy</a>
244
+ </div>
245
+ </div>
246
+ </ng-template>
247
+
248
+ <acp-login [footerContent]="customFooter">
249
+ <!-- form content -->
250
+ </acp-login>
251
+ ```
252
+
253
+ ### Theme Color Inheritance
254
+
255
+ The component uses CSS custom properties to inherit colors from the parent app's theme:
256
+
257
+ - Background gradient: Uses `--mdc-theme-primary` and `--mdc-theme-secondary`
258
+ - Title color: Uses `--mdc-theme-on-surface`
259
+ - Error alerts: Uses `--mdc-theme-error`, `--mdc-theme-error-container`, `--mdc-theme-on-error-container`
260
+
261
+ Fallback colors use Angular Material's Material Design 3 (M3) neutral color tokens:
262
+ - Primary: #5c5f5c (M3 neutral primary)
263
+ - Secondary: #79747e (M3 neutral secondary)
264
+ - On surface: #1c1b1f (M3 on-surface)
265
+ - Error: #ba1a1a (M3 error)
266
+ - Error container: #ffdad6 (M3 error-container)
267
+ - On error container: #410002 (M3 on-error-container)
268
+
269
+ To customize colors, define these CSS variables in your app's global styles:
270
+
271
+ ```css
272
+ :root {
273
+ --mdc-theme-primary: #your-primary-color;
274
+ --mdc-theme-secondary: #your-secondary-color;
275
+ --mdc-theme-on-surface: #your-text-color;
276
+ --mdc-theme-error: #your-error-color;
277
+ --mdc-theme-error-container: #your-error-bg;
278
+ --mdc-theme-on-error-container: #your-error-text;
279
+ }
280
+ ```
281
+
282
+ ### Component Inputs
283
+
284
+ - `title: string` - The title displayed in the card header (default: 'Login')
285
+ - `showRegisterButton: boolean` - Whether to show the register button (default: true)
286
+ - `additionalSigninControls: Record<string, AbstractControl>` - Additional controls for signin form
287
+ - `additionalSignupControls: Record<string, AbstractControl>` - Additional controls for signup form
288
+ - `footerContent: TemplateRef<any> | null` - Custom footer template