@monterosa/sdk-identify-kit 2.0.0-rc.1 → 2.0.0-rc.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/dist/api.d.ts +8 -5
- package/dist/index.cjs +1158 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +5 -5
- package/dist/{index.esm.js → index.js} +10 -7
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +34 -16
- package/package.json +24 -23
- package/dist/index.cjs.js +0 -1502
- package/dist/index.cjs.js.map +0 -1
- package/dist/index.esm.js.map +0 -1
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1158 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var sdkCore = require('@monterosa/sdk-core');
|
|
6
|
+
var sdkUtil = require('@monterosa/sdk-util');
|
|
7
|
+
var sdkLauncherKit = require('@monterosa/sdk-launcher-kit');
|
|
8
|
+
var sdkConnectKit = require('@monterosa/sdk-connect-kit');
|
|
9
|
+
var sdkInteractInterop = require('@monterosa/sdk-interact-interop');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @license
|
|
13
|
+
* @monterosa/sdk-identify-kit
|
|
14
|
+
*
|
|
15
|
+
* Copyright © 2023-2026 Monterosa Productions Limited. All rights reserved.
|
|
16
|
+
*
|
|
17
|
+
* More details on the license can be found at https://www.monterosa.co/sdk/license
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* @internal
|
|
21
|
+
*/
|
|
22
|
+
var IdentifyEvent;
|
|
23
|
+
(function (IdentifyEvent) {
|
|
24
|
+
IdentifyEvent["StateUpdated"] = "state_updated";
|
|
25
|
+
IdentifyEvent["LoginRequested"] = "login_requested";
|
|
26
|
+
IdentifyEvent["LogoutRequested"] = "logout_requested";
|
|
27
|
+
IdentifyEvent["SignatureUpdated"] = "signature_updated";
|
|
28
|
+
IdentifyEvent["UserdataUpdated"] = "userdata_updated";
|
|
29
|
+
IdentifyEvent["CredentialsUpdated"] = "credentials_updated";
|
|
30
|
+
IdentifyEvent["EnmasseLogin"] = "enmasse_login";
|
|
31
|
+
})(IdentifyEvent || (IdentifyEvent = {}));
|
|
32
|
+
/**
|
|
33
|
+
* Defines a set of error codes that may be encountered when using the
|
|
34
|
+
* Identify kit of the Monterosa SDK.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```javascript
|
|
38
|
+
* try {
|
|
39
|
+
* // some code that uses the IdentifyKit
|
|
40
|
+
* } catch (err) {
|
|
41
|
+
* if (err.code === IdentifyError.NoCredentials) {
|
|
42
|
+
* // handle missing credentials error
|
|
43
|
+
* } else if (err.code === IdentifyError.NotInitialised) {
|
|
44
|
+
* // handle initialization error
|
|
45
|
+
* } else {
|
|
46
|
+
* // handle other error types
|
|
47
|
+
* }
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @remarks
|
|
52
|
+
* - The `IdentifyError` enum provides a convenient way to handle errors
|
|
53
|
+
* encountered when using the `IdentifyKit` module. By checking the code
|
|
54
|
+
* property of the caught error against the values of the enum, the error
|
|
55
|
+
* type can be determined and appropriate action taken.
|
|
56
|
+
*
|
|
57
|
+
* - The `IdentifyError` enum is not intended to be instantiated or extended.
|
|
58
|
+
*/
|
|
59
|
+
exports.IdentifyError = void 0;
|
|
60
|
+
(function (IdentifyError) {
|
|
61
|
+
/**
|
|
62
|
+
* Indicates an error occurred during the call to the extension API.
|
|
63
|
+
*/
|
|
64
|
+
IdentifyError["ExtensionApiError"] = "extension_api_error";
|
|
65
|
+
/**
|
|
66
|
+
* Indicates the extension required by the IdentifyKit is not set up properly.
|
|
67
|
+
*/
|
|
68
|
+
IdentifyError["ExtensionNotSetup"] = "extension_not_setup";
|
|
69
|
+
/**
|
|
70
|
+
* Indicates the user's authentication credentials are not available
|
|
71
|
+
* or have expired.
|
|
72
|
+
*/
|
|
73
|
+
IdentifyError["NoCredentials"] = "no_credentials";
|
|
74
|
+
/**
|
|
75
|
+
* Indicates the IdentifyKit has not been initialised properly.
|
|
76
|
+
*/
|
|
77
|
+
IdentifyError["NotInitialised"] = "not_initialised";
|
|
78
|
+
/**
|
|
79
|
+
* Indicates an error occurred in the parent app.
|
|
80
|
+
*/
|
|
81
|
+
IdentifyError["ParentAppError"] = "parent_app_error";
|
|
82
|
+
/**
|
|
83
|
+
* Indicates an unexpected or invalid login state was encountered.
|
|
84
|
+
*/
|
|
85
|
+
IdentifyError["UnexpectedState"] = "unexpected_state";
|
|
86
|
+
/**
|
|
87
|
+
* Indicates there is no parent application to delegate to.
|
|
88
|
+
*/
|
|
89
|
+
IdentifyError["NoParentApplication"] = "no_parent_application";
|
|
90
|
+
/**
|
|
91
|
+
* Indicates a timeout occurred when communicating with the parent app.
|
|
92
|
+
*/
|
|
93
|
+
IdentifyError["ParentTimeoutError"] = "parent_timeout_error";
|
|
94
|
+
})(exports.IdentifyError || (exports.IdentifyError = {}));
|
|
95
|
+
/**
|
|
96
|
+
* @internal
|
|
97
|
+
*/
|
|
98
|
+
const IdentifyErrorMessages = {
|
|
99
|
+
[exports.IdentifyError.ExtensionApiError]: (error) => `Identify extension API returned an error: ${error}`,
|
|
100
|
+
[exports.IdentifyError.ExtensionNotSetup]: () => 'Identify extension is not set up for this project',
|
|
101
|
+
[exports.IdentifyError.NoCredentials]: () => 'Identify credentials are not set',
|
|
102
|
+
[exports.IdentifyError.NotInitialised]: () => 'Identify instance is not initialised',
|
|
103
|
+
[exports.IdentifyError.ParentAppError]: (error) => `Parent application error: ${error}`,
|
|
104
|
+
[exports.IdentifyError.UnexpectedState]: (state) => `Unexpected LoginState: ${state}`,
|
|
105
|
+
[exports.IdentifyError.NoParentApplication]: () => 'No parent application available for delegation',
|
|
106
|
+
[exports.IdentifyError.ParentTimeoutError]: (error) => `Parent application timeout: ${error}. Please check if the identify-kit is imported on the parent page.`,
|
|
107
|
+
};
|
|
108
|
+
var IdentifyAction;
|
|
109
|
+
(function (IdentifyAction) {
|
|
110
|
+
IdentifyAction["Login"] = "identifyLogin";
|
|
111
|
+
IdentifyAction["Logout"] = "identifyLogout";
|
|
112
|
+
IdentifyAction["RequestLogin"] = "identifyRequestLogin";
|
|
113
|
+
IdentifyAction["RequestLogout"] = "identifyRequestLogout";
|
|
114
|
+
IdentifyAction["GetUserData"] = "identifyGetUserData";
|
|
115
|
+
IdentifyAction["GetSessionSignature"] = "identifyGetSessionSignature";
|
|
116
|
+
IdentifyAction["SetCredentials"] = "identifySetCredentials";
|
|
117
|
+
IdentifyAction["ClearCredentials"] = "identifyClearCredentials";
|
|
118
|
+
IdentifyAction["OnStateUpdated"] = "identifyOnStateUpdated";
|
|
119
|
+
IdentifyAction["OnCredentialsUpdated"] = "identifyOnCredentialsUpdated";
|
|
120
|
+
IdentifyAction["OnUserDataUpdated"] = "identifyOnUserDataUpdated";
|
|
121
|
+
IdentifyAction["OnSessionSignatureUpdated"] = "identifyOnSessionSignatureUpdated";
|
|
122
|
+
})(IdentifyAction || (IdentifyAction = {}));
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* @license
|
|
126
|
+
* @monterosa/sdk-identify-kit
|
|
127
|
+
*
|
|
128
|
+
* Copyright © 2023 Monterosa Productions Limited. All rights reserved.
|
|
129
|
+
*
|
|
130
|
+
* More details on the license can be found at https://www.monterosa.co/sdk/license
|
|
131
|
+
*/
|
|
132
|
+
const EXTENSION_ID = 'lvis-id-custom-tab';
|
|
133
|
+
const SIGNATURE_TTL = 10000;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* @license
|
|
137
|
+
* @monterosa/sdk-identify-kit
|
|
138
|
+
*
|
|
139
|
+
* Copyright © 2023 Monterosa Productions Limited. All rights reserved.
|
|
140
|
+
*
|
|
141
|
+
* More details on the license can be found at https://www.monterosa.co/sdk/license
|
|
142
|
+
*/
|
|
143
|
+
class Identify extends sdkUtil.Emitter {
|
|
144
|
+
constructor(sdk, options = {}) {
|
|
145
|
+
super();
|
|
146
|
+
this.sdk = sdk;
|
|
147
|
+
this.wasLoggedIn = false;
|
|
148
|
+
this._credentials = null;
|
|
149
|
+
this._signature = null;
|
|
150
|
+
this._userData = null;
|
|
151
|
+
this._state = {
|
|
152
|
+
state: 'logged_out',
|
|
153
|
+
};
|
|
154
|
+
this._options = Object.assign({ strategy: 'email', deviceId: sdkCore.getDeviceId(), version: 1, loginPolicy: 'disabled' }, options);
|
|
155
|
+
}
|
|
156
|
+
static async fetchIdentifyHost(host, projectId) {
|
|
157
|
+
var _a, _b;
|
|
158
|
+
const listings = await sdkInteractInterop.fetchListings(host, projectId);
|
|
159
|
+
const extensionAssets = (_b = (_a = listings.assets) === null || _a === void 0 ? void 0 : _a[EXTENSION_ID]) !== null && _b !== void 0 ? _b : [];
|
|
160
|
+
const endpointAsset = extensionAssets.find(({ name }) => name === 'endpoint');
|
|
161
|
+
if (!endpointAsset) {
|
|
162
|
+
throw sdkUtil.createError(exports.IdentifyError.ExtensionNotSetup, IdentifyErrorMessages);
|
|
163
|
+
}
|
|
164
|
+
return endpointAsset.data;
|
|
165
|
+
}
|
|
166
|
+
get state() {
|
|
167
|
+
return this._state;
|
|
168
|
+
}
|
|
169
|
+
set state(state) {
|
|
170
|
+
if (this._state.state === state.state) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
switch (state.state) {
|
|
174
|
+
case 'logged_in':
|
|
175
|
+
this.wasLoggedIn = true;
|
|
176
|
+
break;
|
|
177
|
+
case 'logged_out':
|
|
178
|
+
case 'error':
|
|
179
|
+
this.wasLoggedIn = false;
|
|
180
|
+
break;
|
|
181
|
+
case 'pending':
|
|
182
|
+
// preserve wasLoggedIn value during transitional state
|
|
183
|
+
break;
|
|
184
|
+
default: {
|
|
185
|
+
// Exhaustiveness check: TypeScript errors if a new state is added
|
|
186
|
+
// but not handled. This is a compile-time error.
|
|
187
|
+
//
|
|
188
|
+
// See: https://www.typescriptlang.org/docs/handbook/2/narrowing.html#exhaustiveness-checking
|
|
189
|
+
const exhaustiveCheck = state.state;
|
|
190
|
+
throw sdkUtil.createError(exports.IdentifyError.UnexpectedState, IdentifyErrorMessages, String(exhaustiveCheck));
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
this._state = state;
|
|
194
|
+
this.emit(IdentifyEvent.StateUpdated, state);
|
|
195
|
+
}
|
|
196
|
+
get options() {
|
|
197
|
+
return this._options;
|
|
198
|
+
}
|
|
199
|
+
set signature(signature) {
|
|
200
|
+
if (this._signature === signature) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
this._signature = signature;
|
|
204
|
+
clearTimeout(this.signatureExpireTimeout);
|
|
205
|
+
if (signature !== null) {
|
|
206
|
+
this.signatureExpireTimeout = setTimeout(() => {
|
|
207
|
+
this.signature = null;
|
|
208
|
+
}, SIGNATURE_TTL);
|
|
209
|
+
}
|
|
210
|
+
this.emit(IdentifyEvent.SignatureUpdated, this.signature);
|
|
211
|
+
}
|
|
212
|
+
get signature() {
|
|
213
|
+
return this._signature;
|
|
214
|
+
}
|
|
215
|
+
set credentials(credentials) {
|
|
216
|
+
if (this._credentials === credentials) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
this._credentials = credentials;
|
|
220
|
+
this.emit(IdentifyEvent.CredentialsUpdated, credentials);
|
|
221
|
+
}
|
|
222
|
+
get credentials() {
|
|
223
|
+
return this._credentials;
|
|
224
|
+
}
|
|
225
|
+
set userData(data) {
|
|
226
|
+
if (this._userData === data) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
this._userData = data;
|
|
230
|
+
clearTimeout(this.userDataExpireTimeout);
|
|
231
|
+
this.emit(IdentifyEvent.UserdataUpdated, data);
|
|
232
|
+
}
|
|
233
|
+
get userData() {
|
|
234
|
+
return this._userData;
|
|
235
|
+
}
|
|
236
|
+
get shouldLoginOnReconnect() {
|
|
237
|
+
return (this.wasLoggedIn &&
|
|
238
|
+
this.options.loginPolicy === 'auto' &&
|
|
239
|
+
this.credentials !== null);
|
|
240
|
+
}
|
|
241
|
+
reset() {
|
|
242
|
+
this.credentials = null;
|
|
243
|
+
this.userData = null;
|
|
244
|
+
this.signature = null;
|
|
245
|
+
this.wasLoggedIn = false;
|
|
246
|
+
}
|
|
247
|
+
async getUrl(path = '') {
|
|
248
|
+
const { options: { host, projectId }, } = this.sdk;
|
|
249
|
+
if (this.host === undefined) {
|
|
250
|
+
this.host = await Identify.fetchIdentifyHost(host, projectId);
|
|
251
|
+
}
|
|
252
|
+
const url = new URL(this.host);
|
|
253
|
+
const { version, deviceId, strategy, provider } = this._options;
|
|
254
|
+
url.pathname = `/v${version}${path}`;
|
|
255
|
+
url.searchParams.set('projectId', projectId);
|
|
256
|
+
url.searchParams.set('deviceId', deviceId);
|
|
257
|
+
url.searchParams.set('strategy', strategy);
|
|
258
|
+
if (provider !== undefined) {
|
|
259
|
+
url.searchParams.set('provider', provider);
|
|
260
|
+
}
|
|
261
|
+
return url.toString();
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* @license
|
|
267
|
+
* @monterosa/sdk-identify-kit
|
|
268
|
+
*
|
|
269
|
+
* Copyright © 2025-2026 Monterosa Productions Limited. All rights reserved.
|
|
270
|
+
*
|
|
271
|
+
* More details on the license can be found at https://www.monterosa.co/sdk/license
|
|
272
|
+
*/
|
|
273
|
+
const entries = new Map();
|
|
274
|
+
function getExperiences() {
|
|
275
|
+
return Array.from(entries.values()).map((entry) => entry.experience);
|
|
276
|
+
}
|
|
277
|
+
function addExperience(experience, unsubs = []) {
|
|
278
|
+
entries.set(experience.id, {
|
|
279
|
+
experience,
|
|
280
|
+
unsubs: new Set(unsubs),
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
function deleteExperience(experience) {
|
|
284
|
+
const entry = entries.get(experience.id);
|
|
285
|
+
if (entry) {
|
|
286
|
+
entry.unsubs.forEach((unsub) => unsub());
|
|
287
|
+
entries.delete(experience.id);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* @license
|
|
293
|
+
* identify-kit
|
|
294
|
+
*
|
|
295
|
+
* Copyright © 2023-2026 Monterosa Productions Limited. All rights reserved.
|
|
296
|
+
*
|
|
297
|
+
* More details on the license can be found at https://www.monterosa.co/sdk/license
|
|
298
|
+
*/
|
|
299
|
+
const identifyKits = new Map();
|
|
300
|
+
const identifyHooks = [];
|
|
301
|
+
/**
|
|
302
|
+
* @internal
|
|
303
|
+
*/
|
|
304
|
+
const taskQueue = new sdkUtil.TaskQueue();
|
|
305
|
+
function isSdk(value) {
|
|
306
|
+
return value instanceof sdkCore.Sdk;
|
|
307
|
+
}
|
|
308
|
+
async function api(url, token, method = 'GET') {
|
|
309
|
+
const headers = {
|
|
310
|
+
accept: 'application/json',
|
|
311
|
+
};
|
|
312
|
+
let credentials;
|
|
313
|
+
// Only include Authorization header for bearer token authentication
|
|
314
|
+
// When token is 'cookie', use credentials: 'include' to ensure HttpOnly
|
|
315
|
+
// cookies are sent
|
|
316
|
+
if (token === 'cookie') {
|
|
317
|
+
// Use credentials: 'include' to ensure HttpOnly cookies are sent
|
|
318
|
+
credentials = 'include';
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
headers.Authorization = `Bearer ${token}`;
|
|
322
|
+
}
|
|
323
|
+
const response = await fetch(url, {
|
|
324
|
+
method,
|
|
325
|
+
headers,
|
|
326
|
+
credentials,
|
|
327
|
+
});
|
|
328
|
+
const data = (await response.json());
|
|
329
|
+
if (data.result < 0) {
|
|
330
|
+
throw sdkUtil.createError(exports.IdentifyError.ExtensionApiError, IdentifyErrorMessages, data.message);
|
|
331
|
+
}
|
|
332
|
+
return data;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* @internal
|
|
336
|
+
*/
|
|
337
|
+
async function parentAppRequest(identify, action, payload) {
|
|
338
|
+
var _a;
|
|
339
|
+
const parentApp = sdkLauncherKit.getParentApplication();
|
|
340
|
+
if (parentApp === null) {
|
|
341
|
+
throw sdkUtil.createError(exports.IdentifyError.NoParentApplication, IdentifyErrorMessages);
|
|
342
|
+
}
|
|
343
|
+
const { host, projectId } = identify.sdk.options;
|
|
344
|
+
// Preserve existing origin (relay) or set from current context (source)
|
|
345
|
+
const origin = (_a = payload === null || payload === void 0 ? void 0 : payload.origin) !== null && _a !== void 0 ? _a : { host, projectId };
|
|
346
|
+
try {
|
|
347
|
+
const response = await sdkLauncherKit.sendSdkRequest(parentApp, action, Object.assign(Object.assign({}, payload), { origin }));
|
|
348
|
+
const { error, data } = response.payload;
|
|
349
|
+
if (error !== undefined) {
|
|
350
|
+
throw sdkUtil.createError(exports.IdentifyError.ParentAppError, IdentifyErrorMessages, error);
|
|
351
|
+
}
|
|
352
|
+
return data;
|
|
353
|
+
}
|
|
354
|
+
catch (err) {
|
|
355
|
+
if (err instanceof sdkUtil.MonterosaError &&
|
|
356
|
+
err.code === sdkLauncherKit.BridgeError.RequestTimeoutError) {
|
|
357
|
+
const errorMessage = sdkUtil.getErrorMessage(err);
|
|
358
|
+
throw sdkUtil.createError(exports.IdentifyError.ParentTimeoutError, IdentifyErrorMessages, errorMessage);
|
|
359
|
+
}
|
|
360
|
+
throw err;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* @internal
|
|
365
|
+
*/
|
|
366
|
+
function registerIdentifyHook(hook) {
|
|
367
|
+
identifyHooks.push(hook);
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* @internal
|
|
371
|
+
*/
|
|
372
|
+
async function login(identify, credentials) {
|
|
373
|
+
const signature = await getSessionSignatureMemoized(identify, credentials);
|
|
374
|
+
const conn = await sdkConnectKit.getConnect(identify.sdk.options.host);
|
|
375
|
+
await sdkConnectKit.connect(conn);
|
|
376
|
+
await sdkConnectKit.login(conn, ...signature);
|
|
377
|
+
}
|
|
378
|
+
const loginWithRetry = sdkUtil.withRetryAsync(login);
|
|
379
|
+
/**
|
|
380
|
+
* @internal
|
|
381
|
+
*/
|
|
382
|
+
async function logout(identify) {
|
|
383
|
+
const conn = await sdkConnectKit.getConnect(identify.sdk.options.host);
|
|
384
|
+
await sdkConnectKit.connect(conn);
|
|
385
|
+
await sdkConnectKit.logout(conn);
|
|
386
|
+
}
|
|
387
|
+
const logoutWithRetry = sdkUtil.withRetryAsync(logout);
|
|
388
|
+
/**
|
|
389
|
+
* @internal
|
|
390
|
+
*/
|
|
391
|
+
async function loginTask(identify, credentials) {
|
|
392
|
+
try {
|
|
393
|
+
identify.state = {
|
|
394
|
+
state: 'pending',
|
|
395
|
+
};
|
|
396
|
+
await loginWithRetry(identify, credentials);
|
|
397
|
+
// Update state only if there are no pending tasks in the queue
|
|
398
|
+
if (taskQueue.isEmpty()) {
|
|
399
|
+
identify.state = {
|
|
400
|
+
state: 'logged_in',
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
catch (err) {
|
|
405
|
+
taskQueue.clear();
|
|
406
|
+
identify.state = {
|
|
407
|
+
state: 'error',
|
|
408
|
+
error: err instanceof Error ? err.message : 'Unknown error',
|
|
409
|
+
errorType: 'login',
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* @internal
|
|
415
|
+
*/
|
|
416
|
+
async function logoutTask(identify) {
|
|
417
|
+
try {
|
|
418
|
+
identify.state = {
|
|
419
|
+
state: 'pending',
|
|
420
|
+
};
|
|
421
|
+
await logoutWithRetry(identify);
|
|
422
|
+
// Update state only if there are no pending tasks in the queue
|
|
423
|
+
if (taskQueue.isEmpty()) {
|
|
424
|
+
identify.state = {
|
|
425
|
+
state: 'logged_out',
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
catch (err) {
|
|
430
|
+
taskQueue.clear();
|
|
431
|
+
identify.state = {
|
|
432
|
+
state: 'error',
|
|
433
|
+
error: err instanceof Error ? err.message : 'Unknown error',
|
|
434
|
+
errorType: 'logout',
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* @internal
|
|
440
|
+
*/
|
|
441
|
+
function enqueueLogin(identify, credentials, prioritise = false) {
|
|
442
|
+
if (identify.options.loginPolicy === 'auto') {
|
|
443
|
+
if (prioritise) {
|
|
444
|
+
taskQueue.enqueueFront(() => loginTask(identify, credentials));
|
|
445
|
+
}
|
|
446
|
+
else {
|
|
447
|
+
taskQueue.enqueue(() => loginTask(identify, credentials));
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
for (const experience of getExperiences()) {
|
|
451
|
+
sdkLauncherKit.sendSdkMessage(experience, IdentifyAction.Login, credentials);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* @internal
|
|
456
|
+
*/
|
|
457
|
+
function enqueueLogout(identify) {
|
|
458
|
+
if (identify.options.loginPolicy === 'auto') {
|
|
459
|
+
taskQueue.enqueue(() => logoutTask(identify));
|
|
460
|
+
}
|
|
461
|
+
for (const experience of getExperiences()) {
|
|
462
|
+
sdkLauncherKit.sendSdkMessage(experience, IdentifyAction.Logout);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
function getIdentify(sdkOrOptions, options) {
|
|
466
|
+
let sdk;
|
|
467
|
+
let identifyOptions;
|
|
468
|
+
if (isSdk(sdkOrOptions)) {
|
|
469
|
+
/**
|
|
470
|
+
* Interface: getIdentify(sdk, options?)
|
|
471
|
+
*/
|
|
472
|
+
sdk = sdkOrOptions;
|
|
473
|
+
if (options !== undefined) {
|
|
474
|
+
identifyOptions = options;
|
|
475
|
+
}
|
|
476
|
+
else {
|
|
477
|
+
identifyOptions = {};
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
else if (sdkOrOptions !== undefined) {
|
|
481
|
+
/**
|
|
482
|
+
* Interface: getIdentify(options)
|
|
483
|
+
*/
|
|
484
|
+
sdk = sdkCore.getSdk();
|
|
485
|
+
identifyOptions = sdkOrOptions;
|
|
486
|
+
}
|
|
487
|
+
else {
|
|
488
|
+
/**
|
|
489
|
+
* Interface: getIdentify()
|
|
490
|
+
*/
|
|
491
|
+
sdk = sdkCore.getSdk();
|
|
492
|
+
identifyOptions = {};
|
|
493
|
+
}
|
|
494
|
+
const { options: { projectId }, } = sdk;
|
|
495
|
+
if (identifyKits.has(projectId)) {
|
|
496
|
+
return identifyKits.get(projectId);
|
|
497
|
+
}
|
|
498
|
+
const identify = new Identify(sdk, identifyOptions);
|
|
499
|
+
for (const hook of identifyHooks) {
|
|
500
|
+
hook(identify);
|
|
501
|
+
}
|
|
502
|
+
watchConnectionStatus(identify);
|
|
503
|
+
identifyKits.set(projectId, identify);
|
|
504
|
+
return identify;
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* A function that requests a user login via the `IdentifyKit` of the Monterosa SDK.
|
|
508
|
+
*
|
|
509
|
+
* @example
|
|
510
|
+
* ```javascript
|
|
511
|
+
* import { configure } from '@monterosa/sdk-core';
|
|
512
|
+
* import { getIdentify, requestLogin } from '@monterosa/sdk-identify-kit';
|
|
513
|
+
*
|
|
514
|
+
* configure({ host: '...', projectId: '...' });
|
|
515
|
+
*
|
|
516
|
+
* try {
|
|
517
|
+
* const identify = getIdentify();
|
|
518
|
+
*
|
|
519
|
+
* await requestLogin(identify);
|
|
520
|
+
*
|
|
521
|
+
* console.log('Login request successful');
|
|
522
|
+
* } catch (err) {
|
|
523
|
+
* console.error('Error requesting login:', error.message)
|
|
524
|
+
* }
|
|
525
|
+
* ```
|
|
526
|
+
*
|
|
527
|
+
* @remarks
|
|
528
|
+
* - If the app is running within a third-party application that uses
|
|
529
|
+
* the Monterosa SDK, the function delegates to the parent app
|
|
530
|
+
* to handle the login process.
|
|
531
|
+
*
|
|
532
|
+
* @param identify - An instance of the `IdentifyKit` class used for user
|
|
533
|
+
* identification.
|
|
534
|
+
* @returns A Promise that resolves with `void` when the login request
|
|
535
|
+
* is completed.
|
|
536
|
+
*/
|
|
537
|
+
async function requestLogin(identify) {
|
|
538
|
+
const parentApp = sdkLauncherKit.getParentApplication();
|
|
539
|
+
if (parentApp !== null) {
|
|
540
|
+
await parentAppRequest(identify, IdentifyAction.RequestLogin);
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
identify.emit(IdentifyEvent.LoginRequested);
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* A function that requests a user login via the `IdentifyKit` of the Space.
|
|
547
|
+
*
|
|
548
|
+
* @example
|
|
549
|
+
* ```javascript
|
|
550
|
+
* import { getSpace } from '@monterosa/sdk-core';
|
|
551
|
+
* import { getIdentify, requestLogout } from '@monterosa/sdk-identify-kit';
|
|
552
|
+
*
|
|
553
|
+
* const space = getSpace('host', 'space-id');
|
|
554
|
+
* const identify = getIdentify(space);
|
|
555
|
+
*
|
|
556
|
+
* try {
|
|
557
|
+
* await requestLogout(identify);
|
|
558
|
+
*
|
|
559
|
+
* console.log('Logout request successful');
|
|
560
|
+
* } catch (err) {
|
|
561
|
+
* console.error('Error requesting logout:', error.message)
|
|
562
|
+
* }
|
|
563
|
+
* ```
|
|
564
|
+
*
|
|
565
|
+
* @remarks
|
|
566
|
+
* - If the app is running within a third-party application that uses
|
|
567
|
+
* the Space, the function delegates to the parent app
|
|
568
|
+
* to handle the logout process.
|
|
569
|
+
*
|
|
570
|
+
* @param identify - An instance of the `IdentifyKit` class used for user
|
|
571
|
+
* identification.
|
|
572
|
+
* @returns A Promise that resolves with `void` when the logout request
|
|
573
|
+
* is completed.
|
|
574
|
+
*/
|
|
575
|
+
async function requestLogout(identify) {
|
|
576
|
+
const parentApp = sdkLauncherKit.getParentApplication();
|
|
577
|
+
if (parentApp !== null) {
|
|
578
|
+
await parentAppRequest(identify, IdentifyAction.RequestLogout);
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
identify.emit(IdentifyEvent.LogoutRequested);
|
|
582
|
+
}
|
|
583
|
+
/**
|
|
584
|
+
* @internal
|
|
585
|
+
*
|
|
586
|
+
* Returns a signature for a user session. The signature is based on the
|
|
587
|
+
* user's identifying information provided in the `IdentifyKit` instance.
|
|
588
|
+
*
|
|
589
|
+
* @remarks
|
|
590
|
+
* This function does not handle errors. Callers are responsible for catching
|
|
591
|
+
* and handling failures.
|
|
592
|
+
*
|
|
593
|
+
* @param identify - An instance of the `IdentifyKit` class used for user
|
|
594
|
+
* identification.
|
|
595
|
+
* @param credentials - The credentials of the user.
|
|
596
|
+
*
|
|
597
|
+
* @returns A Promise that resolves to a `Signature` object.
|
|
598
|
+
*/
|
|
599
|
+
async function getSessionSignature(identify, credentials) {
|
|
600
|
+
const parentApp = sdkLauncherKit.getParentApplication();
|
|
601
|
+
if (parentApp !== null) {
|
|
602
|
+
const signature = await parentAppRequest(identify, IdentifyAction.GetSessionSignature, credentials);
|
|
603
|
+
return signature;
|
|
604
|
+
}
|
|
605
|
+
if (identify.signature !== null) {
|
|
606
|
+
return identify.signature;
|
|
607
|
+
}
|
|
608
|
+
const url = await identify.getUrl('/user/check');
|
|
609
|
+
const { data } = await api(url, credentials.token);
|
|
610
|
+
const signature = [data.userId, data.timeStamp, data.signature];
|
|
611
|
+
identify.signature = signature;
|
|
612
|
+
return signature;
|
|
613
|
+
}
|
|
614
|
+
/**
|
|
615
|
+
* A memoized version of the `getSessionSignature` function.
|
|
616
|
+
*/
|
|
617
|
+
const getSessionSignatureMemoized = sdkUtil.memoizePromise(getSessionSignature, (identify, credentials) => credentials.token, {
|
|
618
|
+
clearOnResolve: true,
|
|
619
|
+
clearOnReject: true,
|
|
620
|
+
});
|
|
621
|
+
/**
|
|
622
|
+
* The function that takes an instance of `IdentifyKit` as its argument and
|
|
623
|
+
* returns a Promise that resolves to a key-value object representing user data.
|
|
624
|
+
*
|
|
625
|
+
* @example
|
|
626
|
+
* ```javascript
|
|
627
|
+
* import { configure } from '@monterosa/sdk-core';
|
|
628
|
+
* import { getIdentify, getUserData } from '@monterosa/sdk-identify-kit';
|
|
629
|
+
*
|
|
630
|
+
* configure({ host: '...', projectId: '...' });
|
|
631
|
+
*
|
|
632
|
+
* const identify = getIdentify();
|
|
633
|
+
* const userData = await getUserData(identify);
|
|
634
|
+
*
|
|
635
|
+
* console.log('User data:', userData);
|
|
636
|
+
* ```
|
|
637
|
+
*
|
|
638
|
+
* @remarks
|
|
639
|
+
* - If the app is running within a third-party application that uses
|
|
640
|
+
* the Monterosa SDK, the function delegates to the parent app
|
|
641
|
+
* to get user data.
|
|
642
|
+
*
|
|
643
|
+
* - If the request is successful, the function resolves with a key-value object
|
|
644
|
+
* representing user data. If not, a `MonterosaError` is thrown.
|
|
645
|
+
*
|
|
646
|
+
* @param identify - An instance of `IdentifyKit` that provides the user
|
|
647
|
+
* identification functionality.
|
|
648
|
+
* @returns A Promise that resolves to a key-value object representing user data.
|
|
649
|
+
* The structure and content of the user data object depend on the identify
|
|
650
|
+
* service provider and may vary. It typically contains information about the user,
|
|
651
|
+
* but the specific fields are determined by the Identify service provider.
|
|
652
|
+
*/
|
|
653
|
+
async function getUserData(identify) {
|
|
654
|
+
const parentApp = sdkLauncherKit.getParentApplication();
|
|
655
|
+
if (parentApp !== null) {
|
|
656
|
+
const userData = await parentAppRequest(identify, IdentifyAction.GetUserData);
|
|
657
|
+
return userData;
|
|
658
|
+
}
|
|
659
|
+
// Return cached user data if available
|
|
660
|
+
if (identify.userData !== null) {
|
|
661
|
+
return identify.userData;
|
|
662
|
+
}
|
|
663
|
+
if (identify.credentials === null) {
|
|
664
|
+
throw sdkUtil.createError(exports.IdentifyError.NoCredentials, IdentifyErrorMessages);
|
|
665
|
+
}
|
|
666
|
+
const url = await identify.getUrl('/user');
|
|
667
|
+
const { data } = await api(url, identify.credentials.token);
|
|
668
|
+
identify.userData = data;
|
|
669
|
+
return data;
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
672
|
+
* Sets the user's authentication credentials.
|
|
673
|
+
*
|
|
674
|
+
* @example
|
|
675
|
+
* ```javascript
|
|
676
|
+
* import { configure } from '@monterosa/sdk-core';
|
|
677
|
+
* import { getIdentify, setCredentials } from '@monterosa/sdk-identify-kit';
|
|
678
|
+
*
|
|
679
|
+
* configure({ host: '...', projectId: '...' });
|
|
680
|
+
*
|
|
681
|
+
* const credentials = { token: 'abc123' };
|
|
682
|
+
* const identify = getIdentify();
|
|
683
|
+
*
|
|
684
|
+
* await setCredentials(identify, credentials)
|
|
685
|
+
* ```
|
|
686
|
+
*
|
|
687
|
+
* @remarks
|
|
688
|
+
* - If the app is running within a third-party application that uses
|
|
689
|
+
* the Monterosa SDK, the function delegates to the parent app
|
|
690
|
+
* to set user credentials.
|
|
691
|
+
*
|
|
692
|
+
* - If the request is successful, the function resolves to `void`.
|
|
693
|
+
* If not, a `MonterosaError` is thrown.
|
|
694
|
+
*
|
|
695
|
+
* @param identify - An instance of `IdentifyKit` that provides the user
|
|
696
|
+
* identification functionality.
|
|
697
|
+
* @param credentials - An object representing the user's authentication
|
|
698
|
+
* credentials.
|
|
699
|
+
* @returns A Promise that resolves to `void`.
|
|
700
|
+
*/
|
|
701
|
+
async function setCredentials(identify, credentials) {
|
|
702
|
+
const parentApp = sdkLauncherKit.getParentApplication();
|
|
703
|
+
if (parentApp !== null) {
|
|
704
|
+
await parentAppRequest(identify, IdentifyAction.SetCredentials, credentials);
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
const shouldLogin = identify.credentials === null;
|
|
708
|
+
identify.credentials = credentials;
|
|
709
|
+
if (shouldLogin) {
|
|
710
|
+
enqueueLogin(identify, credentials);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Clears the user's authentication credentials.
|
|
715
|
+
*
|
|
716
|
+
* @example
|
|
717
|
+
* ```javascript
|
|
718
|
+
* import { configure } from '@monterosa/sdk-core';
|
|
719
|
+
* import { getIdentify, clearCredentials } from '@monterosa/sdk-identify-kit';
|
|
720
|
+
*
|
|
721
|
+
* configure({ host: '...', projectId: '...' });
|
|
722
|
+
*
|
|
723
|
+
* const identify = getIdentify();
|
|
724
|
+
*
|
|
725
|
+
* await clearCredentials(identify);
|
|
726
|
+
* ```
|
|
727
|
+
*
|
|
728
|
+
* @remarks
|
|
729
|
+
* - If the app is running within a third-party application that uses
|
|
730
|
+
* the Monterosa SDK, the function delegates to the parent app
|
|
731
|
+
* to clear user credentials.
|
|
732
|
+
*
|
|
733
|
+
* - If the request is successful, the function resolves to `void`.
|
|
734
|
+
* If not, a `MonterosaError` is thrown.
|
|
735
|
+
*
|
|
736
|
+
* @param identify - An instance of `IdentifyKit` that provides the user
|
|
737
|
+
* identification functionality.
|
|
738
|
+
* @returns A Promise that resolves to `void`.
|
|
739
|
+
*/
|
|
740
|
+
async function clearCredentials(identify) {
|
|
741
|
+
const parentApp = sdkLauncherKit.getParentApplication();
|
|
742
|
+
if (parentApp !== null) {
|
|
743
|
+
await parentAppRequest(identify, IdentifyAction.ClearCredentials);
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
746
|
+
const shouldLogout = identify.credentials !== null;
|
|
747
|
+
identify.credentials = null;
|
|
748
|
+
identify.userData = null;
|
|
749
|
+
identify.signature = null;
|
|
750
|
+
if (shouldLogout) {
|
|
751
|
+
enqueueLogout(identify);
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
/**
|
|
755
|
+
* Registers a callback function that will be called whenever the `LoginState`
|
|
756
|
+
* object associated with the `IdentifyKit` instance is updated.
|
|
757
|
+
*
|
|
758
|
+
* @example
|
|
759
|
+
* ```javascript
|
|
760
|
+
* import { configure } from '@monterosa/sdk-core';
|
|
761
|
+
* import { getIdentify, onStateUpdated } from '@monterosa/sdk-identify-kit';
|
|
762
|
+
*
|
|
763
|
+
* configure({ host: '...', projectId: '...' });
|
|
764
|
+
*
|
|
765
|
+
* const identify = getIdentify();
|
|
766
|
+
*
|
|
767
|
+
* const unsubscribe = onStateUpdated(identify, (state) => {
|
|
768
|
+
* console.log("State updated:", state);
|
|
769
|
+
* });
|
|
770
|
+
*
|
|
771
|
+
* // Call unsubscribe() to unregister the callback function
|
|
772
|
+
* // unsubscribe();
|
|
773
|
+
* ```
|
|
774
|
+
*
|
|
775
|
+
* @param identify - An instance of `IdentifyKit` that provides the user
|
|
776
|
+
* identification functionality.
|
|
777
|
+
* @param callback - The callback function to register. This function will be
|
|
778
|
+
* called with the updated `LoginState` object as its only argument.
|
|
779
|
+
* @returns An `Unsubscribe` function that can be called to unregister
|
|
780
|
+
* the callback function.
|
|
781
|
+
*/
|
|
782
|
+
function onStateUpdated(identify, callback) {
|
|
783
|
+
return sdkUtil.subscribe(identify, IdentifyEvent.StateUpdated, callback);
|
|
784
|
+
}
|
|
785
|
+
/**
|
|
786
|
+
* Registers a callback function that will be called whenever the `Credentials`
|
|
787
|
+
* object associated with the `IdentifyKit` instance is updated.
|
|
788
|
+
*
|
|
789
|
+
* @example
|
|
790
|
+
* ```javascript
|
|
791
|
+
* import { configure } from '@monterosa/sdk-core';
|
|
792
|
+
* import { getIdentify, onCredentialsUpdated } from '@monterosa/sdk-identify-kit';
|
|
793
|
+
*
|
|
794
|
+
* configure({ host: '...', projectId: '...' });
|
|
795
|
+
*
|
|
796
|
+
* const identify = getIdentify();
|
|
797
|
+
*
|
|
798
|
+
* const unsubscribe = onCredentialsUpdated(identify, (credentials) => {
|
|
799
|
+
* if (credentials !== null) {
|
|
800
|
+
* console.log("Credentials updated:", credentials);
|
|
801
|
+
* } else {
|
|
802
|
+
* console.log("Credentials cleared.");
|
|
803
|
+
* }
|
|
804
|
+
* });
|
|
805
|
+
*
|
|
806
|
+
* // Call unsubscribe() to unregister the callback function
|
|
807
|
+
* // unsubscribe();
|
|
808
|
+
* ```
|
|
809
|
+
*
|
|
810
|
+
* @param identify - An instance of `IdentifyKit` that provides the user
|
|
811
|
+
* identification functionality.
|
|
812
|
+
* @param callback - The callback function to register. This function will be
|
|
813
|
+
* called with the updated `Credentials` object as its only argument.
|
|
814
|
+
* If the value `null` is received, it indicates that the signature has
|
|
815
|
+
* been cleared
|
|
816
|
+
* @returns An `Unsubscribe` function that can be called to unregister
|
|
817
|
+
* the callback function.
|
|
818
|
+
*/
|
|
819
|
+
function onCredentialsUpdated(identify, callback) {
|
|
820
|
+
return sdkUtil.subscribe(identify, IdentifyEvent.CredentialsUpdated, callback);
|
|
821
|
+
}
|
|
822
|
+
/**
|
|
823
|
+
* Registers a callback function that will be called whenever the `Signature`
|
|
824
|
+
* object associated with the `IdentifyKit` instance is updated.
|
|
825
|
+
*
|
|
826
|
+
* @example
|
|
827
|
+
* ```javascript
|
|
828
|
+
* import { configure } from '@monterosa/sdk-core';
|
|
829
|
+
* import { getIdentify, onSignatureUpdated } from '@monterosa/sdk-identify-kit';
|
|
830
|
+
*
|
|
831
|
+
* configure({ host: '...', projectId: '...' });
|
|
832
|
+
*
|
|
833
|
+
* const identify = getIdentify();
|
|
834
|
+
*
|
|
835
|
+
* const unsubscribe = onSignatureUpdated(identify, (signature) => {
|
|
836
|
+
* if (signature !== null) {
|
|
837
|
+
* console.log("Signature updated:", signature);
|
|
838
|
+
* } else {
|
|
839
|
+
* console.log("Signature cleared.");
|
|
840
|
+
* }
|
|
841
|
+
* });
|
|
842
|
+
*
|
|
843
|
+
* // Call unsubscribe() to unregister the callback function
|
|
844
|
+
* // unsubscribe();
|
|
845
|
+
* ```
|
|
846
|
+
*
|
|
847
|
+
* @param identify - An instance of `IdentifyKit` that provides the user
|
|
848
|
+
* identification functionality.
|
|
849
|
+
* @param callback - The callback function to register. This function will be
|
|
850
|
+
* called with the updated `Signature` object as its only argument.
|
|
851
|
+
* If the value `null` is received, it indicates that the signature has
|
|
852
|
+
* been cleared
|
|
853
|
+
* @returns An `Unsubscribe` function that can be called to unregister
|
|
854
|
+
* the callback function.
|
|
855
|
+
*/
|
|
856
|
+
function onSignatureUpdated(identify, callback) {
|
|
857
|
+
return sdkUtil.subscribe(identify, IdentifyEvent.SignatureUpdated, callback);
|
|
858
|
+
}
|
|
859
|
+
/**
|
|
860
|
+
* Registers a callback function that will be called whenever the `UserData`
|
|
861
|
+
* object associated with the `IdentifyKit` instance is updated.
|
|
862
|
+
*
|
|
863
|
+
* @example
|
|
864
|
+
* ```javascript
|
|
865
|
+
* import { configure } from '@monterosa/sdk-core';
|
|
866
|
+
* import { getIdentify, onUserDataUpdated } from '@monterosa/sdk-identify-kit';
|
|
867
|
+
*
|
|
868
|
+
* configure({ host: '...', projectId: '...' });
|
|
869
|
+
*
|
|
870
|
+
* const identify = getIdentify();
|
|
871
|
+
*
|
|
872
|
+
* const unsubscribe = onUserDataUpdated(identify, (userData) => {
|
|
873
|
+
* if (userData !== null) {
|
|
874
|
+
* console.log("User's data updated:", userData);
|
|
875
|
+
* } else {
|
|
876
|
+
* console.log("User's data cleared.");
|
|
877
|
+
* }
|
|
878
|
+
* });
|
|
879
|
+
*
|
|
880
|
+
* // Call unsubscribe() to unregister the callback function
|
|
881
|
+
* // unsubscribe();
|
|
882
|
+
* ```
|
|
883
|
+
*
|
|
884
|
+
* @param identify - An instance of `IdentifyKit` that provides the user
|
|
885
|
+
* identification functionality.
|
|
886
|
+
* @param callback - The callback function to register. This function will be
|
|
887
|
+
* called with the updated `UserData` object as its only argument.
|
|
888
|
+
* If the value `null` is received, it indicates that the user's data has
|
|
889
|
+
* been cleared
|
|
890
|
+
* @returns An `Unsubscribe` function that can be called to unregister
|
|
891
|
+
* the callback function.
|
|
892
|
+
*/
|
|
893
|
+
function onUserDataUpdated(identify, callback) {
|
|
894
|
+
return sdkUtil.subscribe(identify, IdentifyEvent.UserdataUpdated, callback);
|
|
895
|
+
}
|
|
896
|
+
/**
|
|
897
|
+
* Registers a callback function that will be called when a login is requested
|
|
898
|
+
* by an Experience.
|
|
899
|
+
*
|
|
900
|
+
* @example
|
|
901
|
+
* ```javascript
|
|
902
|
+
* import { configure } from '@monterosa/sdk-core';
|
|
903
|
+
* import { getIdentify, onLoginRequestedByExperience } from '@monterosa/sdk-identify-kit';
|
|
904
|
+
*
|
|
905
|
+
* configure({ host: '...', projectId: '...' });
|
|
906
|
+
*
|
|
907
|
+
* const identify = getIdentify();
|
|
908
|
+
*
|
|
909
|
+
* const unsubscribe = onLoginRequestedByExperience(identify, () => {
|
|
910
|
+
* showLoginForm();
|
|
911
|
+
* });
|
|
912
|
+
*
|
|
913
|
+
* // Call unsubscribe() to unregister the callback function
|
|
914
|
+
* // unsubscribe();
|
|
915
|
+
* ```
|
|
916
|
+
*
|
|
917
|
+
* @param identify - An instance of `IdentifyKit` that provides the user
|
|
918
|
+
* identification functionality.
|
|
919
|
+
* @param callback - The callback function to register. This function will
|
|
920
|
+
* be called when a login is requested by an Experience.
|
|
921
|
+
* @returns An `Unsubscribe` function that can be called to unregister
|
|
922
|
+
* the callback function.
|
|
923
|
+
*/
|
|
924
|
+
function onLoginRequestedByExperience(identify, callback) {
|
|
925
|
+
return sdkUtil.subscribe(identify, IdentifyEvent.LoginRequested, callback);
|
|
926
|
+
}
|
|
927
|
+
/**
|
|
928
|
+
* Registers a callback function that will be called when a logout is requested
|
|
929
|
+
* by an Experience.
|
|
930
|
+
*
|
|
931
|
+
* @example
|
|
932
|
+
* ```javascript
|
|
933
|
+
* import { getSpace } from '@monterosa/sdk-core';
|
|
934
|
+
* import { getIdentify, onLogoutRequestedByExperience } from '@monterosa/sdk-identify-kit';
|
|
935
|
+
*
|
|
936
|
+
* const space = getSpace('host', 'space-id');
|
|
937
|
+
* const identify = getIdentify(space);
|
|
938
|
+
*
|
|
939
|
+
* const unsubscribe = onLogoutRequestedByExperience(identify, () => {
|
|
940
|
+
* // logout requested by experience
|
|
941
|
+
* // perform logout from Auth service
|
|
942
|
+
* });
|
|
943
|
+
*
|
|
944
|
+
* // Call unsubscribe() to unregister the callback function
|
|
945
|
+
* // unsubscribe();
|
|
946
|
+
* ```
|
|
947
|
+
*
|
|
948
|
+
* @param identify - An instance of `IdentifyKit` that provides the user
|
|
949
|
+
* identification functionality.
|
|
950
|
+
* @param callback - The callback function to register. This function will
|
|
951
|
+
* be called when a logout is requested by an Experience.
|
|
952
|
+
* @returns An `Unsubscribe` function that can be called to unregister
|
|
953
|
+
* the callback function.
|
|
954
|
+
*/
|
|
955
|
+
function onLogoutRequestedByExperience(identify, callback) {
|
|
956
|
+
return sdkUtil.subscribe(identify, IdentifyEvent.LogoutRequested, callback);
|
|
957
|
+
}
|
|
958
|
+
/**
|
|
959
|
+
*
|
|
960
|
+
* @internal
|
|
961
|
+
*/
|
|
962
|
+
async function watchConnectionStatus(identify) {
|
|
963
|
+
const conn = await sdkConnectKit.getConnect(identify.sdk.options.host);
|
|
964
|
+
sdkConnectKit.onConnected(conn, () => {
|
|
965
|
+
if (identify.shouldLoginOnReconnect) {
|
|
966
|
+
enqueueLogin(identify, identify.credentials, true);
|
|
967
|
+
}
|
|
968
|
+
taskQueue.resume();
|
|
969
|
+
});
|
|
970
|
+
sdkConnectKit.onConnecting(conn, () => {
|
|
971
|
+
taskQueue.pause();
|
|
972
|
+
// If the user was logged in before the connection loss, set the state
|
|
973
|
+
// to pending to notify that the user is no longer logged in and a login
|
|
974
|
+
// attempt will be made when the connection is restored.
|
|
975
|
+
if (identify.state.state === 'logged_in') {
|
|
976
|
+
identify.state = {
|
|
977
|
+
state: 'pending',
|
|
978
|
+
};
|
|
979
|
+
}
|
|
980
|
+
});
|
|
981
|
+
sdkConnectKit.onDisconnected(conn, () => {
|
|
982
|
+
taskQueue.pause();
|
|
983
|
+
taskQueue.clear();
|
|
984
|
+
// If the user was logged in before the connection loss, set the state
|
|
985
|
+
// to logged_out to notify that the user is no longer logged in.
|
|
986
|
+
if (identify.state.state === 'logged_in') {
|
|
987
|
+
identify.state = {
|
|
988
|
+
state: 'logged_out',
|
|
989
|
+
};
|
|
990
|
+
}
|
|
991
|
+
});
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
/**
|
|
995
|
+
* @license
|
|
996
|
+
* @monterosa/sdk-identify-kit
|
|
997
|
+
*
|
|
998
|
+
* Copyright © 2023-2026 Monterosa Productions Limited. All rights reserved.
|
|
999
|
+
*
|
|
1000
|
+
* More details on the license can be found at https://www.monterosa.co/sdk/license
|
|
1001
|
+
*/
|
|
1002
|
+
/**
|
|
1003
|
+
* @internal
|
|
1004
|
+
*/
|
|
1005
|
+
function parentMessagesHook(identify) {
|
|
1006
|
+
const parentApp = sdkLauncherKit.getParentApplication();
|
|
1007
|
+
if (parentApp === null) {
|
|
1008
|
+
return () => { };
|
|
1009
|
+
}
|
|
1010
|
+
return sdkLauncherKit.onSdkMessage(parentApp, async (message) => {
|
|
1011
|
+
switch (message.action) {
|
|
1012
|
+
case IdentifyAction.OnCredentialsUpdated: {
|
|
1013
|
+
identify.credentials = message.payload.data;
|
|
1014
|
+
break;
|
|
1015
|
+
}
|
|
1016
|
+
case IdentifyAction.OnSessionSignatureUpdated: {
|
|
1017
|
+
identify.signature = message.payload.data;
|
|
1018
|
+
break;
|
|
1019
|
+
}
|
|
1020
|
+
case IdentifyAction.OnUserDataUpdated: {
|
|
1021
|
+
identify.userData = message.payload.data;
|
|
1022
|
+
break;
|
|
1023
|
+
}
|
|
1024
|
+
case IdentifyAction.Login: {
|
|
1025
|
+
enqueueLogin(identify, message.payload);
|
|
1026
|
+
break;
|
|
1027
|
+
}
|
|
1028
|
+
case IdentifyAction.Logout: {
|
|
1029
|
+
enqueueLogout(identify);
|
|
1030
|
+
break;
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
});
|
|
1034
|
+
}
|
|
1035
|
+
/**
|
|
1036
|
+
* Finds an existing SDK with matching projectId, or creates a new one.
|
|
1037
|
+
*/
|
|
1038
|
+
function getOrConfigureSdk(host, projectId) {
|
|
1039
|
+
const existingSdk = sdkCore.getSdks().find((sdk) => sdk.options.projectId === projectId);
|
|
1040
|
+
return existingSdk !== null && existingSdk !== void 0 ? existingSdk : sdkCore.configure({ host, projectId }, projectId);
|
|
1041
|
+
}
|
|
1042
|
+
/**
|
|
1043
|
+
* @internal
|
|
1044
|
+
*/
|
|
1045
|
+
function handleExperienceEmbedded(experience) {
|
|
1046
|
+
const identify = getIdentify(experience.sdk);
|
|
1047
|
+
// Send login action with current credentials to the new experience if available.
|
|
1048
|
+
// The bridge queues this message until the experience is initialised.
|
|
1049
|
+
if (identify.credentials) {
|
|
1050
|
+
sdkLauncherKit.sendSdkMessage(experience, IdentifyAction.Login, identify.credentials);
|
|
1051
|
+
}
|
|
1052
|
+
const credentialsUpdatedUnsub = onCredentialsUpdated(identify, (data) => {
|
|
1053
|
+
sdkLauncherKit.sendSdkMessage(experience, IdentifyAction.OnCredentialsUpdated, {
|
|
1054
|
+
data,
|
|
1055
|
+
});
|
|
1056
|
+
});
|
|
1057
|
+
const signatureUpdatedUnsub = onSignatureUpdated(identify, (data) => {
|
|
1058
|
+
sdkLauncherKit.sendSdkMessage(experience, IdentifyAction.OnSessionSignatureUpdated, {
|
|
1059
|
+
data,
|
|
1060
|
+
});
|
|
1061
|
+
});
|
|
1062
|
+
const userDataUpdatedUnsub = onUserDataUpdated(identify, (data) => {
|
|
1063
|
+
sdkLauncherKit.sendSdkMessage(experience, IdentifyAction.OnUserDataUpdated, {
|
|
1064
|
+
data,
|
|
1065
|
+
});
|
|
1066
|
+
});
|
|
1067
|
+
const sdkMessageUnsub = sdkLauncherKit.onSdkMessage(experience, async (message) => {
|
|
1068
|
+
if (!Object.values(IdentifyAction).includes(message.action)) {
|
|
1069
|
+
return;
|
|
1070
|
+
}
|
|
1071
|
+
// Extract origin context from payload if available.
|
|
1072
|
+
// New clients include origin (host, projectId) to ensure correct project context
|
|
1073
|
+
// in multilevel integrations. Old clients don't send origin, so we fall back
|
|
1074
|
+
// to the experience's SDK to maintain backward compatibility.
|
|
1075
|
+
const { origin } = message.payload;
|
|
1076
|
+
const originSdk = origin
|
|
1077
|
+
? getOrConfigureSdk(origin.host, origin.projectId)
|
|
1078
|
+
: experience.sdk;
|
|
1079
|
+
const originIdentify = getIdentify(originSdk);
|
|
1080
|
+
const respond = (payload) => sdkLauncherKit.respondToSdkMessage(experience, message, payload);
|
|
1081
|
+
try {
|
|
1082
|
+
switch (message.action) {
|
|
1083
|
+
case IdentifyAction.RequestLogin: {
|
|
1084
|
+
await requestLogin(originIdentify);
|
|
1085
|
+
respond();
|
|
1086
|
+
break;
|
|
1087
|
+
}
|
|
1088
|
+
case IdentifyAction.RequestLogout: {
|
|
1089
|
+
await requestLogout(identify);
|
|
1090
|
+
respond();
|
|
1091
|
+
break;
|
|
1092
|
+
}
|
|
1093
|
+
case IdentifyAction.GetUserData: {
|
|
1094
|
+
const data = await getUserData(identify);
|
|
1095
|
+
respond({ data });
|
|
1096
|
+
break;
|
|
1097
|
+
}
|
|
1098
|
+
case IdentifyAction.GetSessionSignature: {
|
|
1099
|
+
const signature = await getSessionSignatureMemoized(identify, message.payload);
|
|
1100
|
+
respond({ data: signature });
|
|
1101
|
+
break;
|
|
1102
|
+
}
|
|
1103
|
+
case IdentifyAction.SetCredentials: {
|
|
1104
|
+
await setCredentials(originIdentify, {
|
|
1105
|
+
token: message.payload.token,
|
|
1106
|
+
});
|
|
1107
|
+
respond();
|
|
1108
|
+
break;
|
|
1109
|
+
}
|
|
1110
|
+
case IdentifyAction.ClearCredentials: {
|
|
1111
|
+
await clearCredentials(identify);
|
|
1112
|
+
respond();
|
|
1113
|
+
break;
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
catch (err) {
|
|
1118
|
+
respond({
|
|
1119
|
+
error: sdkUtil.getErrorMessage(err),
|
|
1120
|
+
});
|
|
1121
|
+
}
|
|
1122
|
+
});
|
|
1123
|
+
addExperience(experience, [
|
|
1124
|
+
credentialsUpdatedUnsub,
|
|
1125
|
+
signatureUpdatedUnsub,
|
|
1126
|
+
userDataUpdatedUnsub,
|
|
1127
|
+
sdkMessageUnsub,
|
|
1128
|
+
]);
|
|
1129
|
+
}
|
|
1130
|
+
/**
|
|
1131
|
+
* @internal
|
|
1132
|
+
*/
|
|
1133
|
+
function handleExperienceUnmounted(experience) {
|
|
1134
|
+
deleteExperience(experience);
|
|
1135
|
+
}
|
|
1136
|
+
sdkLauncherKit.onStateChanged((experience, state) => {
|
|
1137
|
+
if (state === 'mounted') {
|
|
1138
|
+
handleExperienceEmbedded(experience);
|
|
1139
|
+
}
|
|
1140
|
+
else if (state === 'unmounted') {
|
|
1141
|
+
handleExperienceUnmounted(experience);
|
|
1142
|
+
}
|
|
1143
|
+
});
|
|
1144
|
+
registerIdentifyHook(parentMessagesHook);
|
|
1145
|
+
|
|
1146
|
+
exports.clearCredentials = clearCredentials;
|
|
1147
|
+
exports.getIdentify = getIdentify;
|
|
1148
|
+
exports.getUserData = getUserData;
|
|
1149
|
+
exports.onCredentialsUpdated = onCredentialsUpdated;
|
|
1150
|
+
exports.onLoginRequestedByExperience = onLoginRequestedByExperience;
|
|
1151
|
+
exports.onLogoutRequestedByExperience = onLogoutRequestedByExperience;
|
|
1152
|
+
exports.onSignatureUpdated = onSignatureUpdated;
|
|
1153
|
+
exports.onStateUpdated = onStateUpdated;
|
|
1154
|
+
exports.onUserDataUpdated = onUserDataUpdated;
|
|
1155
|
+
exports.requestLogin = requestLogin;
|
|
1156
|
+
exports.requestLogout = requestLogout;
|
|
1157
|
+
exports.setCredentials = setCredentials;
|
|
1158
|
+
//# sourceMappingURL=index.cjs.map
|