@stytch/react 19.4.4 → 19.5.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 (49) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/README.md +7 -7
  3. package/b2b/headless/package.json +6 -0
  4. package/b2b/ui/package.json +6 -0
  5. package/dist/{StytchB2BContext-09d376ba.d.ts → StytchB2BContext-9c062712.d.ts} +127 -23
  6. package/dist/{StytchB2BContext-09d376ba.js → StytchB2BContext-9c062712.js} +85 -42
  7. package/dist/{StytchB2BContext-a8d57249.d.ts → StytchB2BContext-c5062f9b.d.ts} +127 -23
  8. package/dist/{StytchB2BContext-a8d57249.js → StytchB2BContext-c5062f9b.js} +95 -52
  9. package/dist/StytchSSRProxy-34c789b5.d.ts +3 -0
  10. package/dist/StytchSSRProxy-34c789b5.js +48 -0
  11. package/dist/StytchSSRProxy-86bc42b3.d.ts +3 -0
  12. package/dist/StytchSSRProxy-86bc42b3.js +54 -0
  13. package/dist/adminPortal/index.d.ts +1 -1
  14. package/dist/adminPortal/index.esm.d.ts +1 -1
  15. package/dist/adminPortal/index.esm.js +5 -4
  16. package/dist/adminPortal/index.js +5 -4
  17. package/dist/b2b/index.d.ts +170 -6
  18. package/dist/b2b/index.esm.d.ts +170 -6
  19. package/dist/b2b/index.esm.js +85 -8
  20. package/dist/b2b/index.headless.d.ts +16 -0
  21. package/dist/b2b/index.headless.esm.d.ts +16 -0
  22. package/dist/b2b/index.headless.esm.js +24 -0
  23. package/dist/b2b/index.headless.js +28 -0
  24. package/dist/b2b/index.js +93 -16
  25. package/dist/b2b/index.ui.d.ts +17 -0
  26. package/dist/b2b/index.ui.esm.d.ts +17 -0
  27. package/dist/b2b/index.ui.esm.js +25 -0
  28. package/dist/b2b/index.ui.js +29 -0
  29. package/dist/errors-d9d5fbc8.d.ts +5 -0
  30. package/dist/index-b14d4efe.d.ts +1 -1
  31. package/dist/index.d.ts +92 -54
  32. package/dist/index.esm.d.ts +92 -54
  33. package/dist/index.esm.js +112 -38
  34. package/dist/index.headless.d.ts +16 -0
  35. package/dist/index.headless.esm.d.ts +16 -0
  36. package/dist/index.headless.esm.js +24 -0
  37. package/dist/index.headless.js +28 -0
  38. package/dist/index.js +126 -52
  39. package/dist/index.ui.d.ts +17 -0
  40. package/dist/index.ui.esm.d.ts +17 -0
  41. package/dist/index.ui.esm.js +25 -0
  42. package/dist/index.ui.js +29 -0
  43. package/dist/useIsomorphicLayoutEffect-1babb81e.d.ts +24 -0
  44. package/dist/{invariant-568a7633.js → useIsomorphicLayoutEffect-1babb81e.js} +5 -13
  45. package/dist/useIsomorphicLayoutEffect-65746ef3.d.ts +24 -0
  46. package/dist/{invariant-ae5a5bce.js → useIsomorphicLayoutEffect-65746ef3.js} +4 -16
  47. package/package.json +4 -3
  48. package/dist/invariant-568a7633.d.ts +0 -27
  49. package/dist/invariant-ae5a5bce.d.ts +0 -27
package/dist/index.esm.js CHANGED
@@ -1,13 +1,16 @@
1
- import React, { createContext, useContext, useMemo, useEffect, useRef, useLayoutEffect } from 'react';
2
- import { i as invariant, u as useAsyncState, m as mergeWithStableProps, n as noProviderError, p as providerMustBeUniqueError, a as noSSRError, b as noHeadlessClientError } from './invariant-568a7633.js';
1
+ import React, { createContext, useContext, useMemo, useCallback, useRef, useEffect } from 'react';
2
+ import { i as invariant, u as useAsyncState, m as mergeWithStableProps, a as useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect-1babb81e.js';
3
+ import { n as noProviderError, p as providerMustBeUniqueError, i as isStytchSSRProxy, a as noHeadlessClientError } from './StytchSSRProxy-34c789b5.js';
3
4
 
4
5
  const initialUser = {
5
6
  user: null,
6
7
  fromCache: false,
8
+ isInitialized: false,
7
9
  };
8
10
  const initialSession = {
9
11
  session: null,
10
12
  fromCache: false,
13
+ isInitialized: false,
11
14
  };
12
15
  const StytchContext = createContext({ isMounted: false });
13
16
  const StytchUserContext = createContext(initialUser);
@@ -19,28 +22,36 @@ const isUIClient = (client) => {
19
22
  /**
20
23
  * Returns the active User.
21
24
  * The Stytch SDKs are used for client-side authentication and session management.
25
+ * Check the isInitialized property to determine if the SDK has completed initialization.
22
26
  * Check the fromCache property to determine if the user data is from persistent storage.
23
27
  * @example
24
- * const {user} = useStytchUser();
28
+ * const {user, isInitialized, fromCache} = useStytchUser();
29
+ * if (!isInitialized) {
30
+ * return <p>Loading...</p>;
31
+ * }
25
32
  * return (<h1>Welcome, {user.name.first_name}</h1>);
26
33
  */
27
- const useStytchUser = () => {
34
+ const useStytchUser$1 = () => {
28
35
  invariant(useIsMounted__INTERNAL(), noProviderError('useStytchUser'));
29
36
  return useContext(StytchUserContext);
30
37
  };
31
38
  /**
32
39
  * Returns the active user's Stytch session.
33
40
  * The Stytch SDKs are used for client-side authentication and session management.
41
+ * Check the isInitialized property to determine if the SDK has completed initialization.
34
42
  * Check the fromCache property to determine if the session data is from persistent storage.
35
43
  * @example
36
- * const {session} = useStytchSession();
44
+ * const {session, isInitialized, fromCache} = useStytchSession();
37
45
  * useEffect(() => {
46
+ * if (!isInitialized) {
47
+ * return;
48
+ * }
38
49
  * if (!session) {
39
50
  * router.replace('/login')
40
51
  * }
41
- * }, [session]);
52
+ * }, [session, isInitialized]);
42
53
  */
43
- const useStytchSession = () => {
54
+ const useStytchSession$1 = () => {
44
55
  invariant(useIsMounted__INTERNAL(), noProviderError('useStytchSession'));
45
56
  return useContext(StytchSessionContext);
46
57
  };
@@ -66,20 +77,20 @@ const withStytch = (Component) => {
66
77
  WithStytch.displayName = `withStytch(${Component.displayName || Component.name || 'Component'})`;
67
78
  return WithStytch;
68
79
  };
69
- const withStytchUser = (Component) => {
80
+ const withStytchUser$1 = (Component) => {
70
81
  const WithStytchUser = (props) => {
71
82
  invariant(useIsMounted__INTERNAL(), noProviderError('withStytchUser'));
72
- const { user, fromCache } = useStytchUser();
73
- return React.createElement(Component, Object.assign({}, props, { stytchUser: user, stytchUserIsFromCache: fromCache }));
83
+ const { user, isInitialized, fromCache } = useStytchUser$1();
84
+ return (React.createElement(Component, Object.assign({}, props, { stytchUser: user, stytchUserIsInitialized: isInitialized, stytchUserIsFromCache: fromCache })));
74
85
  };
75
86
  WithStytchUser.displayName = `withStytchUser(${Component.displayName || Component.name || 'Component'})`;
76
87
  return WithStytchUser;
77
88
  };
78
- const withStytchSession = (Component) => {
89
+ const withStytchSession$1 = (Component) => {
79
90
  const WithStytchSession = (props) => {
80
91
  invariant(useIsMounted__INTERNAL(), noProviderError('withStytchSession'));
81
- const { session, fromCache } = useStytchSession();
82
- return React.createElement(Component, Object.assign({}, props, { stytchSession: session, stytchSessionIsFromCache: fromCache }));
92
+ const { session, isInitialized, fromCache } = useStytchSession$1();
93
+ return (React.createElement(Component, Object.assign({}, props, { stytchSession: session, stytchSessionIsInitialized: isInitialized, stytchSessionIsFromCache: fromCache })));
83
94
  };
84
95
  WithStytchSession.displayName = `withStytchSession(${Component.displayName || Component.name || 'Component'})`;
85
96
  return WithStytchSession;
@@ -88,32 +99,46 @@ const withStytchSession = (Component) => {
88
99
  * The Stytch Context Provider.
89
100
  * Wrap your application with this component in order to use Stytch everywhere in your app.
90
101
  * @example
91
- * const stytch = new StytchHeadlessClient('public-token-<find yours in the stytch dashboard>')
102
+ * const stytch = createStytchHeadlessClient('public-token-<find yours in the stytch dashboard>')
92
103
  *
93
- * ReactDOM.render(
104
+ * return (
94
105
  * <StytchProvider stytch={stytch}>
95
106
  * <App />
96
- * </StytchProvider>,
97
- * document.getElementById('root'),
107
+ * </StytchProvider>
98
108
  * )
99
109
  */
100
- const StytchProvider = ({ stytch, children, }) => {
110
+ const StytchProvider$1 = ({ stytch, children, assumeHydrated = false, }) => {
101
111
  invariant(!useIsMounted__INTERNAL(), providerMustBeUniqueError);
102
- invariant(typeof window !== 'undefined', noSSRError);
112
+ invariant(!assumeHydrated || typeof window !== 'undefined', 'The `assumeHydrated` prop must be set to `false` when using StytchProvider in a server environment.');
103
113
  const ctx = useMemo(() => ({ client: stytch, isMounted: true }), [stytch]);
104
- const [{ user, session }, setClientState] = useAsyncState({
105
- session: stytch.session.getInfo(),
106
- user: stytch.user.getInfo(),
107
- });
108
- useEffect(() => stytch.onStateChange(() => {
109
- setClientState((oldState) => {
110
- const newState = {
111
- session: stytch.session.getInfo(),
112
- user: stytch.user.getInfo(),
113
- };
114
- return mergeWithStableProps(oldState, newState);
115
- });
116
- }), [setClientState, stytch]);
114
+ const getHydratedState = useCallback(() => {
115
+ return {
116
+ session: Object.assign(Object.assign({}, stytch.session.getInfo()), { isInitialized: true }),
117
+ user: Object.assign(Object.assign({}, stytch.user.getInfo()), { isInitialized: true }),
118
+ };
119
+ }, [stytch]);
120
+ const getInitialState = () => {
121
+ return {
122
+ session: initialSession,
123
+ user: initialUser,
124
+ };
125
+ };
126
+ const [{ user, session }, setClientState] = useAsyncState(() => assumeHydrated ? getHydratedState() : getInitialState());
127
+ // Store the initial value of `assumeHydrated` in a ref, because it is
128
+ // logically only relevant for the first render
129
+ const assumeHydratedRef = useRef(assumeHydrated);
130
+ useEffect(() => {
131
+ if (isStytchSSRProxy(stytch)) {
132
+ return;
133
+ }
134
+ const updateState = () => {
135
+ setClientState((oldState) => mergeWithStableProps(oldState, getHydratedState()));
136
+ };
137
+ if (!assumeHydratedRef.current) {
138
+ updateState();
139
+ }
140
+ return stytch.onStateChange(updateState);
141
+ }, [getHydratedState, setClientState, stytch]);
117
142
  return (React.createElement(StytchContext.Provider, { value: ctx },
118
143
  React.createElement(StytchUserContext.Provider, { value: user },
119
144
  React.createElement(StytchSessionContext.Provider, { value: session }, children))));
@@ -152,7 +177,7 @@ const StytchLogin = ({ config, styles, callbacks, }) => {
152
177
  invariant(useIsMounted__INTERNAL(), noProviderError('<StytchLogin />'));
153
178
  const stytchClient = useStytch();
154
179
  const containerEl = useRef(null);
155
- useLayoutEffect(() => {
180
+ useIsomorphicLayoutEffect(() => {
156
181
  if (!isUIClient(stytchClient)) {
157
182
  throw Error(noHeadlessClientError);
158
183
  }
@@ -206,7 +231,7 @@ const StytchPasswordReset = ({ config, styles, callbacks, passwordResetToken, })
206
231
  invariant(useIsMounted__INTERNAL(), noProviderError('<StytchResetPassword />'));
207
232
  const stytchClient = useStytch();
208
233
  const containerEl = useRef(null);
209
- useLayoutEffect(() => {
234
+ useIsomorphicLayoutEffect(() => {
210
235
  if (!isUIClient(stytchClient)) {
211
236
  throw Error(noHeadlessClientError);
212
237
  }
@@ -257,9 +282,9 @@ const StytchPasswordReset = ({ config, styles, callbacks, passwordResetToken, })
257
282
  const StytchPasskeyRegistration = ({ config, styles, callbacks, }) => {
258
283
  invariant(useIsMounted__INTERNAL(), noProviderError('<StytchPasskeyRegistration />'));
259
284
  const stytchClient = useStytch();
260
- const user = useStytchUser();
285
+ const user = useStytchUser$1();
261
286
  const containerEl = useRef(null);
262
- useLayoutEffect(() => {
287
+ useIsomorphicLayoutEffect(() => {
263
288
  if (!isUIClient(stytchClient)) {
264
289
  throw Error(noHeadlessClientError);
265
290
  }
@@ -309,9 +334,9 @@ const StytchPasskeyRegistration = ({ config, styles, callbacks, }) => {
309
334
  const IdentityProvider = ({ styles, callbacks }) => {
310
335
  invariant(useIsMounted__INTERNAL(), noProviderError('<IdentityProvider />'));
311
336
  const stytchClient = useStytch();
312
- const user = useStytchUser();
337
+ const user = useStytchUser$1();
313
338
  const containerEl = useRef(null);
314
- useLayoutEffect(() => {
339
+ useIsomorphicLayoutEffect(() => {
315
340
  if (!isUIClient(stytchClient)) {
316
341
  throw Error(noHeadlessClientError);
317
342
  }
@@ -331,4 +356,53 @@ const IdentityProvider = ({ styles, callbacks }) => {
331
356
  return React.createElement("div", { ref: containerEl });
332
357
  };
333
358
 
359
+ /**
360
+ * Returns the active User.
361
+ * The Stytch SDKs are used for client-side authentication and session management.
362
+ * Check the isInitialized property to determine if the SDK has completed initialization.
363
+ * Check the fromCache property to determine if the user data is from persistent storage.
364
+ * @example
365
+ * const {user, isInitialized, fromCache} = useStytchUser();
366
+ * if (!isInitialized) {
367
+ * return <p>Loading...</p>;
368
+ * }
369
+ * return (<h1>Welcome, {user.name.first_name}</h1>);
370
+ */
371
+ const useStytchUser = useStytchUser$1;
372
+ /**
373
+ * Returns the active user's Stytch session.
374
+ * The Stytch SDKs are used for client-side authentication and session management.
375
+ * Check the isInitialized property to determine if the SDK has completed initialization.
376
+ * Check the fromCache property to determine if the session data is from persistent storage.
377
+ * @example
378
+ * const {session, isInitialized, fromCache} = useStytchSession();
379
+ * useEffect(() => {
380
+ * if (!isInitialized) {
381
+ * return;
382
+ * }
383
+ * if (!session) {
384
+ * router.replace('/login')
385
+ * }
386
+ * }, [session, isInitialized]);
387
+ */
388
+ const useStytchSession = useStytchSession$1;
389
+ const withStytchUser = withStytchUser$1;
390
+ const withStytchSession = withStytchSession$1;
391
+ /**
392
+ * The Stytch Context Provider.
393
+ * Wrap your application with this component in order to use Stytch everywhere in your app.
394
+ * @example
395
+ * const stytch = createStytchHeadlessClient('public-token-<find yours in the stytch dashboard>')
396
+ *
397
+ * ReactDOM.render(
398
+ * <StytchProvider stytch={stytch}>
399
+ * <App />
400
+ * </StytchProvider>,
401
+ * document.getElementById('root'),
402
+ * )
403
+ */
404
+ const StytchProvider = ({ stytch, children, assumeHydrated = true, }) => {
405
+ return (React.createElement(StytchProvider$1, { stytch: stytch, assumeHydrated: assumeHydrated }, children));
406
+ };
407
+
334
408
  export { IdentityProvider, StytchLogin, StytchPasskeyRegistration, StytchPasswordReset, StytchProvider, useStytch, useStytchSession, useStytchUser, withStytch, withStytchSession, withStytchUser };
@@ -0,0 +1,16 @@
1
+ import { StytchHeadlessClient } from "@stytch/vanilla-js/headless";
2
+ /**
3
+ * Creates a Headless Stytch client object to call the stytch APIs.
4
+ * The Stytch client is not available serverside.
5
+ * @example
6
+ * const stytch = createStytchHeadlessClient('public-token-<find yours in the stytch dashboard>')
7
+ *
8
+ * return (
9
+ * <StytchProvider stytch={stytch}>
10
+ * <App />
11
+ * </StytchProvider>
12
+ * )
13
+ * @returns A {@link StytchHeadlessClient}
14
+ */
15
+ declare const createStytchHeadlessClient: <TProjectConfiguration extends Partial<import("@stytch/core/public").StytchProjectConfiguration> = Stytch.DefaultProjectConfiguration>(_PUBLIC_TOKEN: string, options?: import("@stytch/core/public").StytchClientOptions | undefined) => StytchHeadlessClient<TProjectConfiguration>;
16
+ export { createStytchHeadlessClient };
@@ -0,0 +1,16 @@
1
+ import { StytchHeadlessClient } from "@stytch/vanilla-js/headless";
2
+ /**
3
+ * Creates a Headless Stytch client object to call the stytch APIs.
4
+ * The Stytch client is not available serverside.
5
+ * @example
6
+ * const stytch = createStytchHeadlessClient('public-token-<find yours in the stytch dashboard>')
7
+ *
8
+ * return (
9
+ * <StytchProvider stytch={stytch}>
10
+ * <App />
11
+ * </StytchProvider>
12
+ * )
13
+ * @returns A {@link StytchHeadlessClient}
14
+ */
15
+ declare const createStytchHeadlessClient: <TProjectConfiguration extends Partial<import("@stytch/core/public").StytchProjectConfiguration> = Stytch.DefaultProjectConfiguration>(_PUBLIC_TOKEN: string, options?: import("@stytch/core/public").StytchClientOptions | undefined) => StytchHeadlessClient<TProjectConfiguration>;
16
+ export { createStytchHeadlessClient };
@@ -0,0 +1,24 @@
1
+ import { StytchHeadlessClient } from '@stytch/vanilla-js/headless';
2
+ import { c as createStytchSSRProxy } from './StytchSSRProxy-34c789b5.js';
3
+
4
+ /**
5
+ * Creates a Headless Stytch client object to call the stytch APIs.
6
+ * The Stytch client is not available serverside.
7
+ * @example
8
+ * const stytch = createStytchHeadlessClient('public-token-<find yours in the stytch dashboard>')
9
+ *
10
+ * return (
11
+ * <StytchProvider stytch={stytch}>
12
+ * <App />
13
+ * </StytchProvider>
14
+ * )
15
+ * @returns A {@link StytchHeadlessClient}
16
+ */
17
+ const createStytchHeadlessClient = (...args) => {
18
+ if (typeof window === 'undefined') {
19
+ return createStytchSSRProxy();
20
+ }
21
+ return new StytchHeadlessClient(...args);
22
+ };
23
+
24
+ export { createStytchHeadlessClient };
@@ -0,0 +1,28 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var headless = require('@stytch/vanilla-js/headless');
6
+ var StytchSSRProxy = require('./StytchSSRProxy-86bc42b3.js');
7
+
8
+ /**
9
+ * Creates a Headless Stytch client object to call the stytch APIs.
10
+ * The Stytch client is not available serverside.
11
+ * @example
12
+ * const stytch = createStytchHeadlessClient('public-token-<find yours in the stytch dashboard>')
13
+ *
14
+ * return (
15
+ * <StytchProvider stytch={stytch}>
16
+ * <App />
17
+ * </StytchProvider>
18
+ * )
19
+ * @returns A {@link StytchHeadlessClient}
20
+ */
21
+ const createStytchHeadlessClient = (...args) => {
22
+ if (typeof window === 'undefined') {
23
+ return StytchSSRProxy.createStytchSSRProxy();
24
+ }
25
+ return new headless.StytchHeadlessClient(...args);
26
+ };
27
+
28
+ exports.createStytchHeadlessClient = createStytchHeadlessClient;
package/dist/index.js CHANGED
@@ -3,7 +3,8 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var React = require('react');
6
- var invariant = require('./invariant-ae5a5bce.js');
6
+ var useIsomorphicLayoutEffect = require('./useIsomorphicLayoutEffect-65746ef3.js');
7
+ var StytchSSRProxy = require('./StytchSSRProxy-86bc42b3.js');
7
8
 
8
9
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
10
 
@@ -12,10 +13,12 @@ var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
12
13
  const initialUser = {
13
14
  user: null,
14
15
  fromCache: false,
16
+ isInitialized: false,
15
17
  };
16
18
  const initialSession = {
17
19
  session: null,
18
20
  fromCache: false,
21
+ isInitialized: false,
19
22
  };
20
23
  const StytchContext = React.createContext({ isMounted: false });
21
24
  const StytchUserContext = React.createContext(initialUser);
@@ -27,29 +30,37 @@ const isUIClient = (client) => {
27
30
  /**
28
31
  * Returns the active User.
29
32
  * The Stytch SDKs are used for client-side authentication and session management.
33
+ * Check the isInitialized property to determine if the SDK has completed initialization.
30
34
  * Check the fromCache property to determine if the user data is from persistent storage.
31
35
  * @example
32
- * const {user} = useStytchUser();
36
+ * const {user, isInitialized, fromCache} = useStytchUser();
37
+ * if (!isInitialized) {
38
+ * return <p>Loading...</p>;
39
+ * }
33
40
  * return (<h1>Welcome, {user.name.first_name}</h1>);
34
41
  */
35
- const useStytchUser = () => {
36
- invariant.invariant(useIsMounted__INTERNAL(), invariant.noProviderError('useStytchUser'));
42
+ const useStytchUser$1 = () => {
43
+ useIsomorphicLayoutEffect.invariant(useIsMounted__INTERNAL(), StytchSSRProxy.noProviderError('useStytchUser'));
37
44
  return React.useContext(StytchUserContext);
38
45
  };
39
46
  /**
40
47
  * Returns the active user's Stytch session.
41
48
  * The Stytch SDKs are used for client-side authentication and session management.
49
+ * Check the isInitialized property to determine if the SDK has completed initialization.
42
50
  * Check the fromCache property to determine if the session data is from persistent storage.
43
51
  * @example
44
- * const {session} = useStytchSession();
52
+ * const {session, isInitialized, fromCache} = useStytchSession();
45
53
  * useEffect(() => {
54
+ * if (!isInitialized) {
55
+ * return;
56
+ * }
46
57
  * if (!session) {
47
58
  * router.replace('/login')
48
59
  * }
49
- * }, [session]);
60
+ * }, [session, isInitialized]);
50
61
  */
51
- const useStytchSession = () => {
52
- invariant.invariant(useIsMounted__INTERNAL(), invariant.noProviderError('useStytchSession'));
62
+ const useStytchSession$1 = () => {
63
+ useIsomorphicLayoutEffect.invariant(useIsMounted__INTERNAL(), StytchSSRProxy.noProviderError('useStytchSession'));
53
64
  return React.useContext(StytchSessionContext);
54
65
  };
55
66
  /**
@@ -63,31 +74,31 @@ const useStytchSession = () => {
63
74
  */
64
75
  const useStytch = () => {
65
76
  const ctx = React.useContext(StytchContext);
66
- invariant.invariant(ctx.isMounted, invariant.noProviderError('useStytch'));
77
+ useIsomorphicLayoutEffect.invariant(ctx.isMounted, StytchSSRProxy.noProviderError('useStytch'));
67
78
  return ctx.client;
68
79
  };
69
80
  const withStytch = (Component) => {
70
81
  const WithStytch = (props) => {
71
- invariant.invariant(useIsMounted__INTERNAL(), invariant.noProviderError('withStytch'));
82
+ useIsomorphicLayoutEffect.invariant(useIsMounted__INTERNAL(), StytchSSRProxy.noProviderError('withStytch'));
72
83
  return React__default["default"].createElement(Component, Object.assign({}, props, { stytch: useStytch() }));
73
84
  };
74
85
  WithStytch.displayName = `withStytch(${Component.displayName || Component.name || 'Component'})`;
75
86
  return WithStytch;
76
87
  };
77
- const withStytchUser = (Component) => {
88
+ const withStytchUser$1 = (Component) => {
78
89
  const WithStytchUser = (props) => {
79
- invariant.invariant(useIsMounted__INTERNAL(), invariant.noProviderError('withStytchUser'));
80
- const { user, fromCache } = useStytchUser();
81
- return React__default["default"].createElement(Component, Object.assign({}, props, { stytchUser: user, stytchUserIsFromCache: fromCache }));
90
+ useIsomorphicLayoutEffect.invariant(useIsMounted__INTERNAL(), StytchSSRProxy.noProviderError('withStytchUser'));
91
+ const { user, isInitialized, fromCache } = useStytchUser$1();
92
+ return (React__default["default"].createElement(Component, Object.assign({}, props, { stytchUser: user, stytchUserIsInitialized: isInitialized, stytchUserIsFromCache: fromCache })));
82
93
  };
83
94
  WithStytchUser.displayName = `withStytchUser(${Component.displayName || Component.name || 'Component'})`;
84
95
  return WithStytchUser;
85
96
  };
86
- const withStytchSession = (Component) => {
97
+ const withStytchSession$1 = (Component) => {
87
98
  const WithStytchSession = (props) => {
88
- invariant.invariant(useIsMounted__INTERNAL(), invariant.noProviderError('withStytchSession'));
89
- const { session, fromCache } = useStytchSession();
90
- return React__default["default"].createElement(Component, Object.assign({}, props, { stytchSession: session, stytchSessionIsFromCache: fromCache }));
99
+ useIsomorphicLayoutEffect.invariant(useIsMounted__INTERNAL(), StytchSSRProxy.noProviderError('withStytchSession'));
100
+ const { session, isInitialized, fromCache } = useStytchSession$1();
101
+ return (React__default["default"].createElement(Component, Object.assign({}, props, { stytchSession: session, stytchSessionIsInitialized: isInitialized, stytchSessionIsFromCache: fromCache })));
91
102
  };
92
103
  WithStytchSession.displayName = `withStytchSession(${Component.displayName || Component.name || 'Component'})`;
93
104
  return WithStytchSession;
@@ -96,32 +107,46 @@ const withStytchSession = (Component) => {
96
107
  * The Stytch Context Provider.
97
108
  * Wrap your application with this component in order to use Stytch everywhere in your app.
98
109
  * @example
99
- * const stytch = new StytchHeadlessClient('public-token-<find yours in the stytch dashboard>')
110
+ * const stytch = createStytchHeadlessClient('public-token-<find yours in the stytch dashboard>')
100
111
  *
101
- * ReactDOM.render(
112
+ * return (
102
113
  * <StytchProvider stytch={stytch}>
103
114
  * <App />
104
- * </StytchProvider>,
105
- * document.getElementById('root'),
115
+ * </StytchProvider>
106
116
  * )
107
117
  */
108
- const StytchProvider = ({ stytch, children, }) => {
109
- invariant.invariant(!useIsMounted__INTERNAL(), invariant.providerMustBeUniqueError);
110
- invariant.invariant(typeof window !== 'undefined', invariant.noSSRError);
118
+ const StytchProvider$1 = ({ stytch, children, assumeHydrated = false, }) => {
119
+ useIsomorphicLayoutEffect.invariant(!useIsMounted__INTERNAL(), StytchSSRProxy.providerMustBeUniqueError);
120
+ useIsomorphicLayoutEffect.invariant(!assumeHydrated || typeof window !== 'undefined', 'The `assumeHydrated` prop must be set to `false` when using StytchProvider in a server environment.');
111
121
  const ctx = React.useMemo(() => ({ client: stytch, isMounted: true }), [stytch]);
112
- const [{ user, session }, setClientState] = invariant.useAsyncState({
113
- session: stytch.session.getInfo(),
114
- user: stytch.user.getInfo(),
115
- });
116
- React.useEffect(() => stytch.onStateChange(() => {
117
- setClientState((oldState) => {
118
- const newState = {
119
- session: stytch.session.getInfo(),
120
- user: stytch.user.getInfo(),
121
- };
122
- return invariant.mergeWithStableProps(oldState, newState);
123
- });
124
- }), [setClientState, stytch]);
122
+ const getHydratedState = React.useCallback(() => {
123
+ return {
124
+ session: Object.assign(Object.assign({}, stytch.session.getInfo()), { isInitialized: true }),
125
+ user: Object.assign(Object.assign({}, stytch.user.getInfo()), { isInitialized: true }),
126
+ };
127
+ }, [stytch]);
128
+ const getInitialState = () => {
129
+ return {
130
+ session: initialSession,
131
+ user: initialUser,
132
+ };
133
+ };
134
+ const [{ user, session }, setClientState] = useIsomorphicLayoutEffect.useAsyncState(() => assumeHydrated ? getHydratedState() : getInitialState());
135
+ // Store the initial value of `assumeHydrated` in a ref, because it is
136
+ // logically only relevant for the first render
137
+ const assumeHydratedRef = React.useRef(assumeHydrated);
138
+ React.useEffect(() => {
139
+ if (StytchSSRProxy.isStytchSSRProxy(stytch)) {
140
+ return;
141
+ }
142
+ const updateState = () => {
143
+ setClientState((oldState) => useIsomorphicLayoutEffect.mergeWithStableProps(oldState, getHydratedState()));
144
+ };
145
+ if (!assumeHydratedRef.current) {
146
+ updateState();
147
+ }
148
+ return stytch.onStateChange(updateState);
149
+ }, [getHydratedState, setClientState, stytch]);
125
150
  return (React__default["default"].createElement(StytchContext.Provider, { value: ctx },
126
151
  React__default["default"].createElement(StytchUserContext.Provider, { value: user },
127
152
  React__default["default"].createElement(StytchSessionContext.Provider, { value: session }, children))));
@@ -157,12 +182,12 @@ const StytchProvider = ({ stytch, children, }) => {
157
182
  * />
158
183
  */
159
184
  const StytchLogin = ({ config, styles, callbacks, }) => {
160
- invariant.invariant(useIsMounted__INTERNAL(), invariant.noProviderError('<StytchLogin />'));
185
+ useIsomorphicLayoutEffect.invariant(useIsMounted__INTERNAL(), StytchSSRProxy.noProviderError('<StytchLogin />'));
161
186
  const stytchClient = useStytch();
162
187
  const containerEl = React.useRef(null);
163
- React.useLayoutEffect(() => {
188
+ useIsomorphicLayoutEffect.useIsomorphicLayoutEffect(() => {
164
189
  if (!isUIClient(stytchClient)) {
165
- throw Error(invariant.noHeadlessClientError);
190
+ throw Error(StytchSSRProxy.noHeadlessClientError);
166
191
  }
167
192
  if (!containerEl.current) {
168
193
  return;
@@ -211,12 +236,12 @@ const StytchLogin = ({ config, styles, callbacks, }) => {
211
236
  * />
212
237
  */
213
238
  const StytchPasswordReset = ({ config, styles, callbacks, passwordResetToken, }) => {
214
- invariant.invariant(useIsMounted__INTERNAL(), invariant.noProviderError('<StytchResetPassword />'));
239
+ useIsomorphicLayoutEffect.invariant(useIsMounted__INTERNAL(), StytchSSRProxy.noProviderError('<StytchResetPassword />'));
215
240
  const stytchClient = useStytch();
216
241
  const containerEl = React.useRef(null);
217
- React.useLayoutEffect(() => {
242
+ useIsomorphicLayoutEffect.useIsomorphicLayoutEffect(() => {
218
243
  if (!isUIClient(stytchClient)) {
219
- throw Error(invariant.noHeadlessClientError);
244
+ throw Error(StytchSSRProxy.noHeadlessClientError);
220
245
  }
221
246
  if (!containerEl.current) {
222
247
  return;
@@ -263,13 +288,13 @@ const StytchPasswordReset = ({ config, styles, callbacks, passwordResetToken, })
263
288
  * />
264
289
  */
265
290
  const StytchPasskeyRegistration = ({ config, styles, callbacks, }) => {
266
- invariant.invariant(useIsMounted__INTERNAL(), invariant.noProviderError('<StytchPasskeyRegistration />'));
291
+ useIsomorphicLayoutEffect.invariant(useIsMounted__INTERNAL(), StytchSSRProxy.noProviderError('<StytchPasskeyRegistration />'));
267
292
  const stytchClient = useStytch();
268
- const user = useStytchUser();
293
+ const user = useStytchUser$1();
269
294
  const containerEl = React.useRef(null);
270
- React.useLayoutEffect(() => {
295
+ useIsomorphicLayoutEffect.useIsomorphicLayoutEffect(() => {
271
296
  if (!isUIClient(stytchClient)) {
272
- throw Error(invariant.noHeadlessClientError);
297
+ throw Error(StytchSSRProxy.noHeadlessClientError);
273
298
  }
274
299
  if (!containerEl.current) {
275
300
  return;
@@ -315,13 +340,13 @@ const StytchPasskeyRegistration = ({ config, styles, callbacks, }) => {
315
340
  * />
316
341
  */
317
342
  const IdentityProvider = ({ styles, callbacks }) => {
318
- invariant.invariant(useIsMounted__INTERNAL(), invariant.noProviderError('<IdentityProvider />'));
343
+ useIsomorphicLayoutEffect.invariant(useIsMounted__INTERNAL(), StytchSSRProxy.noProviderError('<IdentityProvider />'));
319
344
  const stytchClient = useStytch();
320
- const user = useStytchUser();
345
+ const user = useStytchUser$1();
321
346
  const containerEl = React.useRef(null);
322
- React.useLayoutEffect(() => {
347
+ useIsomorphicLayoutEffect.useIsomorphicLayoutEffect(() => {
323
348
  if (!isUIClient(stytchClient)) {
324
- throw Error(invariant.noHeadlessClientError);
349
+ throw Error(StytchSSRProxy.noHeadlessClientError);
325
350
  }
326
351
  if (!containerEl.current) {
327
352
  return;
@@ -339,6 +364,55 @@ const IdentityProvider = ({ styles, callbacks }) => {
339
364
  return React__default["default"].createElement("div", { ref: containerEl });
340
365
  };
341
366
 
367
+ /**
368
+ * Returns the active User.
369
+ * The Stytch SDKs are used for client-side authentication and session management.
370
+ * Check the isInitialized property to determine if the SDK has completed initialization.
371
+ * Check the fromCache property to determine if the user data is from persistent storage.
372
+ * @example
373
+ * const {user, isInitialized, fromCache} = useStytchUser();
374
+ * if (!isInitialized) {
375
+ * return <p>Loading...</p>;
376
+ * }
377
+ * return (<h1>Welcome, {user.name.first_name}</h1>);
378
+ */
379
+ const useStytchUser = useStytchUser$1;
380
+ /**
381
+ * Returns the active user's Stytch session.
382
+ * The Stytch SDKs are used for client-side authentication and session management.
383
+ * Check the isInitialized property to determine if the SDK has completed initialization.
384
+ * Check the fromCache property to determine if the session data is from persistent storage.
385
+ * @example
386
+ * const {session, isInitialized, fromCache} = useStytchSession();
387
+ * useEffect(() => {
388
+ * if (!isInitialized) {
389
+ * return;
390
+ * }
391
+ * if (!session) {
392
+ * router.replace('/login')
393
+ * }
394
+ * }, [session, isInitialized]);
395
+ */
396
+ const useStytchSession = useStytchSession$1;
397
+ const withStytchUser = withStytchUser$1;
398
+ const withStytchSession = withStytchSession$1;
399
+ /**
400
+ * The Stytch Context Provider.
401
+ * Wrap your application with this component in order to use Stytch everywhere in your app.
402
+ * @example
403
+ * const stytch = createStytchHeadlessClient('public-token-<find yours in the stytch dashboard>')
404
+ *
405
+ * ReactDOM.render(
406
+ * <StytchProvider stytch={stytch}>
407
+ * <App />
408
+ * </StytchProvider>,
409
+ * document.getElementById('root'),
410
+ * )
411
+ */
412
+ const StytchProvider = ({ stytch, children, assumeHydrated = true, }) => {
413
+ return (React__default["default"].createElement(StytchProvider$1, { stytch: stytch, assumeHydrated: assumeHydrated }, children));
414
+ };
415
+
342
416
  exports.IdentityProvider = IdentityProvider;
343
417
  exports.StytchLogin = StytchLogin;
344
418
  exports.StytchPasskeyRegistration = StytchPasskeyRegistration;
@@ -0,0 +1,17 @@
1
+ import { StytchUIClient } from "@stytch/vanilla-js";
2
+ /**
3
+ * Creates a Stytch UI client object to call the Stytch APIs and render Stytch UI components.
4
+ * The Stytch client is not available serverside.
5
+ * If you do not use Stytch UI components, use {@link createStytchHeadlessClient} to reduce your bundle size.
6
+ * @example
7
+ * const stytch = createStytchUIClient('public-token-<find yours in the stytch dashboard>')
8
+ *
9
+ * return (
10
+ * <StytchProvider stytch={stytch}>
11
+ * <App />
12
+ * </StytchProvider>
13
+ * )
14
+ * @returns A {@link StytchUIClient}
15
+ */
16
+ declare const createStytchUIClient: <TProjectConfiguration extends Partial<import("@stytch/core/public").StytchProjectConfiguration> = Stytch.DefaultProjectConfiguration>(_PUBLIC_TOKEN: string, options?: import("@stytch/core/public").StytchClientOptions | undefined) => StytchUIClient<TProjectConfiguration>;
17
+ export { createStytchUIClient };