@saasquatch/component-environment 1.0.8 → 1.0.9-0
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/index.d.ts +15 -1
- package/dist/index.js +56 -0
- package/dist/index.mjs +51 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -7,20 +7,27 @@ declare global {
|
|
|
7
7
|
squatchUserIdentity?: ContextProvider<UserIdentity | undefined>;
|
|
8
8
|
squatchLocale?: ContextProvider<string | undefined>;
|
|
9
9
|
squatchProgramId?: ContextProvider<string | undefined>;
|
|
10
|
+
squatchVerification?: ContextProvider<VerificationContext | undefined>;
|
|
10
11
|
}
|
|
11
12
|
}
|
|
12
13
|
declare type UserContextName = "sq:user-identity";
|
|
13
14
|
declare type LocaleContextName = "sq:locale";
|
|
14
15
|
declare type ProgramContextName = "sq:program-id";
|
|
16
|
+
declare type VerificationContextName = "sq:verification";
|
|
15
17
|
declare const USER_CONTEXT_NAME: UserContextName;
|
|
16
18
|
declare const LOCALE_CONTEXT_NAME: LocaleContextName;
|
|
17
19
|
declare const PROGRAM_CONTEXT_NAME: ProgramContextName;
|
|
20
|
+
declare const VERIFICATION_CONTEXT_NAME: VerificationContextName;
|
|
21
|
+
declare type VerificationContext = {
|
|
22
|
+
token: string;
|
|
23
|
+
};
|
|
18
24
|
/**
|
|
19
25
|
* The value stored in the UserContext
|
|
20
26
|
*/
|
|
21
27
|
declare type UserIdentity = {
|
|
22
28
|
id: string;
|
|
23
29
|
accountId: string;
|
|
30
|
+
email?: string;
|
|
24
31
|
jwt?: string;
|
|
25
32
|
managedIdentity?: {
|
|
26
33
|
email: string;
|
|
@@ -39,6 +46,7 @@ interface DecodedSquatchJWT {
|
|
|
39
46
|
user: {
|
|
40
47
|
accountId: string;
|
|
41
48
|
id: string;
|
|
49
|
+
email?: string;
|
|
42
50
|
};
|
|
43
51
|
}
|
|
44
52
|
interface DecodedWidgetAPIJWT {
|
|
@@ -59,6 +67,7 @@ interface WidgetIdent {
|
|
|
59
67
|
token: string;
|
|
60
68
|
userId: string;
|
|
61
69
|
accountId: string;
|
|
70
|
+
email?: string;
|
|
62
71
|
locale?: string;
|
|
63
72
|
engagementMedium?: "POPUP" | "EMBED";
|
|
64
73
|
programId?: string;
|
|
@@ -293,4 +302,9 @@ declare function setProgramId(programId: string | undefined): void;
|
|
|
293
302
|
*/
|
|
294
303
|
declare function getProgramId(): string | undefined;
|
|
295
304
|
|
|
296
|
-
|
|
305
|
+
declare function lazilyStartVerificationContext(): ContextProvider<VerificationContext | undefined>;
|
|
306
|
+
declare function checkVerificationToken(token: string): boolean;
|
|
307
|
+
declare function setVerificationContext(context: VerificationContext): void;
|
|
308
|
+
declare function getVerificationContext(): VerificationContext | undefined;
|
|
309
|
+
|
|
310
|
+
export { BrowserHistory, DEFAULT_MEDIUM, DecodedSquatchJWT, DecodedWidgetAPIJWT, EngagementMedium, Environment, EnvironmentSDK, HistoryEntry, LOCALE_CONTEXT_NAME, LazyHistory, LocaleContextName, MemoryHistory, PROGRAM_CONTEXT_NAME, PortalEnv, ProgramContextName, SquatchAdmin, SquatchAndroid, SquatchJS2, USER_CONTEXT_NAME, UserContextName, UserId, UserIdentity, VERIFICATION_CONTEXT_NAME, VerificationContext, VerificationContextName, WidgetIdent, checkVerificationToken, getAppDomain, getEngagementMedium, getEnvironment, getEnvironmentSDK, getLocale, getProgramId, getTenantAlias, getUserIdentity, getVerificationContext, _default as history, isDemo, lazilyStartLocaleContext, lazilyStartProgramContext, lazilyStartUserContext, lazilyStartVerificationContext, setLocale, setProgramId, setUserIdentity, setVerificationContext, userIdentityFromJwt, validateLocale };
|
package/dist/index.js
CHANGED
|
@@ -47,6 +47,8 @@ __export(src_exports, {
|
|
|
47
47
|
MemoryHistory: () => MemoryHistory,
|
|
48
48
|
PROGRAM_CONTEXT_NAME: () => PROGRAM_CONTEXT_NAME,
|
|
49
49
|
USER_CONTEXT_NAME: () => USER_CONTEXT_NAME,
|
|
50
|
+
VERIFICATION_CONTEXT_NAME: () => VERIFICATION_CONTEXT_NAME,
|
|
51
|
+
checkVerificationToken: () => checkVerificationToken,
|
|
50
52
|
getAppDomain: () => getAppDomain,
|
|
51
53
|
getEngagementMedium: () => getEngagementMedium,
|
|
52
54
|
getEnvironment: () => getEnvironment,
|
|
@@ -55,14 +57,17 @@ __export(src_exports, {
|
|
|
55
57
|
getProgramId: () => getProgramId,
|
|
56
58
|
getTenantAlias: () => getTenantAlias,
|
|
57
59
|
getUserIdentity: () => getUserIdentity,
|
|
60
|
+
getVerificationContext: () => getVerificationContext,
|
|
58
61
|
history: () => history_default,
|
|
59
62
|
isDemo: () => isDemo,
|
|
60
63
|
lazilyStartLocaleContext: () => lazilyStartLocaleContext,
|
|
61
64
|
lazilyStartProgramContext: () => lazilyStartProgramContext,
|
|
62
65
|
lazilyStartUserContext: () => lazilyStartUserContext,
|
|
66
|
+
lazilyStartVerificationContext: () => lazilyStartVerificationContext,
|
|
63
67
|
setLocale: () => setLocale,
|
|
64
68
|
setProgramId: () => setProgramId,
|
|
65
69
|
setUserIdentity: () => setUserIdentity,
|
|
70
|
+
setVerificationContext: () => setVerificationContext,
|
|
66
71
|
userIdentityFromJwt: () => userIdentityFromJwt,
|
|
67
72
|
validateLocale: () => validateLocale
|
|
68
73
|
});
|
|
@@ -72,6 +77,7 @@ module.exports = __toCommonJS(src_exports);
|
|
|
72
77
|
var USER_CONTEXT_NAME = "sq:user-identity";
|
|
73
78
|
var LOCALE_CONTEXT_NAME = "sq:locale";
|
|
74
79
|
var PROGRAM_CONTEXT_NAME = "sq:program-id";
|
|
80
|
+
var VERIFICATION_CONTEXT_NAME = "sq:verification";
|
|
75
81
|
var DEFAULT_MEDIUM = "EMBED";
|
|
76
82
|
|
|
77
83
|
// src/environment.ts
|
|
@@ -504,6 +510,7 @@ function userIdentityFromJwt(jwt) {
|
|
|
504
510
|
const exp = decoded.exp;
|
|
505
511
|
let userId = void 0;
|
|
506
512
|
let accountId = void 0;
|
|
513
|
+
let email = void 0;
|
|
507
514
|
if (isDecodedWidgetAPIJWT(decoded)) {
|
|
508
515
|
const matches = decoded.sub.match(/(.*):(.*)@(.*):users/);
|
|
509
516
|
if (matches == null ? void 0 : matches[1])
|
|
@@ -513,6 +520,7 @@ function userIdentityFromJwt(jwt) {
|
|
|
513
520
|
} else if (isDecodedSquatchJWT(decoded)) {
|
|
514
521
|
accountId = decoded.user.accountId;
|
|
515
522
|
userId = decoded.user.id;
|
|
523
|
+
email = decoded.user.email;
|
|
516
524
|
}
|
|
517
525
|
if (!userId || !accountId) {
|
|
518
526
|
debug6("No user information");
|
|
@@ -525,6 +533,7 @@ function userIdentityFromJwt(jwt) {
|
|
|
525
533
|
return {
|
|
526
534
|
id: userId,
|
|
527
535
|
accountId,
|
|
536
|
+
email,
|
|
528
537
|
jwt
|
|
529
538
|
};
|
|
530
539
|
} catch (e) {
|
|
@@ -546,6 +555,7 @@ function _getInitialValue() {
|
|
|
546
555
|
return {
|
|
547
556
|
id: sdk.widgetIdent.userId,
|
|
548
557
|
accountId: sdk.widgetIdent.accountId,
|
|
558
|
+
email: sdk.widgetIdent.email,
|
|
549
559
|
jwt: sdk.widgetIdent.token
|
|
550
560
|
};
|
|
551
561
|
case "SquatchPortal":
|
|
@@ -616,6 +626,47 @@ function getProgramId() {
|
|
|
616
626
|
var _a;
|
|
617
627
|
return (_a = window.squatchProgramId) == null ? void 0 : _a.context;
|
|
618
628
|
}
|
|
629
|
+
|
|
630
|
+
// src/contexts/VerificationContext.ts
|
|
631
|
+
var import_equality2 = __toESM(require("@wry/equality"));
|
|
632
|
+
var import_jwt_decode2 = __toESM(require("jwt-decode"));
|
|
633
|
+
var import_dom_context5 = require("dom-context");
|
|
634
|
+
var debug8 = (...args) => debug(VERIFICATION_CONTEXT_NAME, ...args);
|
|
635
|
+
function lazilyStartVerificationContext() {
|
|
636
|
+
let globalProvider = window.squatchVerification;
|
|
637
|
+
if (!globalProvider) {
|
|
638
|
+
globalProvider = new import_dom_context5.ContextProvider({
|
|
639
|
+
element: document.documentElement,
|
|
640
|
+
initialState: void 0,
|
|
641
|
+
contextName: VERIFICATION_CONTEXT_NAME
|
|
642
|
+
}).start();
|
|
643
|
+
window.squatchVerification = globalProvider;
|
|
644
|
+
}
|
|
645
|
+
return globalProvider;
|
|
646
|
+
}
|
|
647
|
+
function checkVerificationToken(token) {
|
|
648
|
+
if (!token)
|
|
649
|
+
return false;
|
|
650
|
+
const decoded = (0, import_jwt_decode2.default)(token);
|
|
651
|
+
const isSquatchJWT = decoded.user && decoded.user.id && decoded.user.accountId;
|
|
652
|
+
if (!isSquatchJWT)
|
|
653
|
+
return false;
|
|
654
|
+
const currentUser = getUserIdentity();
|
|
655
|
+
if (decoded.user.id !== (currentUser == null ? void 0 : currentUser.id) || decoded.user.accountId !== (currentUser == null ? void 0 : currentUser.accountId))
|
|
656
|
+
return false;
|
|
657
|
+
return true;
|
|
658
|
+
}
|
|
659
|
+
function setVerificationContext(context) {
|
|
660
|
+
const globalProvider = lazilyStartVerificationContext();
|
|
661
|
+
if (!(0, import_equality2.default)(globalProvider.context, context)) {
|
|
662
|
+
debug8(`Setting verification context to [${JSON.stringify(context)}]`);
|
|
663
|
+
globalProvider.context = context;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
function getVerificationContext() {
|
|
667
|
+
var _a;
|
|
668
|
+
return (_a = window.squatchVerification) == null ? void 0 : _a.context;
|
|
669
|
+
}
|
|
619
670
|
// Annotate the CommonJS export names for ESM import in node:
|
|
620
671
|
0 && (module.exports = {
|
|
621
672
|
BrowserHistory,
|
|
@@ -625,6 +676,8 @@ function getProgramId() {
|
|
|
625
676
|
MemoryHistory,
|
|
626
677
|
PROGRAM_CONTEXT_NAME,
|
|
627
678
|
USER_CONTEXT_NAME,
|
|
679
|
+
VERIFICATION_CONTEXT_NAME,
|
|
680
|
+
checkVerificationToken,
|
|
628
681
|
getAppDomain,
|
|
629
682
|
getEngagementMedium,
|
|
630
683
|
getEnvironment,
|
|
@@ -633,14 +686,17 @@ function getProgramId() {
|
|
|
633
686
|
getProgramId,
|
|
634
687
|
getTenantAlias,
|
|
635
688
|
getUserIdentity,
|
|
689
|
+
getVerificationContext,
|
|
636
690
|
history,
|
|
637
691
|
isDemo,
|
|
638
692
|
lazilyStartLocaleContext,
|
|
639
693
|
lazilyStartProgramContext,
|
|
640
694
|
lazilyStartUserContext,
|
|
695
|
+
lazilyStartVerificationContext,
|
|
641
696
|
setLocale,
|
|
642
697
|
setProgramId,
|
|
643
698
|
setUserIdentity,
|
|
699
|
+
setVerificationContext,
|
|
644
700
|
userIdentityFromJwt,
|
|
645
701
|
validateLocale
|
|
646
702
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -23,6 +23,7 @@ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
|
23
23
|
var USER_CONTEXT_NAME = "sq:user-identity";
|
|
24
24
|
var LOCALE_CONTEXT_NAME = "sq:locale";
|
|
25
25
|
var PROGRAM_CONTEXT_NAME = "sq:program-id";
|
|
26
|
+
var VERIFICATION_CONTEXT_NAME = "sq:verification";
|
|
26
27
|
var DEFAULT_MEDIUM = "EMBED";
|
|
27
28
|
|
|
28
29
|
// src/environment.ts
|
|
@@ -455,6 +456,7 @@ function userIdentityFromJwt(jwt) {
|
|
|
455
456
|
const exp = decoded.exp;
|
|
456
457
|
let userId = void 0;
|
|
457
458
|
let accountId = void 0;
|
|
459
|
+
let email = void 0;
|
|
458
460
|
if (isDecodedWidgetAPIJWT(decoded)) {
|
|
459
461
|
const matches = decoded.sub.match(/(.*):(.*)@(.*):users/);
|
|
460
462
|
if (matches == null ? void 0 : matches[1])
|
|
@@ -464,6 +466,7 @@ function userIdentityFromJwt(jwt) {
|
|
|
464
466
|
} else if (isDecodedSquatchJWT(decoded)) {
|
|
465
467
|
accountId = decoded.user.accountId;
|
|
466
468
|
userId = decoded.user.id;
|
|
469
|
+
email = decoded.user.email;
|
|
467
470
|
}
|
|
468
471
|
if (!userId || !accountId) {
|
|
469
472
|
debug6("No user information");
|
|
@@ -476,6 +479,7 @@ function userIdentityFromJwt(jwt) {
|
|
|
476
479
|
return {
|
|
477
480
|
id: userId,
|
|
478
481
|
accountId,
|
|
482
|
+
email,
|
|
479
483
|
jwt
|
|
480
484
|
};
|
|
481
485
|
} catch (e) {
|
|
@@ -497,6 +501,7 @@ function _getInitialValue() {
|
|
|
497
501
|
return {
|
|
498
502
|
id: sdk.widgetIdent.userId,
|
|
499
503
|
accountId: sdk.widgetIdent.accountId,
|
|
504
|
+
email: sdk.widgetIdent.email,
|
|
500
505
|
jwt: sdk.widgetIdent.token
|
|
501
506
|
};
|
|
502
507
|
case "SquatchPortal":
|
|
@@ -567,6 +572,47 @@ function getProgramId() {
|
|
|
567
572
|
var _a;
|
|
568
573
|
return (_a = window.squatchProgramId) == null ? void 0 : _a.context;
|
|
569
574
|
}
|
|
575
|
+
|
|
576
|
+
// src/contexts/VerificationContext.ts
|
|
577
|
+
import equal2 from "@wry/equality";
|
|
578
|
+
import decode2 from "jwt-decode";
|
|
579
|
+
import { ContextProvider as ContextProvider4 } from "dom-context";
|
|
580
|
+
var debug8 = (...args) => debug(VERIFICATION_CONTEXT_NAME, ...args);
|
|
581
|
+
function lazilyStartVerificationContext() {
|
|
582
|
+
let globalProvider = window.squatchVerification;
|
|
583
|
+
if (!globalProvider) {
|
|
584
|
+
globalProvider = new ContextProvider4({
|
|
585
|
+
element: document.documentElement,
|
|
586
|
+
initialState: void 0,
|
|
587
|
+
contextName: VERIFICATION_CONTEXT_NAME
|
|
588
|
+
}).start();
|
|
589
|
+
window.squatchVerification = globalProvider;
|
|
590
|
+
}
|
|
591
|
+
return globalProvider;
|
|
592
|
+
}
|
|
593
|
+
function checkVerificationToken(token) {
|
|
594
|
+
if (!token)
|
|
595
|
+
return false;
|
|
596
|
+
const decoded = decode2(token);
|
|
597
|
+
const isSquatchJWT = decoded.user && decoded.user.id && decoded.user.accountId;
|
|
598
|
+
if (!isSquatchJWT)
|
|
599
|
+
return false;
|
|
600
|
+
const currentUser = getUserIdentity();
|
|
601
|
+
if (decoded.user.id !== (currentUser == null ? void 0 : currentUser.id) || decoded.user.accountId !== (currentUser == null ? void 0 : currentUser.accountId))
|
|
602
|
+
return false;
|
|
603
|
+
return true;
|
|
604
|
+
}
|
|
605
|
+
function setVerificationContext(context) {
|
|
606
|
+
const globalProvider = lazilyStartVerificationContext();
|
|
607
|
+
if (!equal2(globalProvider.context, context)) {
|
|
608
|
+
debug8(`Setting verification context to [${JSON.stringify(context)}]`);
|
|
609
|
+
globalProvider.context = context;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
function getVerificationContext() {
|
|
613
|
+
var _a;
|
|
614
|
+
return (_a = window.squatchVerification) == null ? void 0 : _a.context;
|
|
615
|
+
}
|
|
570
616
|
export {
|
|
571
617
|
BrowserHistory,
|
|
572
618
|
DEFAULT_MEDIUM,
|
|
@@ -575,6 +621,8 @@ export {
|
|
|
575
621
|
MemoryHistory,
|
|
576
622
|
PROGRAM_CONTEXT_NAME,
|
|
577
623
|
USER_CONTEXT_NAME,
|
|
624
|
+
VERIFICATION_CONTEXT_NAME,
|
|
625
|
+
checkVerificationToken,
|
|
578
626
|
getAppDomain,
|
|
579
627
|
getEngagementMedium,
|
|
580
628
|
getEnvironment,
|
|
@@ -583,14 +631,17 @@ export {
|
|
|
583
631
|
getProgramId,
|
|
584
632
|
getTenantAlias,
|
|
585
633
|
getUserIdentity,
|
|
634
|
+
getVerificationContext,
|
|
586
635
|
history_default as history,
|
|
587
636
|
isDemo,
|
|
588
637
|
lazilyStartLocaleContext,
|
|
589
638
|
lazilyStartProgramContext,
|
|
590
639
|
lazilyStartUserContext,
|
|
640
|
+
lazilyStartVerificationContext,
|
|
591
641
|
setLocale,
|
|
592
642
|
setProgramId,
|
|
593
643
|
setUserIdentity,
|
|
644
|
+
setVerificationContext,
|
|
594
645
|
userIdentityFromJwt,
|
|
595
646
|
validateLocale
|
|
596
647
|
};
|