@knocklabs/react-core 0.6.15 → 0.7.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/CHANGELOG.md +15 -0
- package/dist/cjs/modules/core/context/KnockProvider.js +1 -1
- package/dist/cjs/modules/core/context/KnockProvider.js.map +1 -1
- package/dist/cjs/modules/core/hooks/useAuthenticatedKnockClient.js +1 -1
- package/dist/cjs/modules/core/hooks/useAuthenticatedKnockClient.js.map +1 -1
- package/dist/esm/modules/core/context/KnockProvider.mjs +25 -25
- package/dist/esm/modules/core/context/KnockProvider.mjs.map +1 -1
- package/dist/esm/modules/core/hooks/useAuthenticatedKnockClient.mjs +16 -16
- package/dist/esm/modules/core/hooks/useAuthenticatedKnockClient.mjs.map +1 -1
- package/dist/types/modules/core/context/KnockProvider.d.ts +25 -4
- package/dist/types/modules/core/context/KnockProvider.d.ts.map +1 -1
- package/dist/types/modules/core/hooks/useAuthenticatedKnockClient.d.ts +11 -2
- package/dist/types/modules/core/hooks/useAuthenticatedKnockClient.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/modules/core/context/KnockProvider.tsx +36 -7
- package/src/modules/core/hooks/useAuthenticatedKnockClient.ts +47 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.7.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 2a0b3e2: Adds support for inline user `identify` calls when authenticating a user via the Knock client.
|
|
8
|
+
You can now pass a `user` object, for example `{ id: "123" }`, directly to the `authenticate` function.
|
|
9
|
+
Additional properties can also be included to update the user record, such as `{ id: "123", name: "Knock" }`.
|
|
10
|
+
|
|
11
|
+
This update also applies to `KnockProvider`, where you can now pass a `user` prop instead of a `userId` prop to achieve the same behavior.
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- Updated dependencies [2a0b3e2]
|
|
16
|
+
- @knocklabs/client@0.15.0
|
|
17
|
+
|
|
3
18
|
## 0.6.15
|
|
4
19
|
|
|
5
20
|
### Patch Changes
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=require("react"),K=require("../../i18n/context/KnockI18nProvider.js"),P=require("../hooks/useAuthenticatedKnockClient.js");require("fast-deep-equal");function m(e){if(e&&typeof e=="object"&&"default"in e)return e;const n=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const t in e)if(t!=="default"){const o=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(n,t,o.get?o:{enumerable:!0,get:()=>e[t]})}}return n.default=e,Object.freeze(n)}const r=m(b),a=r.createContext(null),v=({apiKey:e,host:n,logLevel:t,userToken:o,onUserTokenExpiring:u,timeBeforeExpirationInMs:i,children:s,i18n:l,...c})=>{const d=(c==null?void 0:c.user)||(c==null?void 0:c.userId),k=r.useMemo(()=>({host:n,onUserTokenExpiring:u,timeBeforeExpirationInMs:i,logLevel:t}),[n,u,i,t]),f=P(e??"",d,o,k);return r.createElement(a.Provider,{value:{knock:f}},r.createElement(K.KnockI18nProvider,{i18n:l},s))},C=()=>{const e=r.useContext(a);if(!e)throw new Error("useKnockClient must be used within a KnockProvider");return e.knock};exports.KnockProvider=v;exports.useKnockClient=C;
|
|
2
2
|
//# sourceMappingURL=KnockProvider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"KnockProvider.js","sources":["../../../../../src/modules/core/context/KnockProvider.tsx"],"sourcesContent":["import Knock, {
|
|
1
|
+
{"version":3,"file":"KnockProvider.js","sources":["../../../../../src/modules/core/context/KnockProvider.tsx"],"sourcesContent":["import Knock, {\n AuthenticateOptions,\n LogLevel,\n UserWithProperties,\n} from \"@knocklabs/client\";\nimport * as React from \"react\";\nimport { PropsWithChildren } from \"react\";\n\nimport { I18nContent, KnockI18nProvider } from \"../../i18n\";\nimport { useAuthenticatedKnockClient } from \"../hooks\";\n\nexport interface KnockProviderState {\n knock: Knock;\n}\n\nconst KnockContext = React.createContext<KnockProviderState | null>(null);\n\nexport type KnockProviderProps = {\n // Knock client props\n apiKey: string | undefined;\n host?: string;\n userToken?: Knock[\"userToken\"];\n onUserTokenExpiring?: AuthenticateOptions[\"onUserTokenExpiring\"];\n timeBeforeExpirationInMs?: AuthenticateOptions[\"timeBeforeExpirationInMs\"];\n // i18n translations\n i18n?: I18nContent;\n logLevel?: LogLevel;\n} & (\n | {\n /**\n * @deprecated The `userId` prop is deprecated and will be removed in a future version.\n * Please pass the `user` prop instead containing an `id` value.\n * example:\n * ```ts\n * <KnockProvider user={{ id: \"user_123\" }}></KnockProvider>\n * ```\n */\n userId: Knock[\"userId\"];\n user?: never;\n }\n | {\n user: UserWithProperties;\n /**\n * @deprecated The `userId` prop is deprecated and will be removed in a future version.\n * Please pass the `user` prop instead containing an `id` value.\n * example:\n * ```ts\n * <KnockProvider user={{ id: \"user_123\" }}></KnockProvider>\n * ```\n */\n userId?: never;\n }\n);\n\nexport const KnockProvider: React.FC<PropsWithChildren<KnockProviderProps>> = ({\n apiKey,\n host,\n logLevel,\n userToken,\n onUserTokenExpiring,\n timeBeforeExpirationInMs,\n children,\n i18n,\n ...props\n}) => {\n const userIdOrUserWithProperties = props?.user || props?.userId;\n\n // We memoize the options here so that we don't create a new object on every re-render\n const authenticateOptions = React.useMemo(\n () => ({\n host,\n onUserTokenExpiring,\n timeBeforeExpirationInMs,\n logLevel,\n }),\n [host, onUserTokenExpiring, timeBeforeExpirationInMs, logLevel],\n );\n\n const knock = useAuthenticatedKnockClient(\n apiKey ?? \"\",\n userIdOrUserWithProperties,\n userToken,\n authenticateOptions,\n );\n\n return (\n <KnockContext.Provider value={{ knock }}>\n <KnockI18nProvider i18n={i18n}>{children}</KnockI18nProvider>\n </KnockContext.Provider>\n );\n};\n\nexport const useKnockClient = (): Knock => {\n const context = React.useContext(KnockContext);\n if (!context) {\n throw new Error(\"useKnockClient must be used within a KnockProvider\");\n }\n return context.knock;\n};\n"],"names":["KnockContext","React","createContext","KnockProvider","apiKey","host","logLevel","userToken","onUserTokenExpiring","timeBeforeExpirationInMs","children","i18n","props","userIdOrUserWithProperties","user","userId","authenticateOptions","useMemo","knock","useAuthenticatedKnockClient","KnockI18nProvider","useKnockClient","context","useContext","Error"],"mappings":"yjBAeMA,EAAeC,EAAMC,cAAyC,IAAI,EAuC3DC,EAAiEA,CAAC,CAC7EC,OAAAA,EACAC,KAAAA,EACAC,SAAAA,EACAC,UAAAA,EACAC,oBAAAA,EACAC,yBAAAA,EACAC,SAAAA,EACAC,KAAAA,EACA,GAAGC,CACL,IAAM,CACEC,MAAAA,GAA6BD,GAAAA,YAAAA,EAAOE,QAAQF,GAAAA,YAAAA,EAAOG,QAGnDC,EAAsBf,EAAMgB,QAChC,KAAO,CACLZ,KAAAA,EACAG,oBAAAA,EACAC,yBAAAA,EACAH,SAAAA,IAEF,CAACD,EAAMG,EAAqBC,EAA0BH,CAAQ,CAChE,EAEMY,EAAQC,EACZf,GAAU,GACVS,EACAN,EACAS,CACF,EAEA,OACGf,EAAA,cAAAD,EAAa,SAAb,CAAsB,MAAO,CAAEkB,MAAAA,CAAAA,CAC9B,EAAAjB,EAAA,cAACmB,EAAAA,kBAAkB,CAAA,KAAAT,CAAA,EAAaD,CAAS,CAC3C,CAEJ,EAEaW,EAAiBA,IAAa,CACnCC,MAAAA,EAAUrB,EAAMsB,WAAWvB,CAAY,EAC7C,GAAI,CAACsB,EACG,MAAA,IAAIE,MAAM,oDAAoD,EAEtE,OAAOF,EAAQJ,KACjB"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";const
|
|
1
|
+
"use strict";const q=require("@knocklabs/client"),x=require("react");require("../context/KnockProvider.js");const _=require("./useStableOptions.js");require("date-fns");const i=n=>n&&typeof n=="object"&&"default"in n?n:{default:n},d=i(q),o=i(x);function k(n,t,a,c={}){n.authenticate(t,a,{onUserTokenExpiring:c==null?void 0:c.onUserTokenExpiring,timeBeforeExpirationInMs:c==null?void 0:c.timeBeforeExpirationInMs})}function m(n,t,a,c={}){const l=o.default.useRef(),u=_(c);return o.default.useMemo(()=>{const g=typeof t=="string"?t:t==null?void 0:t.id,e=l.current;if(e&&e.isAuthenticated()&&(e.userId!==g||e.userToken!==a))return k(e,t,a,u),e;e&&e.teardown();const f=new d.default(n,{host:u.host,logLevel:u.logLevel});return k(f,t,a,u),l.current=f,f},[n,t,a,u])}module.exports=m;
|
|
2
2
|
//# sourceMappingURL=useAuthenticatedKnockClient.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAuthenticatedKnockClient.js","sources":["../../../../../src/modules/core/hooks/useAuthenticatedKnockClient.ts"],"sourcesContent":["import Knock, {
|
|
1
|
+
{"version":3,"file":"useAuthenticatedKnockClient.js","sources":["../../../../../src/modules/core/hooks/useAuthenticatedKnockClient.ts"],"sourcesContent":["import Knock, {\n AuthenticateOptions,\n KnockOptions,\n UserId,\n UserIdOrUserWithProperties,\n} from \"@knocklabs/client\";\nimport React from \"react\";\n\nimport { useStableOptions } from \"../../core\";\n\nfunction authenticateWithOptions(\n knock: Knock,\n userIdOrUserWithProperties: UserIdOrUserWithProperties,\n userToken?: Knock[\"userToken\"],\n options: AuthenticateOptions = {},\n) {\n knock.authenticate(userIdOrUserWithProperties, userToken, {\n onUserTokenExpiring: options?.onUserTokenExpiring,\n timeBeforeExpirationInMs: options?.timeBeforeExpirationInMs,\n });\n}\n\nexport type AuthenticatedKnockClientOptions = KnockOptions &\n AuthenticateOptions;\n\n/**\n * @deprecated Passing `userId` as a `string` is deprecated and will be removed in a future version.\n * Please pass a `user` object instead containing an `id` value.\n * example:\n * ```ts\n * useAuthenticatedKnockClient(\"pk_test_12345\", { id: \"user_123\" });\n * ```\n */\nfunction useAuthenticatedKnockClient(\n apiKey: string,\n userIdOrUserWithProperties: UserId,\n userToken?: Knock[\"userToken\"],\n options?: AuthenticatedKnockClientOptions,\n): Knock;\nfunction useAuthenticatedKnockClient(\n apiKey: string,\n userIdOrUserWithProperties: UserIdOrUserWithProperties,\n userToken?: Knock[\"userToken\"],\n options?: AuthenticatedKnockClientOptions,\n): Knock;\nfunction useAuthenticatedKnockClient(\n apiKey: string,\n userIdOrUserWithProperties: UserIdOrUserWithProperties,\n userToken?: Knock[\"userToken\"],\n options: AuthenticatedKnockClientOptions = {},\n) {\n const knockRef = React.useRef<Knock | undefined>();\n const stableOptions = useStableOptions(options);\n\n return React.useMemo(() => {\n const userId =\n typeof userIdOrUserWithProperties === \"string\"\n ? userIdOrUserWithProperties\n : userIdOrUserWithProperties?.id;\n\n const currentKnock = knockRef.current;\n\n // If the userId and the userToken changes then just reauth\n if (\n currentKnock &&\n currentKnock.isAuthenticated() &&\n (currentKnock.userId !== userId || currentKnock.userToken !== userToken)\n ) {\n authenticateWithOptions(\n currentKnock,\n userIdOrUserWithProperties,\n userToken,\n stableOptions,\n );\n return currentKnock;\n }\n\n if (currentKnock) {\n currentKnock.teardown();\n }\n\n // Otherwise instantiate a new Knock client\n const knock = new Knock(apiKey, {\n host: stableOptions.host,\n logLevel: stableOptions.logLevel,\n });\n\n authenticateWithOptions(\n knock,\n userIdOrUserWithProperties,\n userToken,\n stableOptions,\n );\n knockRef.current = knock;\n\n return knock;\n }, [apiKey, userIdOrUserWithProperties, userToken, stableOptions]);\n}\n\nexport default useAuthenticatedKnockClient;\n"],"names":["authenticateWithOptions","knock","userIdOrUserWithProperties","userToken","options","authenticate","onUserTokenExpiring","timeBeforeExpirationInMs","useAuthenticatedKnockClient","apiKey","knockRef","React","useRef","stableOptions","useStableOptions","useMemo","userId","id","currentKnock","current","isAuthenticated","teardown","Knock","host","logLevel"],"mappings":"qPAUA,SAASA,EACPC,EACAC,EACAC,EACAC,EAA+B,CAAA,EAC/B,CACMC,EAAAA,aAAaH,EAA4BC,EAAW,CACxDG,oBAAqBF,GAAAA,YAAAA,EAASE,oBAC9BC,yBAA0BH,GAAAA,YAAAA,EAASG,wBAAAA,CACpC,CACH,CAyBA,SAASC,EACPC,EACAP,EACAC,EACAC,EAA2C,CAAA,EAC3C,CACMM,MAAAA,EAAWC,UAAMC,OAA0B,EAC3CC,EAAgBC,EAAiBV,CAAO,EAEvCO,OAAAA,EAAAA,QAAMI,QAAQ,IAAM,CACzB,MAAMC,EACJ,OAAOd,GAA+B,SAClCA,EACAA,GAAAA,YAAAA,EAA4Be,GAE5BC,EAAeR,EAASS,QAI5BD,GAAAA,GACAA,EAAaE,gBAAgB,IAC5BF,EAAaF,SAAWA,GAAUE,EAAaf,YAAcA,GAG5De,OAAAA,EAAAA,EACAhB,EACAC,EACAU,CACF,EACOK,EAGLA,GACFA,EAAaG,SAAS,EAIlBpB,MAAAA,EAAQ,IAAIqB,EAAAA,QAAMb,EAAQ,CAC9Bc,KAAMV,EAAcU,KACpBC,SAAUX,EAAcW,QAAAA,CACzB,EAGCvB,OAAAA,EAAAA,EACAC,EACAC,EACAU,CACF,EACAH,EAASS,QAAUlB,EAEZA,GACN,CAACQ,EAAQP,EAA4BC,EAAWU,CAAa,CAAC,CACnE"}
|
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
import * as
|
|
2
|
-
import { KnockI18nProvider as
|
|
3
|
-
import
|
|
1
|
+
import * as e from "react";
|
|
2
|
+
import { KnockI18nProvider as l } from "../../i18n/context/KnockI18nProvider.mjs";
|
|
3
|
+
import C from "../hooks/useAuthenticatedKnockClient.mjs";
|
|
4
4
|
import "fast-deep-equal";
|
|
5
|
-
const
|
|
6
|
-
apiKey:
|
|
7
|
-
host:
|
|
8
|
-
logLevel:
|
|
9
|
-
|
|
10
|
-
userToken: s,
|
|
5
|
+
const i = e.createContext(null), f = ({
|
|
6
|
+
apiKey: n,
|
|
7
|
+
host: c,
|
|
8
|
+
logLevel: o,
|
|
9
|
+
userToken: k,
|
|
11
10
|
onUserTokenExpiring: r,
|
|
12
|
-
timeBeforeExpirationInMs:
|
|
13
|
-
children:
|
|
14
|
-
i18n:
|
|
11
|
+
timeBeforeExpirationInMs: u,
|
|
12
|
+
children: m,
|
|
13
|
+
i18n: a,
|
|
14
|
+
...t
|
|
15
15
|
}) => {
|
|
16
|
-
const
|
|
17
|
-
host:
|
|
16
|
+
const s = (t == null ? void 0 : t.user) || (t == null ? void 0 : t.userId), d = e.useMemo(() => ({
|
|
17
|
+
host: c,
|
|
18
18
|
onUserTokenExpiring: r,
|
|
19
|
-
timeBeforeExpirationInMs:
|
|
20
|
-
logLevel:
|
|
21
|
-
}), [
|
|
22
|
-
return /* @__PURE__ */
|
|
19
|
+
timeBeforeExpirationInMs: u,
|
|
20
|
+
logLevel: o
|
|
21
|
+
}), [c, r, u, o]), K = C(n ?? "", s, k, d);
|
|
22
|
+
return /* @__PURE__ */ e.createElement(i.Provider, { value: {
|
|
23
23
|
knock: K
|
|
24
|
-
} }, /* @__PURE__ */
|
|
25
|
-
},
|
|
26
|
-
const
|
|
27
|
-
if (!
|
|
24
|
+
} }, /* @__PURE__ */ e.createElement(l, { i18n: a }, m));
|
|
25
|
+
}, w = () => {
|
|
26
|
+
const n = e.useContext(i);
|
|
27
|
+
if (!n)
|
|
28
28
|
throw new Error("useKnockClient must be used within a KnockProvider");
|
|
29
|
-
return
|
|
29
|
+
return n.knock;
|
|
30
30
|
};
|
|
31
31
|
export {
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
f as KnockProvider,
|
|
33
|
+
w as useKnockClient
|
|
34
34
|
};
|
|
35
35
|
//# sourceMappingURL=KnockProvider.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"KnockProvider.mjs","sources":["../../../../../src/modules/core/context/KnockProvider.tsx"],"sourcesContent":["import Knock, {
|
|
1
|
+
{"version":3,"file":"KnockProvider.mjs","sources":["../../../../../src/modules/core/context/KnockProvider.tsx"],"sourcesContent":["import Knock, {\n AuthenticateOptions,\n LogLevel,\n UserWithProperties,\n} from \"@knocklabs/client\";\nimport * as React from \"react\";\nimport { PropsWithChildren } from \"react\";\n\nimport { I18nContent, KnockI18nProvider } from \"../../i18n\";\nimport { useAuthenticatedKnockClient } from \"../hooks\";\n\nexport interface KnockProviderState {\n knock: Knock;\n}\n\nconst KnockContext = React.createContext<KnockProviderState | null>(null);\n\nexport type KnockProviderProps = {\n // Knock client props\n apiKey: string | undefined;\n host?: string;\n userToken?: Knock[\"userToken\"];\n onUserTokenExpiring?: AuthenticateOptions[\"onUserTokenExpiring\"];\n timeBeforeExpirationInMs?: AuthenticateOptions[\"timeBeforeExpirationInMs\"];\n // i18n translations\n i18n?: I18nContent;\n logLevel?: LogLevel;\n} & (\n | {\n /**\n * @deprecated The `userId` prop is deprecated and will be removed in a future version.\n * Please pass the `user` prop instead containing an `id` value.\n * example:\n * ```ts\n * <KnockProvider user={{ id: \"user_123\" }}></KnockProvider>\n * ```\n */\n userId: Knock[\"userId\"];\n user?: never;\n }\n | {\n user: UserWithProperties;\n /**\n * @deprecated The `userId` prop is deprecated and will be removed in a future version.\n * Please pass the `user` prop instead containing an `id` value.\n * example:\n * ```ts\n * <KnockProvider user={{ id: \"user_123\" }}></KnockProvider>\n * ```\n */\n userId?: never;\n }\n);\n\nexport const KnockProvider: React.FC<PropsWithChildren<KnockProviderProps>> = ({\n apiKey,\n host,\n logLevel,\n userToken,\n onUserTokenExpiring,\n timeBeforeExpirationInMs,\n children,\n i18n,\n ...props\n}) => {\n const userIdOrUserWithProperties = props?.user || props?.userId;\n\n // We memoize the options here so that we don't create a new object on every re-render\n const authenticateOptions = React.useMemo(\n () => ({\n host,\n onUserTokenExpiring,\n timeBeforeExpirationInMs,\n logLevel,\n }),\n [host, onUserTokenExpiring, timeBeforeExpirationInMs, logLevel],\n );\n\n const knock = useAuthenticatedKnockClient(\n apiKey ?? \"\",\n userIdOrUserWithProperties,\n userToken,\n authenticateOptions,\n );\n\n return (\n <KnockContext.Provider value={{ knock }}>\n <KnockI18nProvider i18n={i18n}>{children}</KnockI18nProvider>\n </KnockContext.Provider>\n );\n};\n\nexport const useKnockClient = (): Knock => {\n const context = React.useContext(KnockContext);\n if (!context) {\n throw new Error(\"useKnockClient must be used within a KnockProvider\");\n }\n return context.knock;\n};\n"],"names":["KnockContext","React","createContext","KnockProvider","apiKey","host","logLevel","userToken","onUserTokenExpiring","timeBeforeExpirationInMs","children","i18n","props","userIdOrUserWithProperties","user","userId","authenticateOptions","useMemo","knock","useAuthenticatedKnockClient","KnockI18nProvider","useKnockClient","context","useContext","Error"],"mappings":";;;;AAeA,MAAMA,IAAeC,EAAMC,cAAyC,IAAI,GAuC3DC,IAAiEA,CAAC;AAAA,EAC7EC,QAAAA;AAAAA,EACAC,MAAAA;AAAAA,EACAC,UAAAA;AAAAA,EACAC,WAAAA;AAAAA,EACAC,qBAAAA;AAAAA,EACAC,0BAAAA;AAAAA,EACAC,UAAAA;AAAAA,EACAC,MAAAA;AAAAA,EACA,GAAGC;AACL,MAAM;AACEC,QAAAA,KAA6BD,KAAAA,gBAAAA,EAAOE,UAAQF,KAAAA,gBAAAA,EAAOG,SAGnDC,IAAsBf,EAAMgB,QAChC,OAAO;AAAA,IACLZ,MAAAA;AAAAA,IACAG,qBAAAA;AAAAA,IACAC,0BAAAA;AAAAA,IACAH,UAAAA;AAAAA,MAEF,CAACD,GAAMG,GAAqBC,GAA0BH,CAAQ,CAChE,GAEMY,IAAQC,EACZf,KAAU,IACVS,GACAN,GACAS,CACF;AAEA,SACG,gBAAAf,EAAA,cAAAD,EAAa,UAAb,EAAsB,OAAO;AAAA,IAAEkB,OAAAA;AAAAA,EAAAA,EAC9B,GAAA,gBAAAjB,EAAA,cAACmB,GAAkB,EAAA,MAAAT,EAAA,GAAaD,CAAS,CAC3C;AAEJ,GAEaW,IAAiBA,MAAa;AACnCC,QAAAA,IAAUrB,EAAMsB,WAAWvB,CAAY;AAC7C,MAAI,CAACsB;AACG,UAAA,IAAIE,MAAM,oDAAoD;AAEtE,SAAOF,EAAQJ;AACjB;"}
|
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import
|
|
1
|
+
import g from "@knocklabs/client";
|
|
2
2
|
import u from "react";
|
|
3
3
|
import "../context/KnockProvider.mjs";
|
|
4
|
-
import
|
|
4
|
+
import x from "./useStableOptions.mjs";
|
|
5
5
|
import "date-fns";
|
|
6
|
-
function
|
|
7
|
-
|
|
6
|
+
function k(m, n, a, t = {}) {
|
|
7
|
+
m.authenticate(n, a, {
|
|
8
8
|
onUserTokenExpiring: t == null ? void 0 : t.onUserTokenExpiring,
|
|
9
9
|
timeBeforeExpirationInMs: t == null ? void 0 : t.timeBeforeExpirationInMs
|
|
10
10
|
});
|
|
11
11
|
}
|
|
12
|
-
function
|
|
13
|
-
const
|
|
12
|
+
function v(m, n, a, t = {}) {
|
|
13
|
+
const l = u.useRef(), f = x(t);
|
|
14
14
|
return u.useMemo(() => {
|
|
15
|
-
const e =
|
|
16
|
-
if (
|
|
17
|
-
return
|
|
18
|
-
|
|
19
|
-
const o = new m
|
|
20
|
-
host:
|
|
21
|
-
logLevel:
|
|
15
|
+
const e = typeof n == "string" ? n : n == null ? void 0 : n.id, c = l.current;
|
|
16
|
+
if (c && c.isAuthenticated() && (c.userId !== e || c.userToken !== a))
|
|
17
|
+
return k(c, n, a, f), c;
|
|
18
|
+
c && c.teardown();
|
|
19
|
+
const o = new g(m, {
|
|
20
|
+
host: f.host,
|
|
21
|
+
logLevel: f.logLevel
|
|
22
22
|
});
|
|
23
|
-
return
|
|
24
|
-
}, [
|
|
23
|
+
return k(o, n, a, f), l.current = o, o;
|
|
24
|
+
}, [m, n, a, f]);
|
|
25
25
|
}
|
|
26
26
|
export {
|
|
27
|
-
|
|
27
|
+
v as default
|
|
28
28
|
};
|
|
29
29
|
//# sourceMappingURL=useAuthenticatedKnockClient.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAuthenticatedKnockClient.mjs","sources":["../../../../../src/modules/core/hooks/useAuthenticatedKnockClient.ts"],"sourcesContent":["import Knock, {
|
|
1
|
+
{"version":3,"file":"useAuthenticatedKnockClient.mjs","sources":["../../../../../src/modules/core/hooks/useAuthenticatedKnockClient.ts"],"sourcesContent":["import Knock, {\n AuthenticateOptions,\n KnockOptions,\n UserId,\n UserIdOrUserWithProperties,\n} from \"@knocklabs/client\";\nimport React from \"react\";\n\nimport { useStableOptions } from \"../../core\";\n\nfunction authenticateWithOptions(\n knock: Knock,\n userIdOrUserWithProperties: UserIdOrUserWithProperties,\n userToken?: Knock[\"userToken\"],\n options: AuthenticateOptions = {},\n) {\n knock.authenticate(userIdOrUserWithProperties, userToken, {\n onUserTokenExpiring: options?.onUserTokenExpiring,\n timeBeforeExpirationInMs: options?.timeBeforeExpirationInMs,\n });\n}\n\nexport type AuthenticatedKnockClientOptions = KnockOptions &\n AuthenticateOptions;\n\n/**\n * @deprecated Passing `userId` as a `string` is deprecated and will be removed in a future version.\n * Please pass a `user` object instead containing an `id` value.\n * example:\n * ```ts\n * useAuthenticatedKnockClient(\"pk_test_12345\", { id: \"user_123\" });\n * ```\n */\nfunction useAuthenticatedKnockClient(\n apiKey: string,\n userIdOrUserWithProperties: UserId,\n userToken?: Knock[\"userToken\"],\n options?: AuthenticatedKnockClientOptions,\n): Knock;\nfunction useAuthenticatedKnockClient(\n apiKey: string,\n userIdOrUserWithProperties: UserIdOrUserWithProperties,\n userToken?: Knock[\"userToken\"],\n options?: AuthenticatedKnockClientOptions,\n): Knock;\nfunction useAuthenticatedKnockClient(\n apiKey: string,\n userIdOrUserWithProperties: UserIdOrUserWithProperties,\n userToken?: Knock[\"userToken\"],\n options: AuthenticatedKnockClientOptions = {},\n) {\n const knockRef = React.useRef<Knock | undefined>();\n const stableOptions = useStableOptions(options);\n\n return React.useMemo(() => {\n const userId =\n typeof userIdOrUserWithProperties === \"string\"\n ? userIdOrUserWithProperties\n : userIdOrUserWithProperties?.id;\n\n const currentKnock = knockRef.current;\n\n // If the userId and the userToken changes then just reauth\n if (\n currentKnock &&\n currentKnock.isAuthenticated() &&\n (currentKnock.userId !== userId || currentKnock.userToken !== userToken)\n ) {\n authenticateWithOptions(\n currentKnock,\n userIdOrUserWithProperties,\n userToken,\n stableOptions,\n );\n return currentKnock;\n }\n\n if (currentKnock) {\n currentKnock.teardown();\n }\n\n // Otherwise instantiate a new Knock client\n const knock = new Knock(apiKey, {\n host: stableOptions.host,\n logLevel: stableOptions.logLevel,\n });\n\n authenticateWithOptions(\n knock,\n userIdOrUserWithProperties,\n userToken,\n stableOptions,\n );\n knockRef.current = knock;\n\n return knock;\n }, [apiKey, userIdOrUserWithProperties, userToken, stableOptions]);\n}\n\nexport default useAuthenticatedKnockClient;\n"],"names":["authenticateWithOptions","knock","userIdOrUserWithProperties","userToken","options","authenticate","onUserTokenExpiring","timeBeforeExpirationInMs","useAuthenticatedKnockClient","apiKey","knockRef","React","useRef","stableOptions","useStableOptions","useMemo","userId","id","currentKnock","current","isAuthenticated","teardown","Knock","host","logLevel"],"mappings":";;;;;AAUA,SAASA,EACPC,GACAC,GACAC,GACAC,IAA+B,CAAA,GAC/B;AACMC,EAAAA,EAAAA,aAAaH,GAA4BC,GAAW;AAAA,IACxDG,qBAAqBF,KAAAA,gBAAAA,EAASE;AAAAA,IAC9BC,0BAA0BH,KAAAA,gBAAAA,EAASG;AAAAA,EAAAA,CACpC;AACH;AAyBA,SAASC,EACPC,GACAP,GACAC,GACAC,IAA2C,CAAA,GAC3C;AACMM,QAAAA,IAAWC,EAAMC,OAA0B,GAC3CC,IAAgBC,EAAiBV,CAAO;AAEvCO,SAAAA,EAAMI,QAAQ,MAAM;AACzB,UAAMC,IACJ,OAAOd,KAA+B,WAClCA,IACAA,KAAAA,gBAAAA,EAA4Be,IAE5BC,IAAeR,EAASS;AAI5BD,QAAAA,KACAA,EAAaE,gBAAgB,MAC5BF,EAAaF,WAAWA,KAAUE,EAAaf,cAAcA;AAG5De,aAAAA,EAAAA,GACAhB,GACAC,GACAU,CACF,GACOK;AAGT,IAAIA,KACFA,EAAaG,SAAS;AAIlBpB,UAAAA,IAAQ,IAAIqB,EAAMb,GAAQ;AAAA,MAC9Bc,MAAMV,EAAcU;AAAAA,MACpBC,UAAUX,EAAcW;AAAAA,IAAAA,CACzB;AAGCvB,WAAAA,EAAAA,GACAC,GACAC,GACAU,CACF,GACAH,EAASS,UAAUlB,GAEZA;AAAAA,KACN,CAACQ,GAAQP,GAA4BC,GAAWU,CAAa,CAAC;AACnE;"}
|
|
@@ -1,20 +1,41 @@
|
|
|
1
|
-
import { default as Knock, AuthenticateOptions, LogLevel } from '@knocklabs/client';
|
|
1
|
+
import { default as Knock, AuthenticateOptions, LogLevel, UserWithProperties } from '@knocklabs/client';
|
|
2
2
|
import { PropsWithChildren } from 'react';
|
|
3
3
|
import { I18nContent } from '../../i18n';
|
|
4
4
|
import * as React from "react";
|
|
5
5
|
export interface KnockProviderState {
|
|
6
6
|
knock: Knock;
|
|
7
7
|
}
|
|
8
|
-
export
|
|
8
|
+
export type KnockProviderProps = {
|
|
9
9
|
apiKey: string | undefined;
|
|
10
10
|
host?: string;
|
|
11
|
-
userId: Knock["userId"];
|
|
12
11
|
userToken?: Knock["userToken"];
|
|
13
12
|
onUserTokenExpiring?: AuthenticateOptions["onUserTokenExpiring"];
|
|
14
13
|
timeBeforeExpirationInMs?: AuthenticateOptions["timeBeforeExpirationInMs"];
|
|
15
14
|
i18n?: I18nContent;
|
|
16
15
|
logLevel?: LogLevel;
|
|
17
|
-
}
|
|
16
|
+
} & ({
|
|
17
|
+
/**
|
|
18
|
+
* @deprecated The `userId` prop is deprecated and will be removed in a future version.
|
|
19
|
+
* Please pass the `user` prop instead containing an `id` value.
|
|
20
|
+
* example:
|
|
21
|
+
* ```ts
|
|
22
|
+
* <KnockProvider user={{ id: "user_123" }}></KnockProvider>
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
userId: Knock["userId"];
|
|
26
|
+
user?: never;
|
|
27
|
+
} | {
|
|
28
|
+
user: UserWithProperties;
|
|
29
|
+
/**
|
|
30
|
+
* @deprecated The `userId` prop is deprecated and will be removed in a future version.
|
|
31
|
+
* Please pass the `user` prop instead containing an `id` value.
|
|
32
|
+
* example:
|
|
33
|
+
* ```ts
|
|
34
|
+
* <KnockProvider user={{ id: "user_123" }}></KnockProvider>
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
userId?: never;
|
|
38
|
+
});
|
|
18
39
|
export declare const KnockProvider: React.FC<PropsWithChildren<KnockProviderProps>>;
|
|
19
40
|
export declare const useKnockClient: () => Knock;
|
|
20
41
|
//# sourceMappingURL=KnockProvider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"KnockProvider.d.ts","sourceRoot":"","sources":["../../../../../src/modules/core/context/KnockProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"KnockProvider.d.ts","sourceRoot":"","sources":["../../../../../src/modules/core/context/KnockProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EACZ,mBAAmB,EACnB,QAAQ,EACR,kBAAkB,EACnB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAC;AAE1C,OAAO,EAAE,WAAW,EAAqB,MAAM,YAAY,CAAC;AAG5D,MAAM,WAAW,kBAAkB;IACjC,KAAK,EAAE,KAAK,CAAC;CACd;AAID,MAAM,MAAM,kBAAkB,GAAG;IAE/B,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,KAAK,CAAC,WAAW,CAAC,CAAC;IAC/B,mBAAmB,CAAC,EAAE,mBAAmB,CAAC,qBAAqB,CAAC,CAAC;IACjE,wBAAwB,CAAC,EAAE,mBAAmB,CAAC,0BAA0B,CAAC,CAAC;IAE3E,IAAI,CAAC,EAAE,WAAW,CAAC;IACnB,QAAQ,CAAC,EAAE,QAAQ,CAAC;CACrB,GAAG,CACA;IACE;;;;;;;OAOG;IACH,MAAM,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC;IACxB,IAAI,CAAC,EAAE,KAAK,CAAC;CACd,GACD;IACE,IAAI,EAAE,kBAAkB,CAAC;IACzB;;;;;;;OAOG;IACH,MAAM,CAAC,EAAE,KAAK,CAAC;CAChB,CACJ,CAAC;AAEF,eAAO,MAAM,aAAa,EAAE,KAAK,CAAC,EAAE,CAAC,iBAAiB,CAAC,kBAAkB,CAAC,CAoCzE,CAAC;AAEF,eAAO,MAAM,cAAc,QAAO,KAMjC,CAAC"}
|
|
@@ -1,5 +1,14 @@
|
|
|
1
|
-
import { default as Knock, AuthenticateOptions, KnockOptions } from '@knocklabs/client';
|
|
1
|
+
import { default as Knock, AuthenticateOptions, KnockOptions, UserId, UserIdOrUserWithProperties } from '@knocklabs/client';
|
|
2
2
|
export type AuthenticatedKnockClientOptions = KnockOptions & AuthenticateOptions;
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated Passing `userId` as a `string` is deprecated and will be removed in a future version.
|
|
5
|
+
* Please pass a `user` object instead containing an `id` value.
|
|
6
|
+
* example:
|
|
7
|
+
* ```ts
|
|
8
|
+
* useAuthenticatedKnockClient("pk_test_12345", { id: "user_123" });
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
declare function useAuthenticatedKnockClient(apiKey: string, userIdOrUserWithProperties: UserId, userToken?: Knock["userToken"], options?: AuthenticatedKnockClientOptions): Knock;
|
|
12
|
+
declare function useAuthenticatedKnockClient(apiKey: string, userIdOrUserWithProperties: UserIdOrUserWithProperties, userToken?: Knock["userToken"], options?: AuthenticatedKnockClientOptions): Knock;
|
|
4
13
|
export default useAuthenticatedKnockClient;
|
|
5
14
|
//# sourceMappingURL=useAuthenticatedKnockClient.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAuthenticatedKnockClient.d.ts","sourceRoot":"","sources":["../../../../../src/modules/core/hooks/useAuthenticatedKnockClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"useAuthenticatedKnockClient.d.ts","sourceRoot":"","sources":["../../../../../src/modules/core/hooks/useAuthenticatedKnockClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EACZ,mBAAmB,EACnB,YAAY,EACZ,MAAM,EACN,0BAA0B,EAC3B,MAAM,mBAAmB,CAAC;AAiB3B,MAAM,MAAM,+BAA+B,GAAG,YAAY,GACxD,mBAAmB,CAAC;AAEtB;;;;;;;GAOG;AACH,iBAAS,2BAA2B,CAClC,MAAM,EAAE,MAAM,EACd,0BAA0B,EAAE,MAAM,EAClC,SAAS,CAAC,EAAE,KAAK,CAAC,WAAW,CAAC,EAC9B,OAAO,CAAC,EAAE,+BAA+B,GACxC,KAAK,CAAC;AACT,iBAAS,2BAA2B,CAClC,MAAM,EAAE,MAAM,EACd,0BAA0B,EAAE,0BAA0B,EACtD,SAAS,CAAC,EAAE,KAAK,CAAC,WAAW,CAAC,EAC9B,OAAO,CAAC,EAAE,+BAA+B,GACxC,KAAK,CAAC;AAuDT,eAAe,2BAA2B,CAAC"}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@knocklabs/react-core",
|
|
3
3
|
"description": "A set of React components to build notification experiences powered by Knock",
|
|
4
4
|
"author": "@knocklabs",
|
|
5
|
-
"version": "0.
|
|
5
|
+
"version": "0.7.0",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"main": "dist/cjs/index.js",
|
|
8
8
|
"module": "dist/esm/index.mjs",
|
|
@@ -47,13 +47,14 @@
|
|
|
47
47
|
"react": "^16.11.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@knocklabs/client": "^0.
|
|
50
|
+
"@knocklabs/client": "^0.15.0",
|
|
51
51
|
"@tanstack/react-store": "^0.7.1",
|
|
52
52
|
"date-fns": "^4.0.0",
|
|
53
53
|
"fast-deep-equal": "^3.1.3",
|
|
54
54
|
"swr": "^2.3.3"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
+
"@codecov/vite-plugin": "^1.9.1",
|
|
57
58
|
"@testing-library/dom": "^10.4.0",
|
|
58
59
|
"@testing-library/react": "^16.3.0",
|
|
59
60
|
"@types/react": "^18.3.6",
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import Knock, {
|
|
1
|
+
import Knock, {
|
|
2
|
+
AuthenticateOptions,
|
|
3
|
+
LogLevel,
|
|
4
|
+
UserWithProperties,
|
|
5
|
+
} from "@knocklabs/client";
|
|
2
6
|
import * as React from "react";
|
|
3
7
|
import { PropsWithChildren } from "react";
|
|
4
8
|
|
|
@@ -11,31 +15,56 @@ export interface KnockProviderState {
|
|
|
11
15
|
|
|
12
16
|
const KnockContext = React.createContext<KnockProviderState | null>(null);
|
|
13
17
|
|
|
14
|
-
export
|
|
18
|
+
export type KnockProviderProps = {
|
|
15
19
|
// Knock client props
|
|
16
20
|
apiKey: string | undefined;
|
|
17
21
|
host?: string;
|
|
18
|
-
// Authentication props
|
|
19
|
-
userId: Knock["userId"];
|
|
20
22
|
userToken?: Knock["userToken"];
|
|
21
23
|
onUserTokenExpiring?: AuthenticateOptions["onUserTokenExpiring"];
|
|
22
24
|
timeBeforeExpirationInMs?: AuthenticateOptions["timeBeforeExpirationInMs"];
|
|
23
25
|
// i18n translations
|
|
24
26
|
i18n?: I18nContent;
|
|
25
27
|
logLevel?: LogLevel;
|
|
26
|
-
}
|
|
28
|
+
} & (
|
|
29
|
+
| {
|
|
30
|
+
/**
|
|
31
|
+
* @deprecated The `userId` prop is deprecated and will be removed in a future version.
|
|
32
|
+
* Please pass the `user` prop instead containing an `id` value.
|
|
33
|
+
* example:
|
|
34
|
+
* ```ts
|
|
35
|
+
* <KnockProvider user={{ id: "user_123" }}></KnockProvider>
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
userId: Knock["userId"];
|
|
39
|
+
user?: never;
|
|
40
|
+
}
|
|
41
|
+
| {
|
|
42
|
+
user: UserWithProperties;
|
|
43
|
+
/**
|
|
44
|
+
* @deprecated The `userId` prop is deprecated and will be removed in a future version.
|
|
45
|
+
* Please pass the `user` prop instead containing an `id` value.
|
|
46
|
+
* example:
|
|
47
|
+
* ```ts
|
|
48
|
+
* <KnockProvider user={{ id: "user_123" }}></KnockProvider>
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
userId?: never;
|
|
52
|
+
}
|
|
53
|
+
);
|
|
27
54
|
|
|
28
55
|
export const KnockProvider: React.FC<PropsWithChildren<KnockProviderProps>> = ({
|
|
29
56
|
apiKey,
|
|
30
57
|
host,
|
|
31
58
|
logLevel,
|
|
32
|
-
userId,
|
|
33
59
|
userToken,
|
|
34
60
|
onUserTokenExpiring,
|
|
35
61
|
timeBeforeExpirationInMs,
|
|
36
62
|
children,
|
|
37
63
|
i18n,
|
|
64
|
+
...props
|
|
38
65
|
}) => {
|
|
66
|
+
const userIdOrUserWithProperties = props?.user || props?.userId;
|
|
67
|
+
|
|
39
68
|
// We memoize the options here so that we don't create a new object on every re-render
|
|
40
69
|
const authenticateOptions = React.useMemo(
|
|
41
70
|
() => ({
|
|
@@ -49,7 +78,7 @@ export const KnockProvider: React.FC<PropsWithChildren<KnockProviderProps>> = ({
|
|
|
49
78
|
|
|
50
79
|
const knock = useAuthenticatedKnockClient(
|
|
51
80
|
apiKey ?? "",
|
|
52
|
-
|
|
81
|
+
userIdOrUserWithProperties,
|
|
53
82
|
userToken,
|
|
54
83
|
authenticateOptions,
|
|
55
84
|
);
|
|
@@ -1,15 +1,20 @@
|
|
|
1
|
-
import Knock, {
|
|
1
|
+
import Knock, {
|
|
2
|
+
AuthenticateOptions,
|
|
3
|
+
KnockOptions,
|
|
4
|
+
UserId,
|
|
5
|
+
UserIdOrUserWithProperties,
|
|
6
|
+
} from "@knocklabs/client";
|
|
2
7
|
import React from "react";
|
|
3
8
|
|
|
4
9
|
import { useStableOptions } from "../../core";
|
|
5
10
|
|
|
6
11
|
function authenticateWithOptions(
|
|
7
12
|
knock: Knock,
|
|
8
|
-
|
|
13
|
+
userIdOrUserWithProperties: UserIdOrUserWithProperties,
|
|
9
14
|
userToken?: Knock["userToken"],
|
|
10
15
|
options: AuthenticateOptions = {},
|
|
11
16
|
) {
|
|
12
|
-
knock.authenticate(
|
|
17
|
+
knock.authenticate(userIdOrUserWithProperties, userToken, {
|
|
13
18
|
onUserTokenExpiring: options?.onUserTokenExpiring,
|
|
14
19
|
timeBeforeExpirationInMs: options?.timeBeforeExpirationInMs,
|
|
15
20
|
});
|
|
@@ -18,9 +23,29 @@ function authenticateWithOptions(
|
|
|
18
23
|
export type AuthenticatedKnockClientOptions = KnockOptions &
|
|
19
24
|
AuthenticateOptions;
|
|
20
25
|
|
|
26
|
+
/**
|
|
27
|
+
* @deprecated Passing `userId` as a `string` is deprecated and will be removed in a future version.
|
|
28
|
+
* Please pass a `user` object instead containing an `id` value.
|
|
29
|
+
* example:
|
|
30
|
+
* ```ts
|
|
31
|
+
* useAuthenticatedKnockClient("pk_test_12345", { id: "user_123" });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
21
34
|
function useAuthenticatedKnockClient(
|
|
22
35
|
apiKey: string,
|
|
23
|
-
|
|
36
|
+
userIdOrUserWithProperties: UserId,
|
|
37
|
+
userToken?: Knock["userToken"],
|
|
38
|
+
options?: AuthenticatedKnockClientOptions,
|
|
39
|
+
): Knock;
|
|
40
|
+
function useAuthenticatedKnockClient(
|
|
41
|
+
apiKey: string,
|
|
42
|
+
userIdOrUserWithProperties: UserIdOrUserWithProperties,
|
|
43
|
+
userToken?: Knock["userToken"],
|
|
44
|
+
options?: AuthenticatedKnockClientOptions,
|
|
45
|
+
): Knock;
|
|
46
|
+
function useAuthenticatedKnockClient(
|
|
47
|
+
apiKey: string,
|
|
48
|
+
userIdOrUserWithProperties: UserIdOrUserWithProperties,
|
|
24
49
|
userToken?: Knock["userToken"],
|
|
25
50
|
options: AuthenticatedKnockClientOptions = {},
|
|
26
51
|
) {
|
|
@@ -28,6 +53,11 @@ function useAuthenticatedKnockClient(
|
|
|
28
53
|
const stableOptions = useStableOptions(options);
|
|
29
54
|
|
|
30
55
|
return React.useMemo(() => {
|
|
56
|
+
const userId =
|
|
57
|
+
typeof userIdOrUserWithProperties === "string"
|
|
58
|
+
? userIdOrUserWithProperties
|
|
59
|
+
: userIdOrUserWithProperties?.id;
|
|
60
|
+
|
|
31
61
|
const currentKnock = knockRef.current;
|
|
32
62
|
|
|
33
63
|
// If the userId and the userToken changes then just reauth
|
|
@@ -36,7 +66,12 @@ function useAuthenticatedKnockClient(
|
|
|
36
66
|
currentKnock.isAuthenticated() &&
|
|
37
67
|
(currentKnock.userId !== userId || currentKnock.userToken !== userToken)
|
|
38
68
|
) {
|
|
39
|
-
authenticateWithOptions(
|
|
69
|
+
authenticateWithOptions(
|
|
70
|
+
currentKnock,
|
|
71
|
+
userIdOrUserWithProperties,
|
|
72
|
+
userToken,
|
|
73
|
+
stableOptions,
|
|
74
|
+
);
|
|
40
75
|
return currentKnock;
|
|
41
76
|
}
|
|
42
77
|
|
|
@@ -50,11 +85,16 @@ function useAuthenticatedKnockClient(
|
|
|
50
85
|
logLevel: stableOptions.logLevel,
|
|
51
86
|
});
|
|
52
87
|
|
|
53
|
-
authenticateWithOptions(
|
|
88
|
+
authenticateWithOptions(
|
|
89
|
+
knock,
|
|
90
|
+
userIdOrUserWithProperties,
|
|
91
|
+
userToken,
|
|
92
|
+
stableOptions,
|
|
93
|
+
);
|
|
54
94
|
knockRef.current = knock;
|
|
55
95
|
|
|
56
96
|
return knock;
|
|
57
|
-
}, [apiKey,
|
|
97
|
+
}, [apiKey, userIdOrUserWithProperties, userToken, stableOptions]);
|
|
58
98
|
}
|
|
59
99
|
|
|
60
100
|
export default useAuthenticatedKnockClient;
|