mesauth-angular 0.1.2 → 0.1.4
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 +175 -32
- package/dist/mes-auth.service.d.ts +1 -2
- package/dist/mes-auth.service.js +5 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,40 +2,183 @@
|
|
|
2
2
|
|
|
3
3
|
Angular helper library to connect to a backend API and SignalR hub to surface the current logged-in user and incoming notifications.
|
|
4
4
|
|
|
5
|
-
Quick
|
|
5
|
+
## Quick Start
|
|
6
6
|
|
|
7
7
|
1. Install (from local folder during development):
|
|
8
8
|
|
|
9
|
-
```bash
|
|
10
|
-
cd /path/to/your/angular-app
|
|
11
|
-
npm install ../path/to/mesauth-angular
|
|
12
|
-
```
|
|
13
|
-
|
|
14
|
-
2.
|
|
15
|
-
|
|
16
|
-
```ts
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
9
|
+
```bash
|
|
10
|
+
cd /path/to/your/angular-app
|
|
11
|
+
npm install ../path/to/mesauth-angular
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
2. Provide the service in your app:
|
|
15
|
+
- **For module-based apps**: Add `MesAuthService` to the providers in your `AppModule` or a feature module.
|
|
16
|
+
```ts
|
|
17
|
+
import { MesAuthService } from 'mesauth-angular';
|
|
18
|
+
|
|
19
|
+
@NgModule({
|
|
20
|
+
// ... other module config ...
|
|
21
|
+
providers: [MesAuthService]
|
|
22
|
+
})
|
|
23
|
+
export class AppModule { }
|
|
24
|
+
```
|
|
25
|
+
- **For standalone apps**: Add `MesAuthService` to the providers in `app.config.ts`.
|
|
26
|
+
```ts
|
|
27
|
+
import { MesAuthService } from 'mesauth-angular';
|
|
28
|
+
|
|
29
|
+
export const appConfig: ApplicationConfig = {
|
|
30
|
+
providers: [
|
|
31
|
+
// ... other providers ...
|
|
32
|
+
MesAuthService
|
|
33
|
+
]
|
|
34
|
+
};
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
3. Initialize in your app (e.g. `AppComponent` constructor or `AppModule` bootstrap):
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
// in AppComponent
|
|
41
|
+
constructor(private mesAuth: MesAuthService) {
|
|
42
|
+
this.mesAuth.init({
|
|
43
|
+
apiBaseUrl: 'https://api.example.com',
|
|
44
|
+
withCredentials: true,
|
|
45
|
+
avatarUrl: 'https://api.example.com/avatars',
|
|
46
|
+
userBaseUrl: 'https://api.example.com/users'
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
this.mesAuth.currentUser$.subscribe(user => console.log('user', user));
|
|
50
|
+
this.mesAuth.notifications$.subscribe(n => console.log('notif', n));
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Alternatively, for standalone components or functions (where constructor injection isn't available), use Angular's `inject()`:
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
// In a standalone component or function
|
|
58
|
+
const mesAuth = inject(MesAuthService);
|
|
59
|
+
mesAuth.init({
|
|
60
|
+
apiBaseUrl: 'https://api.example.com',
|
|
61
|
+
withCredentials: true,
|
|
62
|
+
avatarUrl: 'https://api.example.com/avatars',
|
|
63
|
+
userBaseUrl: 'https://api.example.com/users'
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
mesAuth.currentUser$.subscribe(user => console.log('user', user));
|
|
67
|
+
mesAuth.notifications$.subscribe(n => console.log('notif', n));
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
**For Standalone Apps (using `bootstrapApplication`):**
|
|
71
|
+
- After providing the service as above, initialize it using `APP_INITIALIZER` in your `app.config.ts` for app-wide setup, or in the root component.
|
|
72
|
+
- Example in `app.config.ts` (add to providers):
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { APP_INITIALIZER } from '@angular/core';
|
|
76
|
+
import { MesAuthService } from 'mesauth-angular';
|
|
77
|
+
|
|
78
|
+
export const appConfig: ApplicationConfig = {
|
|
79
|
+
providers: [
|
|
80
|
+
// ... other providers ...
|
|
81
|
+
MesAuthService,
|
|
82
|
+
{
|
|
83
|
+
provide: APP_INITIALIZER,
|
|
84
|
+
useFactory: (mesAuth: MesAuthService) => () => {
|
|
85
|
+
mesAuth.init({
|
|
86
|
+
apiBaseUrl: 'https://api.example.com',
|
|
87
|
+
withCredentials: true,
|
|
88
|
+
avatarUrl: 'https://api.example.com/avatars',
|
|
89
|
+
userBaseUrl: 'https://api.example.com/users'
|
|
90
|
+
});
|
|
91
|
+
},
|
|
92
|
+
deps: [MesAuthService],
|
|
93
|
+
multi: true
|
|
94
|
+
}
|
|
95
|
+
]
|
|
96
|
+
};
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
- Then, in your root component (e.g., `AppComponent`), subscribe to observables as usual:
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
// In AppComponent (standalone)
|
|
103
|
+
constructor() {
|
|
104
|
+
const mesAuth = inject(MesAuthService);
|
|
105
|
+
mesAuth.currentUser$.subscribe(user => console.log('user', user));
|
|
106
|
+
mesAuth.notifications$.subscribe(n => console.log('notif', n));
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
4. Build the package for publishing:
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
cd /path/to/mesauth-angular
|
|
114
|
+
npm install
|
|
115
|
+
npm run build
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Components
|
|
119
|
+
|
|
120
|
+
### ma-user-profile
|
|
121
|
+
|
|
122
|
+
A reusable Angular component for displaying the current user's profile information, with options for navigation and logout.
|
|
123
|
+
|
|
124
|
+
- **Description**: Renders user details (e.g., name, avatar) fetched via the MesAuthService. Supports custom event handlers for navigation and logout actions.
|
|
125
|
+
- **Inputs**: None (data is sourced from the MesAuthService).
|
|
126
|
+
- **Outputs**:
|
|
127
|
+
- `onNavigate`: Emits an event when the user triggers navigation (e.g., to a profile page). Pass a handler to define behavior.
|
|
128
|
+
- `onLogout`: Emits an event when the user logs out. Pass a handler to perform logout logic (e.g., clear tokens, redirect).
|
|
129
|
+
- **Usage Example**:
|
|
130
|
+
|
|
131
|
+
```html
|
|
132
|
+
<ma-user-profile
|
|
133
|
+
(onNavigate)="handleNavigation($event)"
|
|
134
|
+
(onLogout)="handleLogout()">
|
|
135
|
+
</ma-user-profile>
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
In your component's TypeScript file:
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
handleNavigation(event: any) {
|
|
142
|
+
// Navigate to user profile page
|
|
143
|
+
this.router.navigate(['/profile']);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
handleLogout() {
|
|
147
|
+
// Perform logout, e.g., clear session and redirect
|
|
148
|
+
this.mesAuth.logout(); // Assuming a logout method exists
|
|
149
|
+
this.router.navigate(['/login']);
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Notes
|
|
40
154
|
- The service expects an endpoint `GET /api/user/current` that returns the current user.
|
|
41
155
|
- SignalR events used: `UserUpdated` and `NewNotification` (adjust to your backend).
|
|
156
|
+
|
|
157
|
+
## Troubleshooting
|
|
158
|
+
|
|
159
|
+
### JIT Compiler Error in Production or AOT Mode
|
|
160
|
+
If you encounter an error like "The injectable 'MesAuthService' needs to be compiled using the JIT compiler, but '@angular/compiler' is not available," this typically occurs because:
|
|
161
|
+
- The package is being imported directly from source code (e.g., during development) without building it first.
|
|
162
|
+
- The client app is running in AOT (Ahead-of-Time) compilation mode, which requires pre-compiled libraries.
|
|
163
|
+
|
|
164
|
+
**Solutions:**
|
|
165
|
+
1. **Build the package for production/AOT compatibility:**
|
|
166
|
+
- Ensure you have built the package using `npm run build` (which uses ng-packagr or similar to generate AOT-ready code).
|
|
167
|
+
- Install the built package via npm (e.g., from a local tarball or registry) instead of linking to the source folder.
|
|
168
|
+
|
|
169
|
+
2. **For development (if you must link to source):**
|
|
170
|
+
- Switch your Angular app to JIT mode by bootstrapping with `@angular/platform-browser-dynamic` instead of `@angular/platform-browser`.
|
|
171
|
+
- Example in `main.ts`:
|
|
172
|
+
```ts
|
|
173
|
+
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
|
174
|
+
import { AppModule } from './app/app.module';
|
|
175
|
+
|
|
176
|
+
platformBrowserDynamic().bootstrapModule(AppModule);
|
|
177
|
+
```
|
|
178
|
+
- Note: JIT is not recommended for production; use AOT for better performance.
|
|
179
|
+
|
|
180
|
+
3. **Verify imports:**
|
|
181
|
+
- Ensure you're importing from the built package (e.g., `import { MesAuthService } from 'mesauth-angular';`) and not from the `src` folder.
|
|
182
|
+
- If using standalone components, confirm the service is available in the injection context.
|
|
183
|
+
|
|
184
|
+
If issues persist, check your Angular version compatibility and ensure the package's `package.json` includes the correct entry points for AOT.
|
|
@@ -72,7 +72,6 @@ export interface RealTimeNotificationDto {
|
|
|
72
72
|
sourceAppIconUrl?: string;
|
|
73
73
|
}
|
|
74
74
|
export declare class MesAuthService {
|
|
75
|
-
private http;
|
|
76
75
|
private hubConnection;
|
|
77
76
|
private _currentUser;
|
|
78
77
|
currentUser$: Observable<IUser | null>;
|
|
@@ -80,7 +79,7 @@ export declare class MesAuthService {
|
|
|
80
79
|
notifications$: Observable<any>;
|
|
81
80
|
private apiBase;
|
|
82
81
|
private config;
|
|
83
|
-
|
|
82
|
+
readonly http: HttpClient;
|
|
84
83
|
init(config: MesAuthConfig): void;
|
|
85
84
|
getConfig(): MesAuthConfig | null;
|
|
86
85
|
private fetchCurrentUser;
|
package/dist/mes-auth.service.js
CHANGED
|
@@ -4,10 +4,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
4
4
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
5
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
6
|
};
|
|
7
|
-
|
|
8
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
-
};
|
|
10
|
-
import { Injectable } from '@angular/core';
|
|
7
|
+
import { inject, Injectable } from '@angular/core';
|
|
11
8
|
import { HttpClient } from '@angular/common/http';
|
|
12
9
|
import { HubConnectionBuilder, LogLevel } from '@microsoft/signalr';
|
|
13
10
|
import { BehaviorSubject, Subject } from 'rxjs';
|
|
@@ -19,8 +16,7 @@ export var NotificationType;
|
|
|
19
16
|
NotificationType["Success"] = "Success";
|
|
20
17
|
})(NotificationType || (NotificationType = {}));
|
|
21
18
|
let MesAuthService = class MesAuthService {
|
|
22
|
-
constructor(
|
|
23
|
-
this.http = http;
|
|
19
|
+
constructor() {
|
|
24
20
|
this.hubConnection = null;
|
|
25
21
|
this._currentUser = new BehaviorSubject(null);
|
|
26
22
|
this.currentUser$ = this._currentUser.asObservable();
|
|
@@ -28,6 +24,8 @@ let MesAuthService = class MesAuthService {
|
|
|
28
24
|
this.notifications$ = this._notifications.asObservable();
|
|
29
25
|
this.apiBase = '';
|
|
30
26
|
this.config = null;
|
|
27
|
+
//constructor(private http: HttpClient) {}
|
|
28
|
+
this.http = inject(HttpClient);
|
|
31
29
|
}
|
|
32
30
|
init(config) {
|
|
33
31
|
this.config = config;
|
|
@@ -105,7 +103,6 @@ let MesAuthService = class MesAuthService {
|
|
|
105
103
|
}
|
|
106
104
|
};
|
|
107
105
|
MesAuthService = __decorate([
|
|
108
|
-
Injectable(
|
|
109
|
-
__metadata("design:paramtypes", [HttpClient])
|
|
106
|
+
Injectable()
|
|
110
107
|
], MesAuthService);
|
|
111
108
|
export { MesAuthService };
|