mesauth-angular 0.1.2 → 0.1.3

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
@@ -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 start
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. Initialize in your app (e.g. `AppComponent` constructor or `AppModule` bootstrap):
15
-
16
- ```ts
17
- // in AppComponent
18
- constructor(private mesAuth: MesAuthService) {
19
- this.mesAuth.init({
20
- apiBaseUrl: 'https://api.example.com',
21
- withCredentials: true,
22
- avatarUrl: 'https://api.example.com/avatars',
23
- userBaseUrl: 'https://api.example.com/users'
24
- });
25
-
26
- this.mesAuth.currentUser$.subscribe(user => console.log('user', user));
27
- this.mesAuth.notifications$.subscribe(n => console.log('notif', n));
28
- }
29
- ```
30
-
31
- 3. Build the package for publishing:
32
-
33
- ```bash
34
- cd /path/to/mesauth-angular
35
- npm install
36
- npm run build
37
- ```
38
-
39
- Notes
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.
@@ -105,7 +105,7 @@ let MesAuthService = class MesAuthService {
105
105
  }
106
106
  };
107
107
  MesAuthService = __decorate([
108
- Injectable({ providedIn: 'root' }),
108
+ Injectable(),
109
109
  __metadata("design:paramtypes", [HttpClient])
110
110
  ], MesAuthService);
111
111
  export { MesAuthService };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mesauth-angular",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Angular-friendly SignalR notifier + API helper for current user and notifications",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",