@medha-analytics/medhasso-auth 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.
package/README.md ADDED
@@ -0,0 +1,297 @@
1
+ # MedHasso Authentication Library
2
+
3
+ Angular authentication library for seamless integration with MedHasso SSO services. This library provides JWT token management, route guards, HTTP interceptors, and user management utilities.
4
+
5
+ ## Features
6
+
7
+ - 🔐 **JWT Token Management** - Automatic token refresh and storage
8
+ - 🛡️ **Route Guards** - Protect routes with authentication and role-based access
9
+ - 🔄 **HTTP Interceptor** - Automatic token attachment to HTTP requests
10
+ - 👤 **User Management** - Extract and manage user information from JWT tokens
11
+ - ⚙️ **Configurable** - Extensive configuration options for different environments
12
+ - 📱 **Angular 14+ Support** - Compatible with Angular 14 through latest versions
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install @medha-analytics/medhasso-auth
18
+ ```
19
+
20
+ ## Quick Start
21
+
22
+ ### 1. Import and Configure
23
+
24
+ In your `app.config.ts` (Angular 17+ standalone) or `app.module.ts`:
25
+
26
+ #### Standalone Bootstrap (Angular 17+)
27
+ ```typescript
28
+ import { bootstrapApplication } from '@angular/platform-browser';
29
+ import { provideRouter } from '@angular/router';
30
+ import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
31
+ import { MedHassoAuthModule } from '@medha-analytics/medhasso-auth';
32
+ import { AppComponent } from './app/app.component';
33
+ import { routes } from './app/app.routes';
34
+ import { environment } from './environments/environment';
35
+
36
+ bootstrapApplication(AppComponent, {
37
+ providers: [
38
+ provideRouter(routes),
39
+ provideHttpClient(withInterceptorsFromDi()),
40
+ ...MedHassoAuthModule.forStandalone({
41
+ ssoUrl: environment.ssoUrl,
42
+ refreshUrl: environment.refreshUrl,
43
+ applicationName: 'Your App Name'
44
+ })
45
+ ]
46
+ });
47
+ ```
48
+
49
+ #### Module-based (Angular 14-16)
50
+ ```typescript
51
+ import { NgModule } from '@angular/core';
52
+ import { BrowserModule } from '@angular/platform-browser';
53
+ import { HttpClientModule } from '@angular/common/http';
54
+ import { MedHassoAuthModule } from '@medha-analytics/medhasso-auth';
55
+ import { AppComponent } from './app.component';
56
+ import { environment } from '../environments/environment';
57
+
58
+ @NgModule({
59
+ declarations: [AppComponent],
60
+ imports: [
61
+ BrowserModule,
62
+ HttpClientModule,
63
+ MedHassoAuthModule.forRoot({
64
+ ssoUrl: environment.ssoUrl,
65
+ refreshUrl: environment.refreshUrl,
66
+ applicationName: 'Your App Name'
67
+ })
68
+ ],
69
+ bootstrap: [AppComponent]
70
+ })
71
+ export class AppModule { }
72
+ ```
73
+
74
+ ### 2. Protect Routes
75
+
76
+ ```typescript
77
+ import { Routes } from '@angular/router';
78
+ import { MedHassoAuthGuard, MedHassoRoleGuard, MedHassoAdminGuard } from '@medha-analytics/medhasso-auth';
79
+
80
+ export const routes: Routes = [
81
+ {
82
+ path: 'dashboard',
83
+ component: DashboardComponent,
84
+ canActivate: [MedHassoAuthGuard]
85
+ },
86
+ {
87
+ path: 'admin',
88
+ component: AdminComponent,
89
+ canActivate: [MedHassoAdminGuard]
90
+ },
91
+ {
92
+ path: 'manager',
93
+ component: ManagerComponent,
94
+ canActivate: [MedHassoRoleGuard],
95
+ data: { roles: ['manager', 'admin'] }
96
+ }
97
+ ];
98
+ ```
99
+
100
+ ### 3. Use in Components
101
+
102
+ ```typescript
103
+ import { Component } from '@angular/core';
104
+ import { MedHassoAuthService } from '@medha-analytics/medhasso-auth';
105
+
106
+ @Component({
107
+ selector: 'app-user-profile',
108
+ template: `
109
+ <div *ngIf="authService.isAuthenticated()">
110
+ <h2>Welcome, {{ authService.getUserDisplayName() }}!</h2>
111
+ <p>Email: {{ authService.getUserEmail() }}</p>
112
+ <div *ngIf="authService.isAdmin()">
113
+ <button>Admin Panel</button>
114
+ </div>
115
+ <button (click)="logout()">Logout</button>
116
+ </div>
117
+ `
118
+ })
119
+ export class UserProfileComponent {
120
+ constructor(public authService: MedHassoAuthService) {}
121
+
122
+ logout() {
123
+ this.authService.logout();
124
+ }
125
+ }
126
+ ```
127
+
128
+ ## Configuration Options
129
+
130
+ ### Environment Setup
131
+ First, configure your environment files:
132
+
133
+ ```typescript
134
+ // src/environments/environment.ts
135
+ export const environment = {
136
+ production: false,
137
+ ssoUrl: 'https://your-dev-sso.domain.com:3000',
138
+ refreshUrl: 'https://your-dev-refresh.domain.com:4000',
139
+ // ... other config
140
+ };
141
+
142
+ // src/environments/environment.production.ts
143
+ export const environment = {
144
+ production: true,
145
+ ssoUrl: 'https://your-prod-sso.domain.com:3000',
146
+ refreshUrl: 'https://your-prod-refresh.domain.com:4000',
147
+ // ... other config
148
+ };
149
+ ```
150
+
151
+ ### Basic Configuration
152
+ ```typescript
153
+ {
154
+ ssoUrl: environment.ssoUrl,
155
+ refreshUrl: environment.refreshUrl,
156
+ applicationName: 'Your Application Name'
157
+ }
158
+ ```
159
+
160
+ ### Advanced Configuration
161
+ ```typescript
162
+ {
163
+ ssoUrl: environment.ssoUrl,
164
+ refreshUrl: environment.refreshUrl,
165
+ applicationName: 'Your Application Name',
166
+
167
+ // Token configuration
168
+ tokenConfig: {
169
+ storageType: 'localStorage', // or 'sessionStorage'
170
+ idTokenKey: 'idToken',
171
+ refreshTokenKey: 'refreshToken',
172
+ refreshBeforeExpiryMinutes: 5,
173
+ autoRefresh: true
174
+ },
175
+
176
+ // HTTP Interceptor configuration
177
+ interceptorConfig: {
178
+ enabled: true,
179
+ authHeaderPrefix: 'Bearer',
180
+ excludeUrls: ['*/public/*', '*/assets/*'],
181
+ includeUrls: ['*/api/*'] // Optional: if specified, only these URLs get tokens
182
+ },
183
+
184
+ // Security configuration
185
+ securityConfig: {
186
+ httpsOnly: true,
187
+ secureCookies: true,
188
+ additionalCspSources: ['https://your-cdn.com']
189
+ }
190
+ }
191
+ ```
192
+
193
+ ## Guards
194
+
195
+ ### MedHassoAuthGuard
196
+ Basic authentication guard that ensures user is logged in.
197
+
198
+ ### MedHassoRoleGuard
199
+ Role-based access guard. Use with route data:
200
+ ```typescript
201
+ {
202
+ path: 'admin',
203
+ component: AdminComponent,
204
+ canActivate: [MedHassoRoleGuard],
205
+ data: {
206
+ role: 'admin', // Single role
207
+ roles: ['admin', 'manager'] // Multiple roles (OR condition)
208
+ }
209
+ }
210
+ ```
211
+
212
+ ### MedHassoAdminGuard
213
+ Admin-only access guard.
214
+
215
+ ## Services
216
+
217
+ ### MedHassoAuthService
218
+ Main authentication service with methods:
219
+ - `isAuthenticated(): boolean`
220
+ - `getCurrentUser(): MedHassoUser | null`
221
+ - `hasRole(role: string): boolean`
222
+ - `isAdmin(): boolean`
223
+ - `login(returnUrl?: string): void`
224
+ - `logout(): void`
225
+ - `getUserEmail(): string | null`
226
+ - `getUserDisplayName(): string | null`
227
+
228
+ ### TokenManagementService
229
+ Low-level token management (usually not needed directly):
230
+ - `getTokenInfo(): TokenInfo | null`
231
+ - `refreshToken(): Observable<any>`
232
+ - `setTokens(idToken: string, refreshToken?: string): void`
233
+
234
+ ## Environment-Specific Setup
235
+
236
+ ### Development
237
+ ```typescript
238
+ // src/environments/environment.ts
239
+ export const environment = {
240
+ production: false,
241
+ ssoUrl: 'https://dev-sso.your-domain.com:3000',
242
+ refreshUrl: 'https://dev-refresh.your-domain.com:4000'
243
+ };
244
+
245
+ // Usage in module/config
246
+ MedHassoAuthModule.forRoot({
247
+ ssoUrl: environment.ssoUrl,
248
+ refreshUrl: environment.refreshUrl,
249
+ applicationName: 'Your App (Dev)'
250
+ })
251
+ ```
252
+
253
+ ### Production
254
+ ```typescript
255
+ // src/environments/environment.production.ts
256
+ export const environment = {
257
+ production: true,
258
+ ssoUrl: 'https://sso.your-domain.com:3000',
259
+ refreshUrl: 'https://refresh.your-domain.com:4000'
260
+ };
261
+
262
+ // Usage in module/config
263
+ MedHassoAuthModule.forRoot({
264
+ ssoUrl: environment.ssoUrl,
265
+ refreshUrl: environment.refreshUrl,
266
+ applicationName: 'Your App'
267
+ })
268
+ ```
269
+
270
+ ## CSP Configuration
271
+
272
+ For server-side Content Security Policy, include these domains:
273
+
274
+ ```javascript
275
+ // In your server configuration (e.g., Express.js)
276
+ const cspSources = [
277
+ process.env.SSO_URL || 'https://your-sso.domain.com:3000',
278
+ process.env.REFRESH_URL || 'https://your-refresh.domain.com:4000'
279
+ ];
280
+ ```
281
+
282
+ ## Migration Guide
283
+
284
+ ### From Custom Implementation
285
+ 1. Remove your existing auth guard, interceptor, and token service
286
+ 2. Install this library
287
+ 3. Update your imports and module configuration
288
+ 4. Update route guards to use library guards
289
+ 5. Update components to use `MedHassoAuthService`
290
+
291
+ ## Contributing
292
+
293
+ Please see [CONTRIBUTING.md](CONTRIBUTING.md) for details.
294
+
295
+ ## License
296
+
297
+ MIT License - see [LICENSE](LICENSE) file for details.