@stytch/nextjs 11.0.0 → 13.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.
Files changed (85) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/README.md +16 -0
  3. package/b2b/headless/package.json +6 -0
  4. package/b2b/ui/package.json +6 -0
  5. package/dist/Stytch.d.ts +172 -0
  6. package/dist/Stytch.js +214 -0
  7. package/dist/Stytch.js.map +1 -0
  8. package/dist/Stytch.spec.d.ts +1 -0
  9. package/dist/Stytch.spec.js +130 -0
  10. package/dist/Stytch.spec.js.map +1 -0
  11. package/dist/StytchContext.d.ts +104 -0
  12. package/dist/StytchContext.js +180 -0
  13. package/dist/StytchContext.js.map +1 -0
  14. package/dist/StytchContext.serverside.spec.d.ts +4 -0
  15. package/dist/StytchContext.serverside.spec.js +51 -0
  16. package/dist/StytchContext.serverside.spec.js.map +1 -0
  17. package/dist/StytchContext.spec.d.ts +1 -0
  18. package/dist/StytchContext.spec.js +212 -0
  19. package/dist/StytchContext.spec.js.map +1 -0
  20. package/dist/StytchSSRProxy.d.ts +2 -0
  21. package/dist/StytchSSRProxy.js +29 -0
  22. package/dist/StytchSSRProxy.js.map +1 -0
  23. package/dist/StytchSSRProxy.spec.d.ts +1 -0
  24. package/dist/StytchSSRProxy.spec.js +16 -0
  25. package/dist/StytchSSRProxy.spec.js.map +1 -0
  26. package/dist/b2b/Stytch.d.ts +104 -0
  27. package/dist/b2b/Stytch.js +90 -0
  28. package/dist/b2b/Stytch.js.map +1 -0
  29. package/dist/b2b/StytchB2BContext.d.ts +104 -0
  30. package/dist/b2b/StytchB2BContext.js +179 -0
  31. package/dist/b2b/StytchB2BContext.js.map +1 -0
  32. package/dist/b2b/StytchB2BContext.spec.d.ts +1 -0
  33. package/dist/b2b/StytchB2BContext.spec.js +217 -0
  34. package/dist/b2b/StytchB2BContext.spec.js.map +1 -0
  35. package/dist/b2b/createStytchB2BHeadlessClient.d.ts +15 -0
  36. package/dist/b2b/createStytchB2BHeadlessClient.js +26 -0
  37. package/dist/b2b/createStytchB2BHeadlessClient.js.map +1 -0
  38. package/dist/b2b/createStytchB2BUIClient.d.ts +16 -0
  39. package/dist/b2b/createStytchB2BUIClient.js +27 -0
  40. package/dist/b2b/createStytchB2BUIClient.js.map +1 -0
  41. package/dist/b2b/index.d.ts +3 -238
  42. package/dist/b2b/index.esm.d.ts +3 -33
  43. package/dist/b2b/index.esm.js +1 -78
  44. package/dist/b2b/index.headless.d.ts +1 -0
  45. package/dist/b2b/index.headless.esm.d.ts +16 -0
  46. package/dist/b2b/index.headless.esm.js +4118 -0
  47. package/dist/b2b/index.headless.js +18 -0
  48. package/dist/b2b/index.headless.js.map +1 -0
  49. package/dist/b2b/index.js +14 -329
  50. package/dist/b2b/index.js.map +1 -0
  51. package/dist/b2b/index.ui.d.ts +1 -0
  52. package/dist/b2b/index.ui.esm.d.ts +17 -0
  53. package/dist/b2b/index.ui.esm.js +62 -0
  54. package/dist/b2b/index.ui.js +18 -0
  55. package/dist/b2b/index.ui.js.map +1 -0
  56. package/dist/createStytchHeadlessClient.d.ts +15 -0
  57. package/dist/createStytchHeadlessClient.js +26 -0
  58. package/dist/createStytchHeadlessClient.js.map +1 -0
  59. package/dist/createStytchUIClient.d.ts +16 -0
  60. package/dist/createStytchUIClient.js +27 -0
  61. package/dist/createStytchUIClient.js.map +1 -0
  62. package/dist/index.d.ts +3 -244
  63. package/dist/index.esm.d.ts +32 -1
  64. package/dist/index.esm.js +56 -1
  65. package/dist/index.headless.d.ts +1 -16
  66. package/dist/index.headless.js +16 -63
  67. package/dist/index.headless.js.map +1 -0
  68. package/dist/index.js +16 -318
  69. package/dist/index.js.map +1 -0
  70. package/dist/index.ui.d.ts +1 -17
  71. package/dist/index.ui.js +16 -64
  72. package/dist/index.ui.js.map +1 -0
  73. package/dist/utils/async.d.ts +2 -0
  74. package/dist/utils/async.js +22 -0
  75. package/dist/utils/async.js.map +1 -0
  76. package/dist/utils/errors.d.ts +3 -0
  77. package/dist/utils/errors.js +28 -0
  78. package/dist/utils/errors.js.map +1 -0
  79. package/dist/utils/invariant.d.ts +1 -0
  80. package/dist/utils/invariant.js +10 -0
  81. package/dist/utils/invariant.js.map +1 -0
  82. package/dist/utils/useIsomorphicLayoutEffect.d.ts +3 -0
  83. package/dist/utils/useIsomorphicLayoutEffect.js +7 -0
  84. package/dist/utils/useIsomorphicLayoutEffect.js.map +1 -0
  85. package/package.json +3 -3
@@ -0,0 +1,104 @@
1
+ import React from 'react';
2
+ import type { Callbacks, StyleConfig, StytchB2BUIConfig } from '@stytch/vanilla-js';
3
+ export interface StytchB2BProps {
4
+ /**
5
+ * An optional {@link StyleConfig} to customize the look and feel of the screen.
6
+ *
7
+ * @example
8
+ * {
9
+ * fontFamily: 'Arial, Helvetica, sans-serif',
10
+ * width: '360px',
11
+ * primaryColor: '#19303D',
12
+ * }
13
+ */
14
+ styles?: StyleConfig;
15
+ /**
16
+ * An optional {@link Callbacks} object.
17
+ *
18
+ * @example
19
+ * {
20
+ * onError: ({message}) => {
21
+ * console.error('Stytch error', message)
22
+ * }
23
+ * }
24
+ *
25
+ * @example
26
+ * {
27
+ * onEvent: ({type, data}) => {
28
+ * if(type === StytchEventType.B2BMagicLinkAuthenticate) {
29
+ * console.log('Logged in with', data);
30
+ * }
31
+ * }
32
+ * }
33
+ */
34
+ callbacks?: Callbacks;
35
+ /**
36
+ * A {@link StytchB2BUIConfig} object. Add products and product-specific config to this object to change the login methods shown.
37
+ *
38
+ *
39
+ * @example
40
+ * {
41
+ * products: ['emailMagicLinks'],
42
+ * authFlowType: "Discovery",
43
+ * emailMagicLinksOptions: {
44
+ * discoveryRedirectURL: 'https://example.com/authenticate',
45
+ * },
46
+ * sessionOptions: {
47
+ * sessionDurationMinutes: 60,
48
+ * },
49
+ * }
50
+ *
51
+ * @example
52
+ * {
53
+ * products: ['emailMagicLinks', 'sso'],
54
+ * authFlowType: "Organization",
55
+ * emailMagicLinksOptions: {
56
+ * loginRedirectURL: 'https://example.com/authenticate',
57
+ * signupRedirectURL: 'https://example.com/authenticate',
58
+ * },
59
+ * ssoOptions: {
60
+ * loginRedirectURL: 'https://example.com/authenticate',
61
+ * signupRedirectURL: 'https://example.com/authenticate',
62
+ * },
63
+ * sessionOptions: {
64
+ * sessionDurationMinutes: 60,
65
+ * },
66
+ * }
67
+ */
68
+ config: StytchB2BUIConfig;
69
+ }
70
+ /**
71
+ * The Stytch B2B UI component.
72
+ * This component can only be used with a {@link StytchB2BUIClient} client constructor
73
+ * passed into the {@link StytchB2BProvider}
74
+ *
75
+ * See the {@link https://stytch.com/docs/b2b/sdks/javascript-sdk online reference}
76
+ *
77
+ * @example
78
+ * <StytchB2B
79
+ * config={{
80
+ * authFlowType: "Organization",
81
+ * emailMagicLinksOptions: {
82
+ * loginRedirectURL: 'https://example.com/authenticate',
83
+ * signupRedirectURL: 'https://example.com/authenticate',
84
+ * },
85
+ * ssoOptions: {
86
+ * loginRedirectURL: 'https://example.com/authenticate',
87
+ * signupRedirectURL: 'https://example.com/authenticate',
88
+ * },
89
+ * sessionOptions: {
90
+ * sessionDurationMinutes: 60,
91
+ * }
92
+ * }}
93
+ * styles={{
94
+ * fontFamily: '"Helvetica New", Helvetica, sans-serif',
95
+ * primaryColor: '#0577CA',
96
+ * width: '321px',
97
+ * }}
98
+ * callbacks={{
99
+ * onEvent: (event) => console.log(event)
100
+ * }}
101
+ * />
102
+ * @param props {@link StytchB2BProps}
103
+ */
104
+ export declare const StytchB2B: ({ styles, callbacks, config }: StytchB2BProps) => React.JSX.Element;
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.StytchB2B = void 0;
27
+ const react_1 = __importStar(require("react"));
28
+ const StytchB2BContext_1 = require("./StytchB2BContext");
29
+ const invariant_1 = require("../utils/invariant");
30
+ const errors_1 = require("../utils/errors");
31
+ /**
32
+ * The Stytch B2B UI component.
33
+ * This component can only be used with a {@link StytchB2BUIClient} client constructor
34
+ * passed into the {@link StytchB2BProvider}
35
+ *
36
+ * See the {@link https://stytch.com/docs/b2b/sdks/javascript-sdk online reference}
37
+ *
38
+ * @example
39
+ * <StytchB2B
40
+ * config={{
41
+ * authFlowType: "Organization",
42
+ * emailMagicLinksOptions: {
43
+ * loginRedirectURL: 'https://example.com/authenticate',
44
+ * signupRedirectURL: 'https://example.com/authenticate',
45
+ * },
46
+ * ssoOptions: {
47
+ * loginRedirectURL: 'https://example.com/authenticate',
48
+ * signupRedirectURL: 'https://example.com/authenticate',
49
+ * },
50
+ * sessionOptions: {
51
+ * sessionDurationMinutes: 60,
52
+ * }
53
+ * }}
54
+ * styles={{
55
+ * fontFamily: '"Helvetica New", Helvetica, sans-serif',
56
+ * primaryColor: '#0577CA',
57
+ * width: '321px',
58
+ * }}
59
+ * callbacks={{
60
+ * onEvent: (event) => console.log(event)
61
+ * }}
62
+ * />
63
+ * @param props {@link StytchB2BProps}
64
+ */
65
+ const StytchB2B = ({ styles, callbacks, config }) => {
66
+ (0, invariant_1.invariant)((0, StytchB2BContext_1.useIsMounted__INTERNAL)(), (0, errors_1.noProviderError)('<StytchB2B />'));
67
+ const stytchClient = (0, StytchB2BContext_1.useStytchB2BClient)();
68
+ const containerEl = (0, react_1.useRef)(null);
69
+ (0, react_1.useLayoutEffect)(() => {
70
+ if (!(0, StytchB2BContext_1.isUIClient)(stytchClient)) {
71
+ throw Error(errors_1.noHeadlessClientError);
72
+ }
73
+ if (!containerEl.current) {
74
+ return;
75
+ }
76
+ if (!containerEl.current.id) {
77
+ const randId = Math.floor(Math.random() * 1e6);
78
+ containerEl.current.id = `stytch-b2b-ui-${randId}`;
79
+ }
80
+ stytchClient.mount({
81
+ callbacks,
82
+ config,
83
+ elementId: `#${containerEl.current.id}`,
84
+ styles,
85
+ });
86
+ }, [stytchClient, styles, callbacks]);
87
+ return react_1.default.createElement("div", { ref: containerEl });
88
+ };
89
+ exports.StytchB2B = StytchB2B;
90
+ //# sourceMappingURL=Stytch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Stytch.js","sourceRoot":"","sources":["../../src/b2b/Stytch.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+CAAuD;AAQvD,yDAA4F;AAC5F,kDAA+C;AAC/C,4CAAyE;AAsEzE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACI,MAAM,SAAS,GAAG,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAkB,EAAE,EAAE;IACzE,IAAA,qBAAS,EAAC,IAAA,yCAAsB,GAAE,EAAE,IAAA,wBAAe,EAAC,eAAe,CAAC,CAAC,CAAC;IACtE,MAAM,YAAY,GAAG,IAAA,qCAAkB,GAAE,CAAC;IAC1C,MAAM,WAAW,GAAG,IAAA,cAAM,EAAiB,IAAI,CAAC,CAAC;IAEjD,IAAA,uBAAe,EAAC,GAAG,EAAE;QACnB,IAAI,CAAC,IAAA,6BAAU,EAAC,YAAY,CAAC,EAAE;YAC7B,MAAM,KAAK,CAAC,8BAAqB,CAAC,CAAC;SACpC;QAED,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE;YACxB,OAAO;SACR;QAED,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,EAAE,EAAE;YAC3B,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,GAAG,CAAC,CAAC;YAC/C,WAAW,CAAC,OAAO,CAAC,EAAE,GAAG,iBAAiB,MAAM,EAAE,CAAC;SACpD;QAED,YAAY,CAAC,KAAK,CAAC;YACjB,SAAS;YACT,MAAM;YACN,SAAS,EAAE,IAAI,WAAW,CAAC,OAAO,CAAC,EAAE,EAAE;YACvC,MAAM;SACP,CAAC,CAAC;IACL,CAAC,EAAE,CAAC,YAAY,EAAE,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC;IAEtC,OAAO,uCAAK,GAAG,EAAE,WAAW,GAAI,CAAC;AACnC,CAAC,CAAC;AA5BW,QAAA,SAAS,aA4BpB"}
@@ -0,0 +1,104 @@
1
+ import React, { ComponentType, ReactNode } from 'react';
2
+ import type { Member, MemberSession, StytchB2BUIClient } from '@stytch/vanilla-js/b2b';
3
+ import type { StytchB2BHeadlessClient } from '@stytch/vanilla-js/b2b/headless';
4
+ /**
5
+ * The Stytch Client object passed in to <StytchProvider /> in your `_app.js`.
6
+ * Either a StytchUIClient or StytchHeadlessClient.
7
+ */
8
+ declare type StytchB2BClient = StytchB2BHeadlessClient | StytchB2BUIClient;
9
+ declare type SWRMember = {
10
+ member: null;
11
+ fromCache: false;
12
+ isInitialized: false;
13
+ } | {
14
+ member: Member | null;
15
+ fromCache: boolean;
16
+ isInitialized: true;
17
+ };
18
+ declare type SWRMemberSession = {
19
+ session: null;
20
+ fromCache: false;
21
+ isInitialized: false;
22
+ } | {
23
+ session: MemberSession | null;
24
+ fromCache: boolean;
25
+ isInitialized: true;
26
+ };
27
+ export declare const useIsMounted__INTERNAL: () => boolean;
28
+ export declare const isUIClient: (client: StytchB2BClient) => client is StytchB2BUIClient;
29
+ /**
30
+ * Returns the active member.
31
+ * The Stytch SDKs are used for client-side authentication and session management.
32
+ * Check the isInitialized property to determine if the SDK has completed initialization.
33
+ * Check the fromCache property to determine if the session data is from persistent storage.
34
+ * See Next's {@link https://nextjs.org/docs/authentication#authenticating-statically-generated-pages documentation} for more.
35
+ * @example
36
+ * const {member, isInitialized, fromCache} = useStytchMember();
37
+ * if (!isInitialized) {
38
+ * return <p>Loading...</p>;
39
+ * }
40
+ * return (<h1>Welcome, {member.name}</h1>);
41
+ */
42
+ export declare const useStytchMember: () => SWRMember;
43
+ /**
44
+ * Returns the member's active Stytch member session.
45
+ * The Stytch SDKs are used for client-side authentication and session management.
46
+ * Check the isInitialized property to determine if the SDK has completed initialization.
47
+ * Check the fromCache property to determine if the session data is from persistent storage.
48
+ * See Next's {@link https://nextjs.org/docs/authentication#authenticating-statically-generated-pages documentation} for more.
49
+ * @example
50
+ * const {session, isInitialized, fromCache} = useStytchMemberSession();
51
+ * useEffect(() => {
52
+ * if (!isInitialized) {
53
+ * return;
54
+ * }
55
+ * if (!session) {
56
+ * router.replace('/login')
57
+ * }
58
+ * }, [session, isInitialized]);
59
+ */
60
+ export declare const useStytchMemberSession: () => SWRMemberSession;
61
+ /**
62
+ * Returns the Stytch client stored in the Stytch context.
63
+ *
64
+ * @example
65
+ * const stytch = useStytch();
66
+ * useEffect(() => {
67
+ * stytch.magicLinks.authenticate('...')
68
+ * }, [stytch]);
69
+ */
70
+ export declare const useStytchB2BClient: () => StytchB2BClient;
71
+ export declare const withStytchB2BClient: <T extends object>(Component: React.ComponentType<T & {
72
+ stytch: StytchB2BClient;
73
+ }>) => React.ComponentType<T>;
74
+ export declare const withStytchMember: <T extends object>(Component: React.ComponentType<T & {
75
+ stytchMember: Member | null;
76
+ stytchMemberIsInitialized: boolean;
77
+ stytchMemberIsFromCache: boolean;
78
+ }>) => React.ComponentType<T>;
79
+ export declare const withStytchMemberSession: <T extends object>(Component: React.ComponentType<T & {
80
+ stytchMemberSession: MemberSession | null;
81
+ stytchMemberSessionIsInitialized: boolean;
82
+ stytchMemberSessionIsFromCache: boolean;
83
+ }>) => React.ComponentType<T>;
84
+ export interface StytchB2BProviderProps {
85
+ /**
86
+ * A Stytch client instance, created using either {@link createStytchHeadlessClient} or {@link createStytchUIClient}
87
+ */
88
+ stytch: StytchB2BClient;
89
+ children?: ReactNode;
90
+ }
91
+ /**
92
+ * The Stytch Context Provider.
93
+ * Wrap your application with this component in `_app.js` in order to use Stytch everywhere in your app.
94
+ * @example
95
+ * const stytch = createStytchB2BHeadlessClient('public-token-<find yours in the stytch dashboard>')
96
+ *
97
+ * return (
98
+ * <StytchB2BProvider stytch={stytch}>
99
+ * <App />
100
+ * </StytchB2BProvider>
101
+ * )
102
+ */
103
+ export declare const StytchB2BProvider: ({ stytch, children }: StytchB2BProviderProps) => JSX.Element;
104
+ export {};
@@ -0,0 +1,179 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.StytchB2BProvider = exports.withStytchMemberSession = exports.withStytchMember = exports.withStytchB2BClient = exports.useStytchB2BClient = exports.useStytchMemberSession = exports.useStytchMember = exports.isUIClient = exports.useIsMounted__INTERNAL = void 0;
27
+ const react_1 = __importStar(require("react"));
28
+ const errors_1 = require("../utils/errors");
29
+ const invariant_1 = require("../utils/invariant");
30
+ const async_1 = require("../utils/async");
31
+ const StytchSSRProxy_1 = require("../StytchSSRProxy");
32
+ const initialMember = {
33
+ member: null,
34
+ fromCache: false,
35
+ isInitialized: false,
36
+ };
37
+ const initialMemberSession = {
38
+ session: null,
39
+ fromCache: false,
40
+ isInitialized: false,
41
+ };
42
+ const StytchContext = (0, react_1.createContext)({ isMounted: false });
43
+ const StytchMemberContext = (0, react_1.createContext)(initialMember);
44
+ const StytchMemberSessionContext = (0, react_1.createContext)(initialMemberSession);
45
+ const useIsMounted__INTERNAL = () => (0, react_1.useContext)(StytchContext).isMounted;
46
+ exports.useIsMounted__INTERNAL = useIsMounted__INTERNAL;
47
+ const isUIClient = (client) => {
48
+ return client.mount !== undefined;
49
+ };
50
+ exports.isUIClient = isUIClient;
51
+ /**
52
+ * Returns the active member.
53
+ * The Stytch SDKs are used for client-side authentication and session management.
54
+ * Check the isInitialized property to determine if the SDK has completed initialization.
55
+ * Check the fromCache property to determine if the session data is from persistent storage.
56
+ * See Next's {@link https://nextjs.org/docs/authentication#authenticating-statically-generated-pages documentation} for more.
57
+ * @example
58
+ * const {member, isInitialized, fromCache} = useStytchMember();
59
+ * if (!isInitialized) {
60
+ * return <p>Loading...</p>;
61
+ * }
62
+ * return (<h1>Welcome, {member.name}</h1>);
63
+ */
64
+ const useStytchMember = () => {
65
+ (0, invariant_1.invariant)((0, exports.useIsMounted__INTERNAL)(), (0, errors_1.noProviderError)('useStytchMember', 'StytchB2BProvider'));
66
+ return (0, react_1.useContext)(StytchMemberContext);
67
+ };
68
+ exports.useStytchMember = useStytchMember;
69
+ /**
70
+ * Returns the member's active Stytch member session.
71
+ * The Stytch SDKs are used for client-side authentication and session management.
72
+ * Check the isInitialized property to determine if the SDK has completed initialization.
73
+ * Check the fromCache property to determine if the session data is from persistent storage.
74
+ * See Next's {@link https://nextjs.org/docs/authentication#authenticating-statically-generated-pages documentation} for more.
75
+ * @example
76
+ * const {session, isInitialized, fromCache} = useStytchMemberSession();
77
+ * useEffect(() => {
78
+ * if (!isInitialized) {
79
+ * return;
80
+ * }
81
+ * if (!session) {
82
+ * router.replace('/login')
83
+ * }
84
+ * }, [session, isInitialized]);
85
+ */
86
+ const useStytchMemberSession = () => {
87
+ (0, invariant_1.invariant)((0, exports.useIsMounted__INTERNAL)(), (0, errors_1.noProviderError)('useStytchMemberSession', 'StytchB2BProvider'));
88
+ return (0, react_1.useContext)(StytchMemberSessionContext);
89
+ };
90
+ exports.useStytchMemberSession = useStytchMemberSession;
91
+ /**
92
+ * Returns the Stytch client stored in the Stytch context.
93
+ *
94
+ * @example
95
+ * const stytch = useStytch();
96
+ * useEffect(() => {
97
+ * stytch.magicLinks.authenticate('...')
98
+ * }, [stytch]);
99
+ */
100
+ const useStytchB2BClient = () => {
101
+ const ctx = (0, react_1.useContext)(StytchContext);
102
+ (0, invariant_1.invariant)(ctx.isMounted, (0, errors_1.noProviderError)('useStytchB2BClient', 'StytchB2BProvider'));
103
+ return ctx.client;
104
+ };
105
+ exports.useStytchB2BClient = useStytchB2BClient;
106
+ const withStytchB2BClient = (Component) => {
107
+ const WithStytch = (props) => {
108
+ (0, invariant_1.invariant)((0, exports.useIsMounted__INTERNAL)(), (0, errors_1.noProviderError)('withStytchB2BClient', 'StytchB2BProvider'));
109
+ return react_1.default.createElement(Component, Object.assign({}, props, { stytch: (0, exports.useStytchB2BClient)() }));
110
+ };
111
+ WithStytch.displayName = `withStytch(${Component.displayName || Component.name || 'Component'})`;
112
+ return WithStytch;
113
+ };
114
+ exports.withStytchB2BClient = withStytchB2BClient;
115
+ const withStytchMember = (Component) => {
116
+ const WithStytchUser = (props) => {
117
+ (0, invariant_1.invariant)((0, exports.useIsMounted__INTERNAL)(), (0, errors_1.noProviderError)('withStytchMember', 'StytchB2BProvider'));
118
+ const { member, isInitialized, fromCache } = (0, exports.useStytchMember)();
119
+ return (react_1.default.createElement(Component, Object.assign({}, props, { stytchMember: member, stytchMemberIsInitialized: isInitialized, stytchMemberIsFromCache: fromCache })));
120
+ };
121
+ WithStytchUser.displayName = `withStytchMember(${Component.displayName || Component.name || 'Component'})`;
122
+ return WithStytchUser;
123
+ };
124
+ exports.withStytchMember = withStytchMember;
125
+ const withStytchMemberSession = (Component) => {
126
+ const WithStytchSession = (props) => {
127
+ (0, invariant_1.invariant)((0, exports.useIsMounted__INTERNAL)(), (0, errors_1.noProviderError)('withStytchMemberSession', 'StytchB2BProvider'));
128
+ const { session, isInitialized, fromCache } = (0, exports.useStytchMemberSession)();
129
+ return (react_1.default.createElement(Component, Object.assign({}, props, { stytchMemberSession: session, stytchMemberSessionIsInitialized: isInitialized, stytchMemberSessionIsFromCache: fromCache })));
130
+ };
131
+ WithStytchSession.displayName = `withStytchMemberSession(${Component.displayName || Component.name || 'Component'})`;
132
+ return WithStytchSession;
133
+ };
134
+ exports.withStytchMemberSession = withStytchMemberSession;
135
+ /**
136
+ * The Stytch Context Provider.
137
+ * Wrap your application with this component in `_app.js` in order to use Stytch everywhere in your app.
138
+ * @example
139
+ * const stytch = createStytchB2BHeadlessClient('public-token-<find yours in the stytch dashboard>')
140
+ *
141
+ * return (
142
+ * <StytchB2BProvider stytch={stytch}>
143
+ * <App />
144
+ * </StytchB2BProvider>
145
+ * )
146
+ */
147
+ const StytchB2BProvider = ({ stytch, children }) => {
148
+ const ctx = (0, react_1.useMemo)(() => ({ client: stytch, isMounted: true }), [stytch]);
149
+ const [member, setMember] = (0, async_1.useAsyncState)(initialMember);
150
+ const [session, setMemberSession] = (0, async_1.useAsyncState)(initialMemberSession);
151
+ (0, react_1.useEffect)(() => {
152
+ if ((0, StytchSSRProxy_1.isStytchSSRProxy)(stytch)) {
153
+ return;
154
+ }
155
+ setMember({
156
+ member: stytch.member.getSync(),
157
+ fromCache: true,
158
+ isInitialized: true,
159
+ });
160
+ setMemberSession({
161
+ session: stytch.session.getSync(),
162
+ fromCache: true,
163
+ isInitialized: true,
164
+ });
165
+ const unsubscribeMember = stytch.member.onChange((member) => setMember({ member, fromCache: false, isInitialized: true }));
166
+ const unsubscribeMemberSession = stytch.session.onChange((session) => setMemberSession({ session, fromCache: false, isInitialized: true }));
167
+ return () => {
168
+ unsubscribeMember();
169
+ unsubscribeMemberSession();
170
+ };
171
+ }, [stytch, setMember, setMemberSession]);
172
+ const finalMemberSession = !!session.session === !!member.member ? session : initialMemberSession;
173
+ const finalMember = !!session.session === !!member.member ? member : initialMember;
174
+ return (react_1.default.createElement(StytchContext.Provider, { value: ctx },
175
+ react_1.default.createElement(StytchMemberContext.Provider, { value: finalMember },
176
+ react_1.default.createElement(StytchMemberSessionContext.Provider, { value: finalMemberSession }, children))));
177
+ };
178
+ exports.StytchB2BProvider = StytchB2BProvider;
179
+ //# sourceMappingURL=StytchB2BContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StytchB2BContext.js","sourceRoot":"","sources":["../../src/b2b/StytchB2BContext.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+CAAuG;AAIvG,4CAAkD;AAClD,kDAA+C;AAC/C,0CAA+C;AAC/C,sDAAqD;AA6BrD,MAAM,aAAa,GAAc;IAC/B,MAAM,EAAE,IAAI;IACZ,SAAS,EAAE,KAAK;IAChB,aAAa,EAAE,KAAK;CACrB,CAAC;AAcF,MAAM,oBAAoB,GAAqB;IAC7C,OAAO,EAAE,IAAI;IACb,SAAS,EAAE,KAAK;IAChB,aAAa,EAAE,KAAK;CACrB,CAAC;AAEF,MAAM,aAAa,GAAG,IAAA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