@saasquatch/component-environment 1.0.0-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/LocaleContext.d.ts +7 -0
- package/dist/LocaleContext.js +43 -0
- package/dist/ProgramContext.d.ts +13 -0
- package/dist/ProgramContext.js +29 -0
- package/dist/UserIdentityContext.d.ts +10 -0
- package/dist/UserIdentityContext.js +124 -0
- package/dist/contexts/LocaleContext.d.ts +18 -0
- package/dist/contexts/LocaleContext.js +41 -0
- package/dist/contexts/ProgramContext.d.ts +18 -0
- package/dist/contexts/ProgramContext.js +42 -0
- package/dist/contexts/UserIdentityContext.d.ts +26 -0
- package/dist/contexts/UserIdentityContext.js +146 -0
- package/dist/debug.d.ts +1 -0
- package/dist/debug.js +6 -0
- package/dist/environment.d.ts +15 -0
- package/dist/environment.js +98 -0
- package/dist/fetchLocale.d.ts +1 -0
- package/dist/fetchLocale.js +37 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/listeners.d.ts +1 -0
- package/dist/listeners.js +44 -0
- package/dist/types.d.ts +147 -0
- package/dist/types.js +4 -0
- package/package.json +28 -0
- package/src/contexts/LocaleContext.ts +50 -0
- package/src/contexts/ProgramContext.ts +50 -0
- package/src/contexts/UserIdentityContext.ts +164 -0
- package/src/debug.ts +7 -0
- package/src/environment.ts +114 -0
- package/src/fetchLocale.ts +52 -0
- package/src/index.ts +5 -0
- package/src/listeners.ts +51 -0
- package/src/types.ts +173 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// const CONTEXT_NAME = "sq:locale";
|
|
2
|
+
// const GET_LOCALE = gql`
|
|
3
|
+
// query {
|
|
4
|
+
// viewer {
|
|
5
|
+
// ... on User {
|
|
6
|
+
// locale
|
|
7
|
+
// }
|
|
8
|
+
// }
|
|
9
|
+
// }
|
|
10
|
+
// `;
|
|
11
|
+
export function lazilyStartLocaleContext() {
|
|
12
|
+
// const globalProvider = window.squatchLocale;
|
|
13
|
+
// const user = useUserIdentity();
|
|
14
|
+
// useEffect(() => {
|
|
15
|
+
// // Clear locale if user is undefined
|
|
16
|
+
// if (!user && globalProvider) {
|
|
17
|
+
// return (globalProvider.context = undefined);
|
|
18
|
+
// }
|
|
19
|
+
// fetch({});
|
|
20
|
+
// }, [user]);
|
|
21
|
+
// const [fetch, { data }] = useLazyQuery(GET_LOCALE);
|
|
22
|
+
// const locale = data?.viewer?.locale;
|
|
23
|
+
// if (!globalProvider) {
|
|
24
|
+
// // Lazily creates a global provider
|
|
25
|
+
// window.squatchLocale = new ContextProvider<string>({
|
|
26
|
+
// element: document.documentElement,
|
|
27
|
+
// initialState: locale || window.widgetIdent?.locale || undefined,
|
|
28
|
+
// contextName: CONTEXT_NAME,
|
|
29
|
+
// }).start();
|
|
30
|
+
// } else if (locale !== globalProvider.context) {
|
|
31
|
+
// globalProvider.context = locale;
|
|
32
|
+
// }
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Overide the globally defined Locale context
|
|
36
|
+
*
|
|
37
|
+
* @param locale the new locale used by the user
|
|
38
|
+
*/
|
|
39
|
+
export function setLocale(locale) {
|
|
40
|
+
const globalProvider = window.squatchLocale;
|
|
41
|
+
if (globalProvider)
|
|
42
|
+
globalProvider.context = locale;
|
|
43
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ContextProvider } from "dom-context";
|
|
2
|
+
/**
|
|
3
|
+
* Lazily start the program context provider
|
|
4
|
+
*
|
|
5
|
+
* @returns The global program context provider
|
|
6
|
+
*/
|
|
7
|
+
export declare function lazilyStartProgramContext(): ContextProvider<string | undefined>;
|
|
8
|
+
/**
|
|
9
|
+
* Overide the globally defined Program ID context
|
|
10
|
+
*
|
|
11
|
+
* @param programId the new programID used by the user, or undefined if logged out
|
|
12
|
+
*/
|
|
13
|
+
export declare function setProgramId(programId: string | undefined): void;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ContextProvider } from "dom-context";
|
|
2
|
+
import { PROGRAM_CONTEXT_NAME } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Lazily start the program context provider
|
|
5
|
+
*
|
|
6
|
+
* @returns The global program context provider
|
|
7
|
+
*/
|
|
8
|
+
export function lazilyStartProgramContext() {
|
|
9
|
+
let globalProvider = window.squatchProgramId;
|
|
10
|
+
if (!globalProvider) {
|
|
11
|
+
// Lazily creates a global provider
|
|
12
|
+
globalProvider = new ContextProvider({
|
|
13
|
+
element: document.documentElement,
|
|
14
|
+
initialState: window.widgetIdent?.programId || undefined,
|
|
15
|
+
contextName: PROGRAM_CONTEXT_NAME,
|
|
16
|
+
}).start();
|
|
17
|
+
window.squatchProgramId = globalProvider;
|
|
18
|
+
}
|
|
19
|
+
return globalProvider;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Overide the globally defined Program ID context
|
|
23
|
+
*
|
|
24
|
+
* @param programId the new programID used by the user, or undefined if logged out
|
|
25
|
+
*/
|
|
26
|
+
export function setProgramId(programId) {
|
|
27
|
+
const globalProvider = lazilyStartProgramContext();
|
|
28
|
+
globalProvider.context = programId;
|
|
29
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ContextProvider } from "dom-context";
|
|
2
|
+
import { UserIdentity } from "./types";
|
|
3
|
+
export declare function lazilyStartUserContext(): ContextProvider<UserIdentity | undefined>;
|
|
4
|
+
export declare function userIdentityFromJwt(jwt?: string): UserIdentity | undefined;
|
|
5
|
+
/**
|
|
6
|
+
* Overide the globally defined user context, and persists the user identity in local storage
|
|
7
|
+
*
|
|
8
|
+
* @param identity the new identity of the user, or undefined if logged out
|
|
9
|
+
*/
|
|
10
|
+
export declare function setUserIdentity(identity?: UserIdentity): void;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import decode from "jwt-decode";
|
|
2
|
+
import { ContextProvider } from "dom-context";
|
|
3
|
+
import { equal } from "@wry/equality";
|
|
4
|
+
import { getEnvironmentSDK } from "./environment";
|
|
5
|
+
import { setLocale } from "./LocaleContext";
|
|
6
|
+
import { USER_CONTEXT_NAME, } from "./types";
|
|
7
|
+
export function lazilyStartUserContext() {
|
|
8
|
+
let globalProvider = window.squatchUserIdentity;
|
|
9
|
+
if (!globalProvider) {
|
|
10
|
+
// Lazily creates a global provider
|
|
11
|
+
globalProvider = new ContextProvider({
|
|
12
|
+
element: document.documentElement,
|
|
13
|
+
initialState: _getInitialValue(),
|
|
14
|
+
contextName: USER_CONTEXT_NAME,
|
|
15
|
+
}).start();
|
|
16
|
+
window.squatchUserIdentity = globalProvider;
|
|
17
|
+
}
|
|
18
|
+
return globalProvider;
|
|
19
|
+
}
|
|
20
|
+
function isDecodedSquatchJWT(decoded) {
|
|
21
|
+
return decoded.user && decoded.user.id && decoded.user.accountId;
|
|
22
|
+
}
|
|
23
|
+
function isDecodedWidgetAPIJWT(decoded) {
|
|
24
|
+
return decoded.sub && /.*:.*@.*:users/.test(decoded.sub);
|
|
25
|
+
}
|
|
26
|
+
export function userIdentityFromJwt(jwt) {
|
|
27
|
+
if (!jwt)
|
|
28
|
+
return undefined;
|
|
29
|
+
try {
|
|
30
|
+
const decoded = decode(jwt);
|
|
31
|
+
const exp = decoded.exp;
|
|
32
|
+
let userId = undefined;
|
|
33
|
+
let accountId = undefined;
|
|
34
|
+
if (isDecodedWidgetAPIJWT(decoded)) {
|
|
35
|
+
// Pull the accountId and userId from the subject and Base64-decode them
|
|
36
|
+
// NOTE: This is to support classic theme engine widget token generation
|
|
37
|
+
const matches = decoded.sub.match(/(.*):(.*)@(.*):users/);
|
|
38
|
+
if (matches?.[1])
|
|
39
|
+
accountId = atob(matches[1]);
|
|
40
|
+
if (matches?.[2])
|
|
41
|
+
userId = atob(matches[2]);
|
|
42
|
+
}
|
|
43
|
+
else if (isDecodedSquatchJWT(decoded)) {
|
|
44
|
+
accountId = decoded.user.accountId;
|
|
45
|
+
userId = decoded.user.id;
|
|
46
|
+
}
|
|
47
|
+
if (!userId || !accountId) {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
// Check if the JWT has expired
|
|
51
|
+
if (exp && Date.now() >= exp * 1000) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
id: userId,
|
|
56
|
+
accountId: accountId,
|
|
57
|
+
jwt,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
// Invalid JWT
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function _getInitialValue() {
|
|
66
|
+
const sdk = getEnvironmentSDK();
|
|
67
|
+
switch (sdk.type) {
|
|
68
|
+
case "SquatchAndroid":
|
|
69
|
+
case "SquatchJS2":
|
|
70
|
+
return {
|
|
71
|
+
id: sdk.widgetIdent.userId,
|
|
72
|
+
accountId: sdk.widgetIdent.accountId,
|
|
73
|
+
jwt: sdk.widgetIdent.token,
|
|
74
|
+
};
|
|
75
|
+
case "SquatchPortal":
|
|
76
|
+
// Portals can have the jwt provided as a URL parameter, so look for that first
|
|
77
|
+
const searchParams = new URLSearchParams(document.location.search);
|
|
78
|
+
if (searchParams.has("jwt")) {
|
|
79
|
+
return userIdentityFromJwt(searchParams.get("jwt"));
|
|
80
|
+
}
|
|
81
|
+
// Look for user identity in local storage
|
|
82
|
+
const stored = localStorage.getItem(USER_CONTEXT_NAME);
|
|
83
|
+
if (!stored)
|
|
84
|
+
return undefined;
|
|
85
|
+
try {
|
|
86
|
+
const potentialUserIdent = JSON.parse(stored);
|
|
87
|
+
const identity = userIdentityFromJwt(potentialUserIdent.jwt);
|
|
88
|
+
if (identity) {
|
|
89
|
+
return {
|
|
90
|
+
...potentialUserIdent,
|
|
91
|
+
...identity,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
catch (e) {
|
|
97
|
+
// Not valid JSON
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
case "SquatchAdmin":
|
|
101
|
+
case "None":
|
|
102
|
+
// Not logged in for admin portal / none default case
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Overide the globally defined user context, and persists the user identity in local storage
|
|
108
|
+
*
|
|
109
|
+
* @param identity the new identity of the user, or undefined if logged out
|
|
110
|
+
*/
|
|
111
|
+
export function setUserIdentity(identity) {
|
|
112
|
+
const globalProvider = lazilyStartUserContext();
|
|
113
|
+
if (!equal(globalProvider.context, identity)) {
|
|
114
|
+
setLocale(undefined);
|
|
115
|
+
globalProvider.context = identity;
|
|
116
|
+
}
|
|
117
|
+
// Portals store identity in local storage
|
|
118
|
+
if (identity && getEnvironmentSDK().type === "SquatchPortal") {
|
|
119
|
+
localStorage.setItem(USER_CONTEXT_NAME, JSON.stringify(identity));
|
|
120
|
+
}
|
|
121
|
+
else if (!identity) {
|
|
122
|
+
localStorage.removeItem(USER_CONTEXT_NAME);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ContextProvider } from "dom-context";
|
|
2
|
+
/**
|
|
3
|
+
* Lazily start the locale context provider. If it already exists, the existing provider is
|
|
4
|
+
* returned. This function is safe to call multiple times.
|
|
5
|
+
*
|
|
6
|
+
* @returns The global locale context provider
|
|
7
|
+
*/
|
|
8
|
+
export declare function lazilyStartLocaleContext(): ContextProvider<string | undefined>;
|
|
9
|
+
/**
|
|
10
|
+
* Overide the globally defined Locale context
|
|
11
|
+
*
|
|
12
|
+
* @param locale the new locale used by the user
|
|
13
|
+
*/
|
|
14
|
+
export declare function setLocale(locale?: string): void;
|
|
15
|
+
/**
|
|
16
|
+
* Get the current value of the locale context
|
|
17
|
+
*/
|
|
18
|
+
export declare function getLocale(): string | undefined;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ContextProvider } from "dom-context";
|
|
2
|
+
import { LOCALE_CONTEXT_NAME } from "../types";
|
|
3
|
+
import { debug as _debug } from "../debug";
|
|
4
|
+
const debug = (...args) => _debug(LOCALE_CONTEXT_NAME, ...args);
|
|
5
|
+
/**
|
|
6
|
+
* Lazily start the locale context provider. If it already exists, the existing provider is
|
|
7
|
+
* returned. This function is safe to call multiple times.
|
|
8
|
+
*
|
|
9
|
+
* @returns The global locale context provider
|
|
10
|
+
*/
|
|
11
|
+
export function lazilyStartLocaleContext() {
|
|
12
|
+
let globalProvider = window.squatchLocale;
|
|
13
|
+
if (!globalProvider) {
|
|
14
|
+
debug("Creating locale context provider");
|
|
15
|
+
globalProvider = new ContextProvider({
|
|
16
|
+
element: document.documentElement,
|
|
17
|
+
initialState: window.widgetIdent?.locale || navigator.language.replace("-", "_"),
|
|
18
|
+
contextName: LOCALE_CONTEXT_NAME,
|
|
19
|
+
}).start();
|
|
20
|
+
window.squatchLocale = globalProvider;
|
|
21
|
+
}
|
|
22
|
+
return globalProvider;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Overide the globally defined Locale context
|
|
26
|
+
*
|
|
27
|
+
* @param locale the new locale used by the user
|
|
28
|
+
*/
|
|
29
|
+
export function setLocale(locale) {
|
|
30
|
+
const globalProvider = lazilyStartLocaleContext();
|
|
31
|
+
if (globalProvider.context !== locale) {
|
|
32
|
+
debug(`Setting locale context value [${locale}]`);
|
|
33
|
+
globalProvider.context = locale;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Get the current value of the locale context
|
|
38
|
+
*/
|
|
39
|
+
export function getLocale() {
|
|
40
|
+
return window.squatchLocale?.context;
|
|
41
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ContextProvider } from "dom-context";
|
|
2
|
+
/**
|
|
3
|
+
* Lazily start the program context provider. If it already exists, the existing provider is
|
|
4
|
+
* returned. This function is safe to call multiple times.
|
|
5
|
+
*
|
|
6
|
+
* @returns The global program context provider
|
|
7
|
+
*/
|
|
8
|
+
export declare function lazilyStartProgramContext(): ContextProvider<string | undefined>;
|
|
9
|
+
/**
|
|
10
|
+
* Overide the globally defined Program ID context
|
|
11
|
+
*
|
|
12
|
+
* @param programId the new programID used by the user, or undefined if logged out
|
|
13
|
+
*/
|
|
14
|
+
export declare function setProgramId(programId: string | undefined): void;
|
|
15
|
+
/**
|
|
16
|
+
* Get the current value of the program context
|
|
17
|
+
*/
|
|
18
|
+
export declare function getProgramId(): string | undefined;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ContextProvider } from "dom-context";
|
|
2
|
+
import { PROGRAM_CONTEXT_NAME } from "../types";
|
|
3
|
+
import { debug as _debug } from "../debug";
|
|
4
|
+
const debug = (...args) => _debug(PROGRAM_CONTEXT_NAME, ...args);
|
|
5
|
+
/**
|
|
6
|
+
* Lazily start the program context provider. If it already exists, the existing provider is
|
|
7
|
+
* returned. This function is safe to call multiple times.
|
|
8
|
+
*
|
|
9
|
+
* @returns The global program context provider
|
|
10
|
+
*/
|
|
11
|
+
export function lazilyStartProgramContext() {
|
|
12
|
+
let globalProvider = window.squatchProgramId;
|
|
13
|
+
if (!globalProvider) {
|
|
14
|
+
debug("Creating program context provider");
|
|
15
|
+
// Lazily creates a global provider
|
|
16
|
+
globalProvider = new ContextProvider({
|
|
17
|
+
element: document.documentElement,
|
|
18
|
+
initialState: window.widgetIdent?.programId || undefined,
|
|
19
|
+
contextName: PROGRAM_CONTEXT_NAME,
|
|
20
|
+
}).start();
|
|
21
|
+
window.squatchProgramId = globalProvider;
|
|
22
|
+
}
|
|
23
|
+
return globalProvider;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Overide the globally defined Program ID context
|
|
27
|
+
*
|
|
28
|
+
* @param programId the new programID used by the user, or undefined if logged out
|
|
29
|
+
*/
|
|
30
|
+
export function setProgramId(programId) {
|
|
31
|
+
const globalProvider = lazilyStartProgramContext();
|
|
32
|
+
if (globalProvider.context !== programId) {
|
|
33
|
+
debug(`Setting program context value [${programId}]`);
|
|
34
|
+
globalProvider.context = programId;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Get the current value of the program context
|
|
39
|
+
*/
|
|
40
|
+
export function getProgramId() {
|
|
41
|
+
return window.squatchLocale?.context;
|
|
42
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ContextProvider } from "dom-context";
|
|
2
|
+
import { UserIdentity } from "../types";
|
|
3
|
+
/**
|
|
4
|
+
* Lazily start the user context provider. If it already exists, the existing provider is
|
|
5
|
+
* returned. This function is safe to call multiple times.
|
|
6
|
+
*
|
|
7
|
+
* @returns The global user context provider
|
|
8
|
+
*/
|
|
9
|
+
export declare function lazilyStartUserContext(): ContextProvider<UserIdentity | undefined>;
|
|
10
|
+
/**
|
|
11
|
+
* Extract a user identity from a JWT
|
|
12
|
+
*
|
|
13
|
+
* @param jwt The JWT to extract a user identity from
|
|
14
|
+
* @returns The user identity or undefined if the JWT is not valid
|
|
15
|
+
*/
|
|
16
|
+
export declare function userIdentityFromJwt(jwt?: string): UserIdentity | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Overide the globally defined user context, and persists the user identity in local storage
|
|
19
|
+
*
|
|
20
|
+
* @param identity the new identity of the user, or undefined if logged out
|
|
21
|
+
*/
|
|
22
|
+
export declare function setUserIdentity(identity?: UserIdentity): void;
|
|
23
|
+
/**
|
|
24
|
+
* Get the current value of the user context
|
|
25
|
+
*/
|
|
26
|
+
export declare function getUserIdentity(): UserIdentity | undefined;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import decode from "jwt-decode";
|
|
2
|
+
import { ContextProvider } from "dom-context";
|
|
3
|
+
import { equal } from "@wry/equality";
|
|
4
|
+
import { getEnvironmentSDK } from "../environment";
|
|
5
|
+
import { USER_CONTEXT_NAME, } from "../types";
|
|
6
|
+
import { startUserContextListenerForLocale } from "../listeners";
|
|
7
|
+
import { debug as _debug } from "../debug";
|
|
8
|
+
const debug = (...args) => _debug(USER_CONTEXT_NAME, ...args);
|
|
9
|
+
/**
|
|
10
|
+
* Lazily start the user context provider. If it already exists, the existing provider is
|
|
11
|
+
* returned. This function is safe to call multiple times.
|
|
12
|
+
*
|
|
13
|
+
* @returns The global user context provider
|
|
14
|
+
*/
|
|
15
|
+
export function lazilyStartUserContext() {
|
|
16
|
+
let globalProvider = window.squatchUserIdentity;
|
|
17
|
+
if (!globalProvider) {
|
|
18
|
+
debug("Creating user context provider");
|
|
19
|
+
// Lazily creates a global provider
|
|
20
|
+
globalProvider = new ContextProvider({
|
|
21
|
+
element: document.documentElement,
|
|
22
|
+
initialState: _getInitialValue(),
|
|
23
|
+
contextName: USER_CONTEXT_NAME,
|
|
24
|
+
}).start();
|
|
25
|
+
window.squatchUserIdentity = globalProvider;
|
|
26
|
+
startUserContextListenerForLocale();
|
|
27
|
+
}
|
|
28
|
+
return globalProvider;
|
|
29
|
+
}
|
|
30
|
+
function isDecodedSquatchJWT(decoded) {
|
|
31
|
+
return decoded.user && decoded.user.id && decoded.user.accountId;
|
|
32
|
+
}
|
|
33
|
+
function isDecodedWidgetAPIJWT(decoded) {
|
|
34
|
+
return decoded.sub && /.*:.*@.*:users/.test(decoded.sub);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Extract a user identity from a JWT
|
|
38
|
+
*
|
|
39
|
+
* @param jwt The JWT to extract a user identity from
|
|
40
|
+
* @returns The user identity or undefined if the JWT is not valid
|
|
41
|
+
*/
|
|
42
|
+
export function userIdentityFromJwt(jwt) {
|
|
43
|
+
if (!jwt)
|
|
44
|
+
return undefined;
|
|
45
|
+
try {
|
|
46
|
+
const decoded = decode(jwt);
|
|
47
|
+
const exp = decoded.exp;
|
|
48
|
+
let userId = undefined;
|
|
49
|
+
let accountId = undefined;
|
|
50
|
+
if (isDecodedWidgetAPIJWT(decoded)) {
|
|
51
|
+
// Pull the accountId and userId from the subject and Base64-decode them
|
|
52
|
+
// NOTE: This is to support classic theme engine widget token generation
|
|
53
|
+
const matches = decoded.sub.match(/(.*):(.*)@(.*):users/);
|
|
54
|
+
if (matches?.[1])
|
|
55
|
+
accountId = atob(matches[1]);
|
|
56
|
+
if (matches?.[2])
|
|
57
|
+
userId = atob(matches[2]);
|
|
58
|
+
}
|
|
59
|
+
else if (isDecodedSquatchJWT(decoded)) {
|
|
60
|
+
accountId = decoded.user.accountId;
|
|
61
|
+
userId = decoded.user.id;
|
|
62
|
+
}
|
|
63
|
+
if (!userId || !accountId) {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
// Check if the JWT has expired
|
|
67
|
+
if (exp && Date.now() >= exp * 1000) {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
id: userId,
|
|
72
|
+
accountId: accountId,
|
|
73
|
+
jwt,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
catch (e) {
|
|
77
|
+
// Invalid JWT
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function _getInitialValue() {
|
|
82
|
+
const sdk = getEnvironmentSDK();
|
|
83
|
+
switch (sdk.type) {
|
|
84
|
+
case "SquatchAndroid":
|
|
85
|
+
case "SquatchJS2":
|
|
86
|
+
return {
|
|
87
|
+
id: sdk.widgetIdent.userId,
|
|
88
|
+
accountId: sdk.widgetIdent.accountId,
|
|
89
|
+
jwt: sdk.widgetIdent.token,
|
|
90
|
+
};
|
|
91
|
+
case "SquatchPortal":
|
|
92
|
+
// Portals can have the jwt provided as a URL parameter, so look for that first
|
|
93
|
+
const searchParams = new URLSearchParams(document.location.search);
|
|
94
|
+
if (searchParams.has("jwt")) {
|
|
95
|
+
return userIdentityFromJwt(searchParams.get("jwt"));
|
|
96
|
+
}
|
|
97
|
+
// Look for user identity in local storage
|
|
98
|
+
const stored = localStorage.getItem(USER_CONTEXT_NAME);
|
|
99
|
+
if (!stored)
|
|
100
|
+
return undefined;
|
|
101
|
+
try {
|
|
102
|
+
const potentialUserIdent = JSON.parse(stored);
|
|
103
|
+
const identity = userIdentityFromJwt(potentialUserIdent.jwt);
|
|
104
|
+
if (identity) {
|
|
105
|
+
return {
|
|
106
|
+
...potentialUserIdent,
|
|
107
|
+
...identity,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
catch (e) {
|
|
113
|
+
// Not valid JSON
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
case "SquatchAdmin":
|
|
117
|
+
case "None":
|
|
118
|
+
// Not logged in for admin portal / none default case
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Overide the globally defined user context, and persists the user identity in local storage
|
|
124
|
+
*
|
|
125
|
+
* @param identity the new identity of the user, or undefined if logged out
|
|
126
|
+
*/
|
|
127
|
+
export function setUserIdentity(identity) {
|
|
128
|
+
const globalProvider = lazilyStartUserContext();
|
|
129
|
+
if (!equal(globalProvider.context, identity)) {
|
|
130
|
+
debug(`Setting user context value [${JSON.stringify(identity)}]`);
|
|
131
|
+
globalProvider.context = identity;
|
|
132
|
+
}
|
|
133
|
+
// Portals store identity in local storage
|
|
134
|
+
if (identity && getEnvironmentSDK().type === "SquatchPortal") {
|
|
135
|
+
localStorage.setItem(USER_CONTEXT_NAME, JSON.stringify(identity));
|
|
136
|
+
}
|
|
137
|
+
else if (!identity) {
|
|
138
|
+
localStorage.removeItem(USER_CONTEXT_NAME);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Get the current value of the user context
|
|
143
|
+
*/
|
|
144
|
+
export function getUserIdentity() {
|
|
145
|
+
return window.squatchUserIdentity?.context;
|
|
146
|
+
}
|
package/dist/debug.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function debug(ns: string, ...args: any[]): void;
|
package/dist/debug.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Environment, EnvironmentSDK, EngagementMedium } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Get the type of environment that this widget is being rendered in
|
|
4
|
+
*
|
|
5
|
+
* Should never return null.
|
|
6
|
+
*/
|
|
7
|
+
export declare function getEnvironment(): Environment;
|
|
8
|
+
/**
|
|
9
|
+
* Get the SDK for interacting with the host environment
|
|
10
|
+
*/
|
|
11
|
+
export declare function getEnvironmentSDK(): EnvironmentSDK;
|
|
12
|
+
export declare function isDemo(): boolean;
|
|
13
|
+
export declare function getTenantAlias(): string;
|
|
14
|
+
export declare function getAppDomain(): string;
|
|
15
|
+
export declare function getEngagementMedium(): EngagementMedium;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { DEFAULT_MEDIUM, } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Get the type of environment that this widget is being rendered in
|
|
4
|
+
*
|
|
5
|
+
* Should never return null.
|
|
6
|
+
*/
|
|
7
|
+
export function getEnvironment() {
|
|
8
|
+
return getEnvironmentSDK().type;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Get the SDK for interacting with the host environment
|
|
12
|
+
*/
|
|
13
|
+
export function getEnvironmentSDK() {
|
|
14
|
+
//@ts-ignore
|
|
15
|
+
if (window["SquatchAndroid"]) {
|
|
16
|
+
return {
|
|
17
|
+
type: "SquatchAndroid",
|
|
18
|
+
//@ts-ignore
|
|
19
|
+
android: window["SquatchAndroid"],
|
|
20
|
+
//@ts-ignore
|
|
21
|
+
widgetIdent: window["widgetIdent"],
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
//@ts-ignore
|
|
25
|
+
if (window["SquatchPortal"]) {
|
|
26
|
+
return {
|
|
27
|
+
type: "SquatchPortal",
|
|
28
|
+
//@ts-ignore
|
|
29
|
+
env: window["SquatchPortal"],
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
//@ts-ignore
|
|
33
|
+
if (window["SquatchAdmin"]) {
|
|
34
|
+
return {
|
|
35
|
+
type: "SquatchAdmin",
|
|
36
|
+
//@ts-ignore
|
|
37
|
+
adminSDK: window["SquatchAdmin"],
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
// Vanilla components mutates `widgetIdent` for portal, causing boilerplate to render as SquatchJS2
|
|
41
|
+
if (window["widgetIdent"] && window["widgetIdent"]?.env !== "demo") {
|
|
42
|
+
return {
|
|
43
|
+
type: "SquatchJS2",
|
|
44
|
+
//@ts-ignore
|
|
45
|
+
api: window.frameElement?.["squatchJsApi"],
|
|
46
|
+
//@ts-ignore
|
|
47
|
+
widgetIdent: window["widgetIdent"],
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
type: "None",
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export function isDemo() {
|
|
55
|
+
const sdk = getEnvironmentSDK();
|
|
56
|
+
return sdk.type === "None" || sdk.type === "SquatchAdmin";
|
|
57
|
+
}
|
|
58
|
+
// Fake tenant alias in demo mode
|
|
59
|
+
const FAKE_TENANT = "demo";
|
|
60
|
+
export function getTenantAlias() {
|
|
61
|
+
const sdk = getEnvironmentSDK();
|
|
62
|
+
switch (sdk.type) {
|
|
63
|
+
case "SquatchAndroid":
|
|
64
|
+
case "SquatchJS2":
|
|
65
|
+
return sdk.widgetIdent.tenantAlias;
|
|
66
|
+
case "SquatchAdmin":
|
|
67
|
+
case "None":
|
|
68
|
+
return FAKE_TENANT;
|
|
69
|
+
case "SquatchPortal":
|
|
70
|
+
return sdk.env.tenantAlias;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const DEFAULT_DOMAIN = "https://app.referralsaasquatch.com";
|
|
74
|
+
export function getAppDomain() {
|
|
75
|
+
const sdk = getEnvironmentSDK();
|
|
76
|
+
switch (sdk.type) {
|
|
77
|
+
case "SquatchAndroid":
|
|
78
|
+
case "SquatchJS2":
|
|
79
|
+
return sdk.widgetIdent.appDomain;
|
|
80
|
+
case "SquatchPortal":
|
|
81
|
+
return sdk.env?.appDomain || DEFAULT_DOMAIN;
|
|
82
|
+
case "SquatchAdmin":
|
|
83
|
+
case "None":
|
|
84
|
+
return DEFAULT_DOMAIN;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
export function getEngagementMedium() {
|
|
88
|
+
const sdk = getEnvironmentSDK();
|
|
89
|
+
switch (sdk.type) {
|
|
90
|
+
case "SquatchJS2":
|
|
91
|
+
return sdk.widgetIdent.engagementMedium || DEFAULT_MEDIUM;
|
|
92
|
+
case "SquatchAndroid":
|
|
93
|
+
case "SquatchPortal":
|
|
94
|
+
case "SquatchAdmin":
|
|
95
|
+
case "None":
|
|
96
|
+
return DEFAULT_MEDIUM;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function fetchLocale(): Promise<string | undefined>;
|