@snugdesk/core 0.2.41 → 0.2.42

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
@@ -18,7 +18,7 @@ To purchase licenses or for assistance with implementing the Snugdesk libraries
18
18
 
19
19
  ## ✨ Features
20
20
 
21
- - Centralized session management using `tenantId` and `userId`
21
+ - Token-based session management — your backend issues a Snugdesk session token, the library establishes the session from it
22
22
  - Shared services for authentication, token reuse, and lifecycle handling
23
23
  - Lightweight, plug-and-play design for host Angular applications
24
24
  - Built-in support for multi-widget communication and coordination
@@ -30,7 +30,6 @@ To purchase licenses or for assistance with implementing the Snugdesk libraries
30
30
  ### Step 1: Install Required Peer Dependencies
31
31
 
32
32
  ```bash
33
- npm install @angular/animations
34
33
  npm install @auth0/angular-jwt crypto-js moment-timezone uuid
35
34
  ```
36
35
 
@@ -48,9 +47,86 @@ Peer dependency ranges (from the package manifest):
48
47
  - RxJS: ~7.8.0
49
48
  - aws-amplify: ^5.0.0, @aws-amplify/api-graphql: ^3.0.0
50
49
 
50
+ > `@angular/animations` is **not** required by this library. Install it only if your own app (or another Snugdesk widget) calls `provideAnimations()`.
51
+
52
+ ---
53
+
54
+ ## 🛠 Workspace Configuration (required)
55
+
56
+ The AWS SDK for JavaScript expects Node-style globals (`global`, `process`) to exist. Without them the first `require` throws `ReferenceError: global is not defined` **before Angular bootstraps** — the symptom is a blank page with a single console error and no Angular output. Add the following once in your host application:
57
+
58
+ 1. **Create `src/custom-polyfills.ts`**
59
+
60
+ ```ts
61
+ // src/custom-polyfills.ts
62
+ (window as any).global = window;
63
+ (window as any).process = { env: { DEBUG: undefined } };
64
+ ```
65
+
66
+ Keep additional polyfills above if your app already uses this file.
67
+
68
+ 2. **Register the polyfill file in `angular.json`**
69
+
70
+ ```json
71
+ {
72
+ "projects": {
73
+ "your-app": {
74
+ "architect": {
75
+ "build": {
76
+ "options": {
77
+ "polyfills": [
78
+ "zone.js",
79
+ "src/custom-polyfills.ts"
80
+ ]
81
+ }
82
+ },
83
+ "test": {
84
+ "options": {
85
+ "polyfills": [
86
+ "zone.js",
87
+ "zone.js/testing",
88
+ "src/custom-polyfills.ts"
89
+ ]
90
+ }
91
+ }
92
+ }
93
+ }
94
+ }
95
+ }
96
+ ```
97
+
98
+ 3. **Let TypeScript know about the polyfill file**
99
+
100
+ Only needed if your `tsconfig.app.json` / `tsconfig.spec.json` use an explicit `files` array — an `include` of `src/**/*.ts` already covers it.
101
+
102
+ ```json
103
+ {
104
+ "files": [
105
+ "src/custom-polyfills.ts"
106
+ ]
107
+ }
108
+ ```
109
+
110
+ These changes ensure the library and its client SDKs run reliably in both builds and tests.
111
+
112
+ ---
113
+
51
114
  ### Step 3: Setup
52
115
 
53
- #### 1) Import the module (if you use the shared components)
116
+ #### 1) Provide `HttpClient`
117
+
118
+ The library's helpers use `HttpClient`, so the host application must provide it:
119
+
120
+ ```ts
121
+ // main.ts (standalone bootstrap)
122
+ import { provideHttpClient } from '@angular/common/http';
123
+
124
+ bootstrapApplication(AppComponent, {
125
+ providers: [provideHttpClient()],
126
+ });
127
+ ```
128
+
129
+ #### 2) Import the module (if you use the shared components)
54
130
 
55
131
  ```ts
56
132
  import { SnugdeskCoreModule } from '@snugdesk/core';
@@ -61,30 +137,96 @@ import { SnugdeskCoreModule } from '@snugdesk/core';
61
137
  export class AppModule {}
62
138
  ```
63
139
 
64
- #### 2) Initialize authentication
140
+ `SnugdeskCoreModule` exports the shared `ErrorComponent`, `FooterComponent` and `LoaderComponent`. The services are `providedIn: 'root'`, so you do **not** need this import just to use them.
141
+
142
+ #### 3) Obtain a session token
143
+
144
+ A `tenantId` and `userId` alone can no longer start a session — neither value is secret. Your **backend** exchanges a tenant integration credential for a short-lived session token.
145
+
146
+ Create an integration credential once, as a tenant administrator, from **Settings → Developers → API Credentials**. Then, from your server:
147
+
148
+ ```http
149
+ POST https://api.snugdesk.com/auth/library
150
+ x-api-key: <your API gateway key>
151
+ Authorization: Bearer <keyId>.<secret>
152
+ Content-Type: application/json
153
+
154
+ { "userId": "<the user to open the session as>" }
155
+ ```
156
+
157
+ The tenant is derived from the verified credential, so only `userId` goes in the body. The response contains a short-lived `sessionToken`:
158
+
159
+ ```json
160
+ { "data": { "sessionToken": "eyJhbGciOi..." } }
161
+ ```
162
+
163
+ > **Never put the integration credential in browser code.** Anything the page can read, so can its visitors. Fetch the token server-side and pass it to the page.
164
+
165
+ #### 4) Initialize authentication
65
166
 
66
167
  ```ts
67
168
  import { SnugdeskAuthenticationService } from '@snugdesk/core';
68
169
 
69
170
  constructor(private authenticationService: SnugdeskAuthenticationService) {}
70
171
 
71
- ngOnInit() {
72
- this.authenticationService.login('yourTenantId', 'yourUserId');
172
+ async ngOnInit(): Promise<void> {
173
+ const isAuthenticated = await this.authenticationService.authenticate(sessionToken);
174
+ if (!isAuthenticated) {
175
+ // The token was missing, malformed or expired — see the console for the reason.
176
+ return;
177
+ }
178
+
179
+ // The token carries every id, so nothing else needs to be supplied.
180
+ const tenantId = this.authenticationService.getTenantId();
181
+ const userId = this.authenticationService.getUserId();
182
+ const userSessionId = this.authenticationService.getUserSessionId();
73
183
  }
74
184
  ```
75
185
 
76
186
  > Other Snugdesk libraries will automatically retrieve and reuse the session.
77
187
 
188
+ **Token lifetime and storage.** Tokens are short-lived and held in `sessionStorage`, so a session does not outlive the browser tab. Supply a fresh token on every app initialization rather than relying on a stored one. Passing a new token replaces whatever is stored, so switching user in the same tab works without clearing storage first.
189
+
190
+ #### 5) React to session changes
191
+
192
+ ```ts
193
+ this.authenticationService.isAuthenticated$.subscribe((isAuthenticated: boolean) => {
194
+ // Fires on authenticate(), on logout(), and once on subscribe with the current value.
195
+ });
196
+ ```
197
+
198
+ #### 6) End the session
199
+
200
+ ```ts
201
+ await this.authenticationService.logout(userSessionId); // userSessionId is optional
202
+ ```
203
+
204
+ ---
205
+
78
206
  ## What you get
79
- - Authentication utilities (`SnugdeskAuthenticationService`)
80
- - Common pipes, components, and shared helpers
81
- - AppSync helper (`AppSyncHelperService`) that handles query/mutate/subscribe
207
+
208
+ **`SnugdeskAuthenticationService`**
209
+ - `authenticate(token)` – establish a session from a Snugdesk-issued token
210
+ - `logout(userSessionId?)` – end the session and clear the stored token
211
+ - `getToken()` / `getDecodedToken()` – the raw or decoded session token
212
+ - `getTenantId()` / `getUserId()` / `getUserSessionId()` – ids read back from the token
213
+ - `isAuthenticated()` – synchronous check; `isAuthenticated$` – observable stream
214
+ - `getUserPreferences()` – user preferences, falling back to tenant preferences
215
+
216
+ **Helpers**
217
+ - `AppSyncHelperService` – handles query / mutate / subscribe against AppSync. Configures Amplify with bundled defaults; override with `provideAmplifyConfig({ ... })` if you point at your own endpoint.
218
+ - `S3HelperService` – `getS3Config(token, region)` returns scoped, temporary S3 credentials for the session.
219
+
220
+ **Shared code**
221
+ - Common pipes, components (`ErrorComponent`, `FooterComponent`, `LoaderComponent`) and utilities
82
222
  - GraphQL services and shared models used by Snugdesk widgets
83
223
 
224
+ ---
225
+
84
226
  ## Support and licensing
85
227
 
86
228
  This library is a proprietary product of Snugdesk and is protected under applicable intellectual property laws.
87
229
 
88
230
  > **Note**: Usage of this library requires a valid license or an active Snugdesk subscription. Unauthorized distribution or usage is strictly prohibited.
89
231
 
90
- For licensing inquiries or to obtain a valid subscription, please contact [sales@snugdesk.com](mailto:sales@snugdesk.com).
232
+ For licensing inquiries or to obtain a valid subscription, please contact [sales@snug.in](mailto:sales@snug.in).