central-auth-js-sdk 1.0.10

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.
@@ -0,0 +1,473 @@
1
+ # Central Auth SDK — Integration Guide
2
+
3
+ This guide explains how to **pull the SDK** and use **all SDK functionalities** in your application.
4
+
5
+ ---
6
+
7
+ ## 1. Pull / install the SDK
8
+
9
+ ```bash
10
+ npm install central-auth-sdk
11
+ ```
12
+
13
+ Use in your app (with a bundler such as Vite, Webpack, or Parcel):
14
+
15
+ ```javascript
16
+ import { initializeAuth, startSignup, startLogin } from 'central-auth-sdk';
17
+ import 'central-auth-sdk/styles.css';
18
+ ```
19
+
20
+ Or in HTML (if your tool resolves node_modules):
21
+
22
+ ```html
23
+ <link rel="stylesheet" href="./node_modules/central-auth-sdk/styles.css" />
24
+ <script type="module">
25
+ import { startSignup, startLogin } from 'central-auth-sdk';
26
+ // ...
27
+ </script>
28
+ ```
29
+
30
+ ---
31
+
32
+ ## 2. Minimal setup
33
+
34
+ You need:
35
+
36
+ 1. A **container** element in the DOM where the SDK will render.
37
+ 2. The **SDK CSS** loaded (so the UI looks correct).
38
+ 3. A call to **`startSignup`** or **`startLogin`** with at least `container` and **`env`** (which backend to use: `local`, `dev`, `staging`, or `prod`). **`appIdentifier`** is optional; include it only if your backend expects it for OAuth or login APIs.
39
+
40
+ **Minimal signup example:**
41
+
42
+ ```html
43
+ <div id="auth-container"></div>
44
+ <link rel="stylesheet" href="central-auth-sdk/styles.css" />
45
+ <script type="module">
46
+ import { startSignup } from 'central-auth-sdk';
47
+
48
+ const instance = startSignup({
49
+ container: '#auth-container',
50
+ env: 'dev',
51
+ appIdentifier: 'your-app-id',
52
+ });
53
+ </script>
54
+ ```
55
+
56
+ **Minimal login example:**
57
+
58
+ ```html
59
+ <div id="auth-container"></div>
60
+ <link rel="stylesheet" href="central-auth-sdk/styles.css" />
61
+ <script type="module">
62
+ import { startLogin } from 'central-auth-sdk';
63
+
64
+ const instance = startLogin({
65
+ container: '#auth-container',
66
+ env: 'dev',
67
+ appIdentifier: 'your-app-id',
68
+ loginMethods: { emailPassword: true },
69
+ });
70
+ </script>
71
+ ```
72
+
73
+ ---
74
+
75
+ ## 3. All SDK functions
76
+
77
+ | Function | Purpose |
78
+ |----------|--------|
79
+ | **`initializeAuth(config)`** | (Optional) Set default config once; `startSignup` / `startLogin` can override per call. |
80
+ | **`startSignup(config)`** | Run the sign-up flow in the given container. |
81
+ | **`startLogin(config)`** | Run the login flow in the given container. |
82
+
83
+ You can use **only** `startSignup` and `startLogin` and pass full config each time. Or call **`initializeAuth`** once and then call **`auth.startSignup({})`** / **`auth.startLogin({})`** so they use the stored defaults.
84
+
85
+ ---
86
+
87
+ ## 4. initializeAuth(config)
88
+
89
+ Call once (e.g. at app load) to set defaults. Returns `{ startSignup, startLogin, getConfig }`.
90
+
91
+ | Parameter | Type | Default | Description |
92
+ |-----------|------|---------|-------------|
93
+ | `env` | string | `'local'` | Environment: `'local'` \| `'dev'` \| `'staging'` \| `'prod'`. SDK uses the predefined API URL for that env. |
94
+ | `appIdentifier` | string | — | Optional. When provided, sent with OAuth and login-related API requests; omit to not send `app_identifier`. |
95
+ | `defaultContainer` | string \| HTMLElement | — | Default container; can override in `startSignup` / `startLogin` |
96
+ | `loginMethods` | object | — | Default login methods (see Login config) |
97
+ | `frsAsSecondFactor` | boolean | `false` | Default: require face verification after primary auth |
98
+ | `enableFaceRegistration` | boolean | `true` | Whether sign-up includes face registration step |
99
+
100
+ **Environments:** The SDK uses only `env` to choose the API base URL:
101
+
102
+ | `env` | API base URL |
103
+ |-----------|----------------|
104
+ | `local` | `http://localhost:8080` |
105
+ | `dev` | `https://central-auth-dev.soham.ai/apis` |
106
+ | `staging` | `https://central-auth-staging.soham.ai/apis` |
107
+ | `prod` | `https://central-auth.soham.ai/apis` |
108
+
109
+ **Example:**
110
+
111
+ ```javascript
112
+ import { initializeAuth } from 'central-auth-sdk';
113
+
114
+ const auth = initializeAuth({
115
+ env: 'dev', // uses https://central-auth-dev.soham.ai/apis
116
+ appIdentifier: 'my-app',
117
+ defaultContainer: '#auth-container',
118
+ loginMethods: {
119
+ emailPassword: true,
120
+ emailOtp: true,
121
+ social: { google: true, microsoft: true },
122
+ },
123
+ frsAsSecondFactor: true,
124
+ });
125
+
126
+ // Later:
127
+ auth.startSignup({});
128
+ auth.startLogin({ onSignUp: () => showSignup() });
129
+ ```
130
+
131
+ ---
132
+
133
+ ## 5. startSignup(config)
134
+
135
+ Starts the **sign-up flow** (email + name → verify → mobile → verify → password → face → success). The first screen collects first name, last name, and email.
136
+
137
+ | Parameter | Type | Required | Description |
138
+ |-----------|------|----------|-------------|
139
+ | `container` | string \| HTMLElement | yes* | Where to render (*or set `defaultContainer` in `initializeAuth`) |
140
+ | `env` | string | no | Environment: `'local'` \| `'dev'` \| `'staging'` \| `'prod'` (default from init or `'local'`) |
141
+ | `appIdentifier` | string | no | Optional. When provided, sent with relevant API requests; omit to not send. |
142
+ | `enableFaceRegistration` | boolean | no | Include face registration step (default from init or `true`) |
143
+ | **`onSignupComplete(payload)`** | function | no | Called **once** when signup completes. `payload`: `email`, `mobile`, `userId` + any fields from final API (e.g. `access_token`, `refresh_token`). |
144
+ | **`onContinue()`** | function | no | Called when user clicks **Continue to application** on the success screen. Use to unmount and redirect. |
145
+ | **`onSwitchToLogin()`** | function | no | Called when user clicks **Sign in** (e.g. “Already have an account? Sign in”). Use to unmount signup and start the login flow in the **same container** (e.g. login + FRS). |
146
+
147
+ **Returns:** `{ unmount, getState, getFlow, getCurrentStep }` or `null` if container is invalid.
148
+
149
+ **Example with callbacks (including switch to login):**
150
+
151
+ ```javascript
152
+ const instance = startSignup({
153
+ container: '#auth-container',
154
+ env: 'dev',
155
+ appIdentifier: 'my-app',
156
+ onSignupComplete(payload) {
157
+ console.log('Signup done', payload);
158
+ saveToken(payload.access_token);
159
+ instance.unmount();
160
+ redirectToDashboard();
161
+ },
162
+ onContinue() {
163
+ instance.unmount();
164
+ redirectToDashboard();
165
+ },
166
+ onSwitchToLogin() {
167
+ instance.unmount();
168
+ startLogin({ container: '#auth-container', env: 'dev', appIdentifier: 'my-app', loginMethods: { emailFrs: true }, onContinue: goToApp, onSignUp: showSignup });
169
+ },
170
+ });
171
+ ```
172
+
173
+ ---
174
+
175
+ ## 5.1. Theming (optional)
176
+
177
+ All SDK functions (`initializeAuth`, `startSignup`, `startLogin`) accept an optional **`theme`** object. If omitted, the SDK renders its built-in warm-orange theme. If provided, only the specified properties are overridden — everything else keeps the default.
178
+
179
+ | Property | Type | Default | Description |
180
+ |----------|------|---------|-------------|
181
+ | `primaryColor` | string (hex) | `#D46B2D` | Buttons, links, active states, progress ring, accent bar |
182
+ | `secondaryColor` | string (hex) | — | Reserved for future accent elements |
183
+ | `backgroundColor` | string (hex) | `#F5F1EC` | Page / layout background |
184
+ | `backgroundImage` | string (URL) | — | Full-bleed background image (an overlay is added automatically) |
185
+ | `cardColor` | string (hex) | `#FFFFFF` | Card panel background |
186
+ | `textColor` | string (hex) | `#1A1714` | Primary text color |
187
+ | `fontFamily` | string | `'Plus Jakarta Sans', system-ui` | CSS font-family |
188
+ | `mode` | `'light'` \| `'dark'` \| `'auto'` | `'auto'` | Color mode. `'auto'` detects from `backgroundColor` luminance. |
189
+
190
+ The SDK **auto-derives** hover colors, glow/shadow colors, gradient endpoints, and input backgrounds from the values you provide. It also runs a WCAG contrast check and logs a warning if `primaryColor` has low contrast against the card.
191
+
192
+ **Minimal example (just brand colors):**
193
+
194
+ ```javascript
195
+ startSignup({
196
+ container: '#auth-container',
197
+ env: 'dev',
198
+ theme: {
199
+ primaryColor: '#2563EB',
200
+ },
201
+ });
202
+ ```
203
+
204
+ **Full customization:**
205
+
206
+ ```javascript
207
+ startSignup({
208
+ container: '#auth-container',
209
+ env: 'dev',
210
+ theme: {
211
+ primaryColor: '#6C63FF',
212
+ secondaryColor: '#2D2B55',
213
+ backgroundColor: '#1A1A2E',
214
+ backgroundImage: 'https://myapp.com/auth-bg.webp',
215
+ cardColor: '#1E1E2E',
216
+ textColor: '#E0E0E0',
217
+ fontFamily: '"DM Sans", sans-serif',
218
+ mode: 'dark',
219
+ },
220
+ });
221
+ ```
222
+
223
+ **Built-in presets** are available via `THEME_PRESETS`:
224
+
225
+ ```javascript
226
+ import { startSignup, THEME_PRESETS } from 'central-auth-sdk';
227
+
228
+ startSignup({
229
+ container: '#auth',
230
+ env: 'dev',
231
+ theme: THEME_PRESETS.midnight, // 'default' | 'dark' | 'ocean' | 'forest' | 'purple' | 'rose' | 'midnight'
232
+ });
233
+ ```
234
+
235
+ You can also spread a preset and override individual properties:
236
+
237
+ ```javascript
238
+ theme: { ...THEME_PRESETS.ocean, fontFamily: '"Poppins", sans-serif' }
239
+ ```
240
+
241
+ Theme can be set once via `initializeAuth` and overridden per flow:
242
+
243
+ ```javascript
244
+ const auth = initializeAuth({
245
+ env: 'dev',
246
+ theme: { primaryColor: '#E11D48' },
247
+ });
248
+
249
+ auth.startSignup({}); // uses rose primary
250
+ auth.startLogin({ theme: { primaryColor: '#2563EB' } }); // overrides to blue for login
251
+ ```
252
+
253
+ ---
254
+
255
+ ## 6. startLogin(config)
256
+
257
+ Starts the **login flow**. If the parent enables **only one** login method, the SDK skips the method picker and goes straight to that flow.
258
+
259
+ | Parameter | Type | Required | Description |
260
+ |-----------|------|----------|-------------|
261
+ | `container` | string \| HTMLElement | yes* | Where to render |
262
+ | `env` | string | no | Environment: `'local'` \| `'dev'` \| `'staging'` \| `'prod'` (default from init or `'local'`) |
263
+ | `appIdentifier` | string | no | Optional. When provided, sent with login/OAuth API requests; omit to not send. |
264
+ | **`loginMethods`** | object | no | Which login methods are enabled (see below) |
265
+ | `frsAsSecondFactor` | boolean | no | Require face verification after password/OTP/social |
266
+ | **`onSignUp()`** | function | no | Called when user clicks **Sign up** on the login screen (e.g. unmount login and start signup in the **same container**). |
267
+ | **`onLoginComplete(payload)`** | function | no | Called **once** when login completes. `payload`: `identifier` + any fields from final API (e.g. `access_token`, `refresh_token`). |
268
+ | **`onContinue()`** | function | no | Called when user clicks **Continue to application** on the success screen. |
269
+
270
+ **Login methods** (`loginMethods`):
271
+
272
+ | Key | Description |
273
+ |-----|-------------|
274
+ | `emailPassword` | Email + Password |
275
+ | `mobilePassword` | Mobile + Password |
276
+ | `emailOtp` | Email + OTP |
277
+ | `mobileOtp` | Mobile + OTP |
278
+ | `social.google` | Google sign-in |
279
+ | `social.microsoft` | Microsoft sign-in |
280
+ | `emailFrs` | Email + Face only (no password/OTP) |
281
+
282
+ **Email + FRS:** On the face capture screen, if the user has no webcam, they can click **“No webcam? Sign in with OTP instead”**. The SDK sends an OTP to the email they entered and switches to the OTP step so they can complete login without face capture. No extra configuration is required.
283
+
284
+ **Example — single method (no method picker):**
285
+
286
+ ```javascript
287
+ startLogin({
288
+ container: '#auth-container',
289
+ env: 'dev',
290
+ appIdentifier: 'my-app',
291
+ loginMethods: { emailFrs: true },
292
+ onLoginComplete(payload) {
293
+ saveToken(payload.access_token);
294
+ instance.unmount();
295
+ redirectToApp();
296
+ },
297
+ onContinue() {
298
+ instance.unmount();
299
+ redirectToApp();
300
+ },
301
+ onSignUp() {
302
+ instance.unmount();
303
+ startSignup({ container: '#auth-container', env: 'dev', appIdentifier: '...', onContinue: goToApp, onSwitchToLogin: showLoginFrs });
304
+ },
305
+ });
306
+ ```
307
+
308
+ **Example — multiple methods (user sees method picker):**
309
+
310
+ ```javascript
311
+ startLogin({
312
+ container: '#auth-container',
313
+ env: 'dev',
314
+ appIdentifier: 'my-app',
315
+ loginMethods: {
316
+ emailPassword: true,
317
+ emailOtp: true,
318
+ social: { google: true, microsoft: true },
319
+ emailFrs: true,
320
+ },
321
+ frsAsSecondFactor: true,
322
+ onLoginComplete(payload) { /* ... */ },
323
+ onContinue() { /* ... */ },
324
+ onSignUp() { /* ... */ },
325
+ });
326
+ ```
327
+
328
+ ---
329
+
330
+ ## 6.1. Switching between signup and login in the same container
331
+
332
+ To keep the user inside the SDK (same container) when they tap **Sign in** or **Sign up**:
333
+
334
+ - **From signup:** Pass **`onSwitchToLogin()`**. When the user clicks “Already have an account? Sign in”, call it to unmount signup and start login (e.g. `startLogin({ container, loginMethods: { emailFrs: true }, onContinue, onSignUp: showSignup })`).
335
+ - **From login:** Pass **`onSignUp()`**. When the user clicks “Don’t have an account? Sign up”, call it to unmount login and start signup (e.g. `startSignup({ container, onContinue, onSwitchToLogin: showLoginFrs })`).
336
+
337
+ Define `showSignup` and `showLoginFrs` (or your equivalents) so each starts the other flow in the same container and passes the other callback for the reverse link.
338
+
339
+ ---
340
+
341
+ ## 7. Instance API (returned by startSignup / startLogin)
342
+
343
+ Each call to `startSignup` or `startLogin` returns an instance (or `null` if the container is invalid):
344
+
345
+ | Method | Description |
346
+ |--------|-------------|
347
+ | **`instance.unmount()`** | Removes the SDK UI from the DOM. |
348
+ | **`instance.getState()`** | Returns current flow state (e.g. `email`, `mobile`, `userId`, `signupResult` / `loginResult`). After completion, `signupResult` or `loginResult` holds the raw final API response. |
349
+ | **`instance.getCurrentStep()`** | Current step id (e.g. `'email'`, `'password'`, `'success'`). |
350
+ | **`instance.getFlow()`** | Reference to the internal flow controller (for advanced use). |
351
+
352
+ **Example:**
353
+
354
+ ```javascript
355
+ const instance = startLogin({ container: '#auth', loginMethods: { emailPassword: true } });
356
+ // Later:
357
+ const state = instance.getState();
358
+ const step = instance.getCurrentStep();
359
+ instance.unmount();
360
+ ```
361
+
362
+ ---
363
+
364
+ ## 8. Getting tokens and details back
365
+
366
+ - **Signup:** Pass **`onSignupComplete(payload)`**. The SDK calls it once when signup finishes. `payload` includes `email`, `mobile`, `userId` and whatever your **final signup API** returns (e.g. `access_token`, `refresh_token`). Your backend should return these in the final signup response (e.g. `POST /signup/fr/enroll`).
367
+ - **Login:** Pass **`onLoginComplete(payload)`**. The SDK calls it once when login finishes. `payload` includes `identifier` and whatever your **final login API** returns (e.g. `access_token`, `refresh_token`). Your backend should return these in the final login response (e.g. `POST /login/custom`, `POST /otp/verify`, etc.).
368
+
369
+ You can also read **`instance.getState()`** after the flow completes; it includes **`signupResult`** or **`loginResult`** (the raw final API response).
370
+
371
+ ---
372
+
373
+ ## 9. Returning to the parent application
374
+
375
+ When the user clicks **Continue to application** on the success screen, the SDK calls **`onContinue()`** if you provided it. Use it to unmount the SDK and show your app (e.g. redirect or switch view):
376
+
377
+ ```javascript
378
+ onContinue() {
379
+ instance.unmount();
380
+ window.location.href = '/dashboard';
381
+ // or: showYourAppView();
382
+ }
383
+ ```
384
+
385
+ ---
386
+
387
+ ## 10. Full integration example
388
+
389
+ ```javascript
390
+ import { initializeAuth, startSignup, startLogin } from 'central-auth-sdk';
391
+ import 'central-auth-sdk/styles.css';
392
+
393
+ const auth = initializeAuth({
394
+ env: 'dev',
395
+ appIdentifier: 'my-app',
396
+ defaultContainer: '#auth-container',
397
+ loginMethods: {
398
+ emailPassword: true,
399
+ emailOtp: true,
400
+ social: { google: true, microsoft: true },
401
+ emailFrs: true,
402
+ },
403
+ frsAsSecondFactor: false,
404
+ });
405
+
406
+ let currentInstance = null;
407
+
408
+ function goToApp() {
409
+ if (currentInstance) currentInstance.unmount();
410
+ currentInstance = null;
411
+ document.getElementById('auth-container').style.display = 'none';
412
+ document.getElementById('app-root').style.display = 'block';
413
+ }
414
+
415
+ function showSignup() {
416
+ if (currentInstance) currentInstance.unmount();
417
+ document.getElementById('auth-container').style.display = 'block';
418
+ currentInstance = auth.startSignup({
419
+ onSignupComplete(payload) {
420
+ saveToken(payload.access_token);
421
+ goToApp();
422
+ },
423
+ onContinue: goToApp,
424
+ onSwitchToLogin: showLoginFrs,
425
+ });
426
+ }
427
+
428
+ function showLoginFrs() {
429
+ if (currentInstance) currentInstance.unmount();
430
+ document.getElementById('auth-container').style.display = 'block';
431
+ currentInstance = auth.startLogin({
432
+ loginMethods: { emailFrs: true },
433
+ onLoginComplete(payload) {
434
+ saveToken(payload.access_token);
435
+ goToApp();
436
+ },
437
+ onContinue: goToApp,
438
+ onSignUp: showSignup,
439
+ });
440
+ }
441
+
442
+ function showLogin(loginMethods = { emailPassword: true }) {
443
+ if (currentInstance) currentInstance.unmount();
444
+ document.getElementById('auth-container').style.display = 'block';
445
+ currentInstance = auth.startLogin({
446
+ loginMethods,
447
+ onLoginComplete(payload) {
448
+ saveToken(payload.access_token);
449
+ goToApp();
450
+ },
451
+ onContinue: goToApp,
452
+ onSignUp: showSignup,
453
+ });
454
+ }
455
+
456
+ function saveToken(token) {
457
+ if (token) localStorage.setItem('access_token', token);
458
+ }
459
+
460
+ showLogin();
461
+ ```
462
+
463
+ ---
464
+
465
+ ## 11. Backend requirements
466
+
467
+ - **CORS:** Your API must allow requests from your app’s origin (e.g. `Access-Control-Allow-Origin`).
468
+ - **Signup:** The SDK calls your signup endpoints (e.g. `/signup/start`, `/signup/email/verify`, `/signup/fr/enroll`). The **final** response can include `access_token`, `refresh_token`, etc.; the SDK forwards them in `onSignupComplete(payload)` and `getState().signupResult`.
469
+ - **Login:** The SDK calls your login endpoints (e.g. `/login/custom`, `/otp/verify`, `/login/email/fr`). The **final** response can include tokens; the SDK forwards them in `onLoginComplete(payload)` and `getState().loginResult`.
470
+ - **OAuth:** For Google/Microsoft, configure your backend with the same redirect URI your app uses. The SDK reads `login_token` and `provider` from the URL after redirect and continues the flow.
471
+ - **app_identifier:** When the parent app passes **`appIdentifier`**, the SDK sends it as `app_identifier` on login-related requests (e.g. `/login/custom`, `/login/email/fr`, OAuth start URLs, social mobile/FR). If the parent omits it, the SDK does not send the parameter.
472
+
473
+ For endpoint details and request/response shapes, see **API_INTEGRATION.md** and your backend docs.