@memberjunction/ng-auth-services 2.32.2 → 2.33.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 (2) hide show
  1. package/README.md +224 -0
  2. package/package.json +2 -2
package/README.md ADDED
@@ -0,0 +1,224 @@
1
+ # @memberjunction/ng-auth-services
2
+
3
+ The `@memberjunction/ng-auth-services` package provides authentication services for MemberJunction Explorer applications. It offers an abstraction layer that supports multiple authentication providers like Auth0 and Microsoft Authentication Library (MSAL).
4
+
5
+ ## Features
6
+
7
+ - Unified authentication service interface through an abstract base class
8
+ - Support for Auth0 authentication provider
9
+ - Support for Microsoft Authentication Library (MSAL) authentication provider
10
+ - Easy switching between providers via configuration
11
+ - Standardized methods for authentication operations (login, logout, token refresh)
12
+ - Reactive user and authentication state management
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install @memberjunction/ng-auth-services
18
+ ```
19
+
20
+ ## Requirements
21
+
22
+ - Angular 18+
23
+ - @memberjunction/core
24
+ - @auth0/auth0-angular (when using Auth0)
25
+ - @azure/msal-angular (when using MSAL)
26
+
27
+ ## Usage
28
+
29
+ ### Setup and Configuration
30
+
31
+ First, set up your authentication environment configuration:
32
+
33
+ ```typescript
34
+ // environment.ts
35
+ export const environment = {
36
+ // For Auth0
37
+ AUTH_TYPE: 'auth0',
38
+ AUTH0_DOMAIN: 'your-auth0-domain.auth0.com',
39
+ AUTH0_CLIENTID: 'your-auth0-client-id',
40
+
41
+ // For MSAL (Azure AD)
42
+ // AUTH_TYPE: 'msal',
43
+ // CLIENT_ID: 'your-azure-client-id',
44
+ // CLIENT_AUTHORITY: 'https://login.microsoftonline.com/your-tenant-id',
45
+ };
46
+ ```
47
+
48
+ Import and configure the AuthServicesModule in your app module:
49
+
50
+ ```typescript
51
+ import { AuthServicesModule, RedirectComponent } from '@memberjunction/ng-auth-services';
52
+ import { environment } from '../environments/environment';
53
+
54
+ @NgModule({
55
+ declarations: [
56
+ AppComponent,
57
+ // other components
58
+ ],
59
+ imports: [
60
+ BrowserModule,
61
+ // other imports
62
+ AuthServicesModule.forRoot(environment),
63
+ ],
64
+ providers: [],
65
+ bootstrap: [AppComponent]
66
+ })
67
+ export class AppModule { }
68
+ ```
69
+
70
+ If using MSAL, add the MsalRedirectComponent to your routes:
71
+
72
+ ```typescript
73
+ import { RedirectComponent } from '@memberjunction/ng-auth-services';
74
+
75
+ const routes: Routes = [
76
+ // Your app routes
77
+ { path: 'auth', component: RedirectComponent }
78
+ ];
79
+ ```
80
+
81
+ ### Basic Usage
82
+
83
+ Inject the `MJAuthBase` service in your components:
84
+
85
+ ```typescript
86
+ import { Component, OnInit } from '@angular/core';
87
+ import { MJAuthBase } from '@memberjunction/ng-auth-services';
88
+
89
+ @Component({
90
+ selector: 'app-user-profile',
91
+ templateUrl: './user-profile.component.html',
92
+ })
93
+ export class UserProfileComponent implements OnInit {
94
+ user: any;
95
+
96
+ constructor(private authService: MJAuthBase) {}
97
+
98
+ async ngOnInit() {
99
+ // Check if the user is authenticated
100
+ const isAuthenticated = await this.authService.isAuthenticated();
101
+ isAuthenticated.subscribe(authenticated => {
102
+ if (authenticated) {
103
+ this.loadUserProfile();
104
+ }
105
+ });
106
+ }
107
+
108
+ async loadUserProfile() {
109
+ const userObs = await this.authService.getUser();
110
+ userObs.subscribe(user => {
111
+ this.user = user;
112
+ });
113
+ }
114
+
115
+ login() {
116
+ this.authService.login();
117
+ }
118
+
119
+ logout() {
120
+ this.authService.logout();
121
+ }
122
+ }
123
+ ```
124
+
125
+ ### Advanced Usage
126
+
127
+ #### Token Refresh and Expired Token Handling
128
+
129
+ ```typescript
130
+ import { HttpErrorResponse } from '@angular/common/http';
131
+ import { MJAuthBase } from '@memberjunction/ng-auth-services';
132
+
133
+ export class ApiService {
134
+ constructor(private authService: MJAuthBase) {}
135
+
136
+ handleApiError(error: HttpErrorResponse) {
137
+ // Check if token is expired
138
+ if (error.status === 401 && this.authService.checkExpiredTokenError(error.error)) {
139
+ // Refresh the token
140
+ this.authService.refresh().then(tokenObs => {
141
+ tokenObs.subscribe(() => {
142
+ // Retry the API call
143
+ // ...
144
+ });
145
+ });
146
+ }
147
+ }
148
+ }
149
+ ```
150
+
151
+ #### Getting User Claims
152
+
153
+ ```typescript
154
+ import { MJAuthBase } from '@memberjunction/ng-auth-services';
155
+
156
+ export class UserService {
157
+ constructor(private authService: MJAuthBase) {}
158
+
159
+ async getUserEmail() {
160
+ const claimsObs = await this.authService.getUserClaims();
161
+ return claimsObs.pipe(
162
+ map(claims => claims?.email || '')
163
+ );
164
+ }
165
+ }
166
+ ```
167
+
168
+ ## API Reference
169
+
170
+ ### MJAuthBase (Abstract Class)
171
+
172
+ #### Properties
173
+
174
+ | Name | Type | Description |
175
+ |------|------|-------------|
176
+ | `authenticated` | `boolean` | Current authentication state |
177
+
178
+ #### Methods
179
+
180
+ | Name | Parameters | Return Type | Description |
181
+ |------|------------|-------------|-------------|
182
+ | `login` | `options?: any` | `Promise<any>` | Initiates the login process |
183
+ | `logout` | None | `Promise<any>` | Logs the user out |
184
+ | `refresh` | None | `Promise<Observable<any>>` | Refreshes the authentication token |
185
+ | `isAuthenticated` | None | `Promise<any>` | Checks if the user is authenticated |
186
+ | `getUser` | None | `Promise<any>` | Gets the current user information |
187
+ | `getUserClaims` | None | `Promise<Observable<any>>` | Gets the user claims from the token |
188
+ | `checkExpiredTokenError` | `error: string` | `boolean` | Checks if an error is due to an expired token |
189
+
190
+ ### MJAuth0Provider
191
+
192
+ Auth0-specific implementation of MJAuthBase.
193
+
194
+ ### MJMSALProvider
195
+
196
+ MSAL-specific implementation of MJAuthBase.
197
+
198
+ ### AuthServicesModule
199
+
200
+ #### Static Methods
201
+
202
+ | Name | Parameters | Return Type | Description |
203
+ |------|------------|-------------|-------------|
204
+ | `forRoot` | `environment: AuthEnvironment` | `ModuleWithProviders<AuthServicesModule>` | Configures the auth services module |
205
+
206
+ #### AuthEnvironment Type
207
+
208
+ ```typescript
209
+ type AuthEnvironment = {
210
+ AUTH_TYPE: string; // 'auth0' or 'msal'
211
+ CLIENT_ID: string; // For MSAL
212
+ CLIENT_AUTHORITY: string; // For MSAL
213
+ AUTH0_CLIENTID: string; // For Auth0
214
+ AUTH0_DOMAIN: string; // For Auth0
215
+ };
216
+ ```
217
+
218
+ ## Dependencies
219
+
220
+ - @angular/common
221
+ - @angular/core
222
+ - @memberjunction/core
223
+ - @auth0/auth0-angular (when using Auth0)
224
+ - @azure/msal-angular (when using MSAL)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@memberjunction/ng-auth-services",
3
- "version": "2.32.2",
3
+ "version": "2.33.0",
4
4
  "description": "MemberJunction Explorer: Authentication Services",
5
5
  "main": "./dist/public-api.js",
6
6
  "typings": "./dist/public-api.d.ts",
@@ -27,7 +27,7 @@
27
27
  "@azure/msal-angular": "^3.0.11"
28
28
  },
29
29
  "dependencies": {
30
- "@memberjunction/core": "2.32.2",
30
+ "@memberjunction/core": "2.33.0",
31
31
  "tslib": "^2.3.0"
32
32
  },
33
33
  "sideEffects": false