@acontplus/ng-auth 1.0.3 → 1.1.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.
package/README.md CHANGED
@@ -12,6 +12,8 @@ npm install @acontplus/ng-auth
12
12
 
13
13
  - **Auth Guard**: Route protection with automatic redirect to login page
14
14
  - **Auth Token Service**: JWT token management and authentication state handling
15
+ - **URL Redirection Strategy**: Automatically redirects users back to their intended destination after login
16
+ - **Session Expiry Handling**: Manages session expiry during HTTP requests with automatic redirection
15
17
  - **CSRF Protection**: Built-in CSRF token management for secure API requests
16
18
  - **Token Repository**: Secure token storage and retrieval with local storage support
17
19
  - **Authentication Use Cases**: Login, register, refresh token, and logout functionality
@@ -20,27 +22,166 @@ npm install @acontplus/ng-auth
20
22
  - **Angular Integration**: Seamless integration with Angular Router and HTTP client
21
23
  - **Type Safety**: Full TypeScript support with comprehensive type definitions
22
24
 
23
- ## Usage
25
+ ## URL Redirection Strategy
26
+
27
+ The module includes a comprehensive URL redirection strategy that ensures users are returned to their intended destination after authentication, even when sessions expire.
28
+
29
+ ### Components
24
30
 
25
- ### Route Protection with Auth Guard
31
+ 1. **UrlRedirectService** - Manages URL storage and redirection logic
32
+ 2. **Enhanced Auth Guard** - Captures URLs before redirecting to login
33
+ 3. **Enhanced Login Use Case** - Redirects to stored URLs after successful authentication
34
+ 4. **Auth Redirect Interceptor** - Handles session expiry during HTTP requests (optional)
35
+
36
+ ### Basic Setup
26
37
 
27
38
  ```typescript
39
+ // app.config.ts
28
40
  import { authGuard } from '@acontplus/ng-auth';
29
41
 
30
42
  const routes: Routes = [
31
43
  {
32
44
  path: 'dashboard',
33
45
  component: DashboardComponent,
34
- canActivate: [authGuard]
35
- },
36
- {
37
- path: 'admin',
38
- component: AdminComponent,
39
- canActivate: [authGuard]
46
+ canActivate: [authGuard] // Automatically captures URL if not authenticated
40
47
  }
41
48
  ];
42
49
  ```
43
50
 
51
+ ### Complete Setup (with session expiry handling)
52
+
53
+ ```typescript
54
+ // app.config.ts
55
+ import { authRedirectInterceptor } from '@acontplus/ng-auth';
56
+
57
+ export const appConfig: ApplicationConfig = {
58
+ providers: [
59
+ provideHttpClient(
60
+ withInterceptors([
61
+ authRedirectInterceptor, // Handles session expiry during API calls
62
+ ])
63
+ ),
64
+ ],
65
+ };
66
+ ```
67
+
68
+ ### How It Works
69
+
70
+ **Scenario 1 - Route Protection:**
71
+ ```
72
+ User → /dashboard → Auth Guard → Store URL → Login → Success → Redirect to /dashboard
73
+ ```
74
+
75
+ **Scenario 2 - Session Expiry (with interceptor):**
76
+ ```
77
+ User on /reports → API Call → 401 Error → Store URL → Login → Success → Redirect to /reports
78
+ ```
79
+
80
+ ### Manual Usage
81
+
82
+ ```typescript
83
+ import { UrlRedirectService } from '@acontplus/ng-auth';
84
+
85
+ @Component({...})
86
+ export class MyComponent {
87
+ constructor(private urlRedirectService: UrlRedirectService) {}
88
+
89
+ navigateToForm() {
90
+ // Store current URL before potentially losing session
91
+ this.urlRedirectService.storeCurrentUrlIfAllowed();
92
+ }
93
+ }
94
+ ```
95
+
96
+ ## Usage
97
+
98
+ ## Usage
99
+
100
+ ### Authentication Service
101
+
102
+ ```typescript
103
+ import { AuthTokenService } from '@acontplus/ng-auth';
104
+
105
+ @Component({...})
106
+ export class MyComponent {
107
+ constructor(private authService: AuthTokenService) {}
108
+
109
+ isUserAuthenticated(): boolean {
110
+ return this.authService.isAuthenticated();
111
+ }
112
+ }
113
+ ```
114
+
115
+ ### Using Authentication Use Cases
116
+
117
+ ```typescript
118
+ import { LoginUseCase, LogoutUseCase } from '@acontplus/ng-auth';
119
+
120
+ @Component({...})
121
+ export class AuthComponent {
122
+ constructor(
123
+ private loginUseCase: LoginUseCase,
124
+ private logoutUseCase: LogoutUseCase
125
+ ) {}
126
+
127
+ async login(credentials: LoginCredentials) {
128
+ try {
129
+ // The LoginUseCase automatically handles URL redirection after success
130
+ const result = await this.loginUseCase.execute(credentials);
131
+ } catch (error) {
132
+ // Handle login error
133
+ }
134
+ }
135
+ }
136
+ ```
137
+
138
+ ## Login UI Component
139
+
140
+ The library includes a comprehensive login UI component with Material Design styling.
141
+
142
+ ### Basic Usage
143
+
144
+ ```html
145
+ <acp-login
146
+ title="Welcome Back"
147
+ [showRegisterButton]="true">
148
+ </acp-login>
149
+ ```
150
+
151
+ ### Component Features
152
+
153
+ - **Dual Mode Support**: Toggle between login and signup modes
154
+ - **Material Design**: Built with Angular Material components
155
+ - **Responsive Layout**: Works on desktop and mobile devices
156
+ - **Theme Integration**: Automatically inherits your app's theme colors
157
+ - **Validation**: Built-in form validation with error messaging
158
+ - **Customizable**: Support for additional form fields and custom footer content
159
+
160
+ ## API Reference
161
+
162
+ ### Core Services
163
+
164
+ - **AuthTokenService**: JWT token management and authentication state
165
+ - **UrlRedirectService**: URL storage and redirection management
166
+ - **CsrfService**: CSRF token management for secure API requests
167
+ - **TokenRepository**: Secure token storage and retrieval
168
+
169
+ ### Guards and Interceptors
170
+
171
+ - **authGuard**: Route protection with automatic URL capture
172
+ - **authRedirectInterceptor**: Session expiry handling during HTTP requests (optional)
173
+
174
+ ### Use Cases
175
+
176
+ - **LoginUseCase**: Handles user login with automatic redirection
177
+ - **RegisterUseCase**: Handles user registration
178
+ - **LogoutUseCase**: Handles user logout
179
+ - **RefreshTokenUseCase**: Handles token refresh
180
+
181
+ ## Architecture
182
+
183
+ This library follows Clean Architecture principles with clear separation of concerns organized in domain, application, data, and presentation layers.
184
+
44
185
  ### Authentication Service
45
186
 
46
187
  ```typescript