@stytch/nextjs 13.0.0 → 14.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/dist/b2b/index.d.ts +208 -3
- package/dist/b2b/index.headless.d.ts +16 -1
- package/dist/b2b/index.headless.esm.js +1 -1
- package/dist/b2b/index.headless.js +4121 -17
- package/dist/b2b/index.js +250 -14
- package/dist/b2b/index.ui.d.ts +17 -1
- package/dist/b2b/index.ui.js +64 -16
- package/dist/index.d.ts +275 -3
- package/dist/index.headless.d.ts +16 -1
- package/dist/index.headless.js +63 -16
- package/dist/index.js +374 -16
- package/dist/index.ui.d.ts +17 -1
- package/dist/index.ui.js +64 -16
- package/package.json +3 -3
- package/dist/Stytch.d.ts +0 -172
- package/dist/Stytch.js +0 -214
- package/dist/Stytch.js.map +0 -1
- package/dist/Stytch.spec.d.ts +0 -1
- package/dist/Stytch.spec.js +0 -130
- package/dist/Stytch.spec.js.map +0 -1
- package/dist/StytchContext.d.ts +0 -104
- package/dist/StytchContext.js +0 -180
- package/dist/StytchContext.js.map +0 -1
- package/dist/StytchContext.serverside.spec.d.ts +0 -4
- package/dist/StytchContext.serverside.spec.js +0 -51
- package/dist/StytchContext.serverside.spec.js.map +0 -1
- package/dist/StytchContext.spec.d.ts +0 -1
- package/dist/StytchContext.spec.js +0 -212
- package/dist/StytchContext.spec.js.map +0 -1
- package/dist/StytchSSRProxy.d.ts +0 -2
- package/dist/StytchSSRProxy.js +0 -29
- package/dist/StytchSSRProxy.js.map +0 -1
- package/dist/StytchSSRProxy.spec.d.ts +0 -1
- package/dist/StytchSSRProxy.spec.js +0 -16
- package/dist/StytchSSRProxy.spec.js.map +0 -1
- package/dist/b2b/Stytch.d.ts +0 -104
- package/dist/b2b/Stytch.js +0 -90
- package/dist/b2b/Stytch.js.map +0 -1
- package/dist/b2b/StytchB2BContext.d.ts +0 -104
- package/dist/b2b/StytchB2BContext.js +0 -179
- package/dist/b2b/StytchB2BContext.js.map +0 -1
- package/dist/b2b/StytchB2BContext.spec.d.ts +0 -1
- package/dist/b2b/StytchB2BContext.spec.js +0 -217
- package/dist/b2b/StytchB2BContext.spec.js.map +0 -1
- package/dist/b2b/createStytchB2BHeadlessClient.d.ts +0 -15
- package/dist/b2b/createStytchB2BHeadlessClient.js +0 -26
- package/dist/b2b/createStytchB2BHeadlessClient.js.map +0 -1
- package/dist/b2b/createStytchB2BUIClient.d.ts +0 -16
- package/dist/b2b/createStytchB2BUIClient.js +0 -27
- package/dist/b2b/createStytchB2BUIClient.js.map +0 -1
- package/dist/b2b/index.headless.js.map +0 -1
- package/dist/b2b/index.js.map +0 -1
- package/dist/b2b/index.ui.js.map +0 -1
- package/dist/createStytchHeadlessClient.d.ts +0 -15
- package/dist/createStytchHeadlessClient.js +0 -26
- package/dist/createStytchHeadlessClient.js.map +0 -1
- package/dist/createStytchUIClient.d.ts +0 -16
- package/dist/createStytchUIClient.js +0 -27
- package/dist/createStytchUIClient.js.map +0 -1
- package/dist/index.headless.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/index.ui.js.map +0 -1
- package/dist/utils/async.d.ts +0 -2
- package/dist/utils/async.js +0 -22
- package/dist/utils/async.js.map +0 -1
- package/dist/utils/errors.d.ts +0 -3
- package/dist/utils/errors.js +0 -28
- package/dist/utils/errors.js.map +0 -1
- package/dist/utils/invariant.d.ts +0 -1
- package/dist/utils/invariant.js +0 -10
- package/dist/utils/invariant.js.map +0 -1
- package/dist/utils/useIsomorphicLayoutEffect.d.ts +0 -3
- package/dist/utils/useIsomorphicLayoutEffect.js +0 -7
- package/dist/utils/useIsomorphicLayoutEffect.js.map +0 -1
package/dist/index.headless.js
CHANGED
|
@@ -1,18 +1,65 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var headless = require('@stytch/vanilla-js/headless');
|
|
6
|
+
|
|
7
|
+
const cannotInvokeMethodOnServerError = (path) => `[Stytch] Invalid serverside function call to ${path}.
|
|
8
|
+
The Stytch Javascript SDK is intended to ony be used on the client side.
|
|
9
|
+
Make sure to wrap your API calls in a hook to ensure they are executed on the client.
|
|
10
|
+
\`\`\`
|
|
11
|
+
const myComponent = () => {
|
|
12
|
+
const stytch = useStytch();
|
|
13
|
+
// This will error out on the server.
|
|
14
|
+
stytch.magicLinks.authenticate(...);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
// This will work well
|
|
17
|
+
stytch.magicLinks.authenticate(...);
|
|
18
|
+
}, []);
|
|
19
|
+
}
|
|
20
|
+
\`\`\`
|
|
21
|
+
|
|
22
|
+
If you want to make API calls from server environments, please use the Stytch Node Library
|
|
23
|
+
https://www.npmjs.com/package/stytch.
|
|
24
|
+
`;
|
|
25
|
+
|
|
26
|
+
const SSRStubKey = Symbol('__stytch_SSRStubKey');
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
28
|
+
const createProxy = (path) => {
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
30
|
+
const noop = () => { };
|
|
31
|
+
return new Proxy(noop, {
|
|
32
|
+
get(target, p) {
|
|
33
|
+
if (p === SSRStubKey) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
return createProxy(path + '.' + String(p));
|
|
37
|
+
},
|
|
38
|
+
apply() {
|
|
39
|
+
throw new Error(cannotInvokeMethodOnServerError(path));
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
const createStytchSSRProxy = () => createProxy('stytch');
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Creates a Headless Stytch client object to call the stytch APIs.
|
|
47
|
+
* The Stytch client is not available serverside.
|
|
48
|
+
* @example
|
|
49
|
+
* const stytch = createStytchHeadlessClient('public-token-<find yours in the stytch dashboard>')
|
|
50
|
+
*
|
|
51
|
+
* return (
|
|
52
|
+
* <StytchProvider stytch={stytch}>
|
|
53
|
+
* <App />
|
|
54
|
+
* </StytchProvider>
|
|
55
|
+
* )
|
|
56
|
+
* @returns A {@link StytchHeadlessClient}
|
|
57
|
+
*/
|
|
58
|
+
const createStytchHeadlessClient = (...args) => {
|
|
59
|
+
if (typeof window === 'undefined') {
|
|
60
|
+
return createStytchSSRProxy();
|
|
7
61
|
}
|
|
8
|
-
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
62
|
+
return new headless.StytchHeadlessClient(...args);
|
|
15
63
|
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
//# sourceMappingURL=index.headless.js.map
|
|
64
|
+
|
|
65
|
+
exports.createStytchHeadlessClient = createStytchHeadlessClient;
|
package/dist/index.js
CHANGED
|
@@ -1,16 +1,374 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var React = require('react');
|
|
6
|
+
|
|
7
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
|
+
|
|
9
|
+
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
10
|
+
|
|
11
|
+
const noProviderError = (item, provider = 'StytchProvider') => `${item} can only be used inside <${provider}>.`;
|
|
12
|
+
const noHeadlessClientError = `Tried to create a Stytch Login UI element using the Stytch Headless SDK.
|
|
13
|
+
You must use the UI SDK to use UI elements.
|
|
14
|
+
Please make sure you are importing createStytchHeadlessClient from @stytch/nextjs/ui and not from @stytch/nextjs/headless.`;
|
|
15
|
+
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
17
|
+
function invariant(cond, message) {
|
|
18
|
+
if (!cond)
|
|
19
|
+
throw new Error(message);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// useState can cause memory leaks if it is set after the component unmounted. For example, if it is
|
|
23
|
+
// set after `await`, or in a `then`, `catch`, or `finally`, or in a setTimout/setInterval.
|
|
24
|
+
const useAsyncState = (initialState) => {
|
|
25
|
+
const isMounted = React.useRef(true);
|
|
26
|
+
const [state, setState] = React.useState(initialState);
|
|
27
|
+
React.useEffect(() => {
|
|
28
|
+
isMounted.current = true;
|
|
29
|
+
return () => {
|
|
30
|
+
isMounted.current = false;
|
|
31
|
+
};
|
|
32
|
+
}, []);
|
|
33
|
+
const setStateAction = React.useCallback((newState) => {
|
|
34
|
+
isMounted.current && setState(newState);
|
|
35
|
+
}, []);
|
|
36
|
+
return [state, setStateAction];
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const SSRStubKey = Symbol('__stytch_SSRStubKey');
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
41
|
+
const isStytchSSRProxy = (proxy) => {
|
|
42
|
+
return !!proxy[SSRStubKey];
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const initialUser = {
|
|
46
|
+
user: null,
|
|
47
|
+
fromCache: false,
|
|
48
|
+
isInitialized: false,
|
|
49
|
+
};
|
|
50
|
+
const initialSession = {
|
|
51
|
+
session: null,
|
|
52
|
+
fromCache: false,
|
|
53
|
+
isInitialized: false,
|
|
54
|
+
};
|
|
55
|
+
const StytchContext = React.createContext({ isMounted: false });
|
|
56
|
+
const StytchUserContext = React.createContext(initialUser);
|
|
57
|
+
const StytchSessionContext = React.createContext(initialSession);
|
|
58
|
+
const useIsMounted__INTERNAL = () => React.useContext(StytchContext).isMounted;
|
|
59
|
+
const isUIClient = (client) => {
|
|
60
|
+
return client.mountLogin !== undefined;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Returns the active User.
|
|
64
|
+
* The Stytch SDKs are used for client-side authentication and session management.
|
|
65
|
+
* Check the isInitialized property to determine if the SDK has completed initialization.
|
|
66
|
+
* Check the fromCache property to determine if the session data is from persistent storage.
|
|
67
|
+
* See Next's {@link https://nextjs.org/docs/authentication#authenticating-statically-generated-pages documentation} for more.
|
|
68
|
+
* @example
|
|
69
|
+
* const {user, isInitialized, fromCache} = useStytchUser();
|
|
70
|
+
* if (!isInitialized) {
|
|
71
|
+
* return <p>Loading...</p>;
|
|
72
|
+
* }
|
|
73
|
+
* return (<h1>Welcome, {user.name.first_name}</h1>);
|
|
74
|
+
*/
|
|
75
|
+
const useStytchUser = () => {
|
|
76
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('useStytchUser'));
|
|
77
|
+
return React.useContext(StytchUserContext);
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Returns the user's active Stytch session.
|
|
81
|
+
* The Stytch SDKs are used for client-side authentication and session management.
|
|
82
|
+
* Check the isInitialized property to determine if the SDK has completed initialization.
|
|
83
|
+
* Check the fromCache property to determine if the session data is from persistent storage.
|
|
84
|
+
* See Next's {@link https://nextjs.org/docs/authentication#authenticating-statically-generated-pages documentation} for more.
|
|
85
|
+
* @example
|
|
86
|
+
* const {session, isInitialized, fromCache} = useStytchSession();
|
|
87
|
+
* useEffect(() => {
|
|
88
|
+
* if (!isInitialized) {
|
|
89
|
+
* return;
|
|
90
|
+
* }
|
|
91
|
+
* if (!session) {
|
|
92
|
+
* router.replace('/login')
|
|
93
|
+
* }
|
|
94
|
+
* }, [session, isInitialized]);
|
|
95
|
+
*/
|
|
96
|
+
const useStytchSession = () => {
|
|
97
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('useStytchSession'));
|
|
98
|
+
return React.useContext(StytchSessionContext);
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Returns the Stytch client stored in the Stytch context.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* const stytch = useStytch();
|
|
105
|
+
* useEffect(() => {
|
|
106
|
+
* stytch.magicLinks.authenticate('...')
|
|
107
|
+
* }, [stytch]);
|
|
108
|
+
*/
|
|
109
|
+
const useStytch = () => {
|
|
110
|
+
const ctx = React.useContext(StytchContext);
|
|
111
|
+
invariant(ctx.isMounted, noProviderError('useStytch'));
|
|
112
|
+
return ctx.client;
|
|
113
|
+
};
|
|
114
|
+
const withStytch = (Component) => {
|
|
115
|
+
const WithStytch = (props) => {
|
|
116
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('withStytch'));
|
|
117
|
+
return React__default['default'].createElement(Component, Object.assign({}, props, { stytch: useStytch() }));
|
|
118
|
+
};
|
|
119
|
+
WithStytch.displayName = `withStytch(${Component.displayName || Component.name || 'Component'})`;
|
|
120
|
+
return WithStytch;
|
|
121
|
+
};
|
|
122
|
+
const withStytchUser = (Component) => {
|
|
123
|
+
const WithStytchUser = (props) => {
|
|
124
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('withStytchUser'));
|
|
125
|
+
const { user, isInitialized, fromCache } = useStytchUser();
|
|
126
|
+
return (React__default['default'].createElement(Component, Object.assign({}, props, { stytchUser: user, stytchUserIsInitialized: isInitialized, stytchUserIsFromCache: fromCache })));
|
|
127
|
+
};
|
|
128
|
+
WithStytchUser.displayName = `withStytchUser(${Component.displayName || Component.name || 'Component'})`;
|
|
129
|
+
return WithStytchUser;
|
|
130
|
+
};
|
|
131
|
+
const withStytchSession = (Component) => {
|
|
132
|
+
const WithStytchSession = (props) => {
|
|
133
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('withStytchSession'));
|
|
134
|
+
const { session, isInitialized, fromCache } = useStytchSession();
|
|
135
|
+
return (React__default['default'].createElement(Component, Object.assign({}, props, { stytchSession: session, stytchSessionIsInitialized: isInitialized, stytchSessionIsFromCache: fromCache })));
|
|
136
|
+
};
|
|
137
|
+
WithStytchSession.displayName = `withStytchSession(${Component.displayName || Component.name || 'Component'})`;
|
|
138
|
+
return WithStytchSession;
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* The Stytch Context Provider.
|
|
142
|
+
* Wrap your application with this component in `_app.js` in order to use Stytch everywhere in your app.
|
|
143
|
+
* @example
|
|
144
|
+
* const stytch = createStytchHeadlessClient('public-token-<find yours in the stytch dashboard>')
|
|
145
|
+
*
|
|
146
|
+
* return (
|
|
147
|
+
* <StytchProvider stytch={stytch}>
|
|
148
|
+
* <App />
|
|
149
|
+
* </StytchProvider>
|
|
150
|
+
* )
|
|
151
|
+
*/
|
|
152
|
+
const StytchProvider = ({ stytch, children }) => {
|
|
153
|
+
const ctx = React.useMemo(() => ({ client: stytch, isMounted: true }), [stytch]);
|
|
154
|
+
const [user, setUser] = useAsyncState(initialUser);
|
|
155
|
+
const [session, setSession] = useAsyncState(initialSession);
|
|
156
|
+
React.useEffect(() => {
|
|
157
|
+
if (isStytchSSRProxy(stytch)) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
setUser({
|
|
161
|
+
user: stytch.user.getSync(),
|
|
162
|
+
fromCache: true,
|
|
163
|
+
isInitialized: true,
|
|
164
|
+
});
|
|
165
|
+
setSession({
|
|
166
|
+
session: stytch.session.getSync(),
|
|
167
|
+
fromCache: true,
|
|
168
|
+
isInitialized: true,
|
|
169
|
+
});
|
|
170
|
+
const unsubscribeUser = stytch.user.onChange((user) => setUser({ user, fromCache: false, isInitialized: true }));
|
|
171
|
+
const unsubscribeSession = stytch.session.onChange((session) => setSession({ session, fromCache: false, isInitialized: true }));
|
|
172
|
+
return () => {
|
|
173
|
+
unsubscribeUser();
|
|
174
|
+
unsubscribeSession();
|
|
175
|
+
};
|
|
176
|
+
}, [stytch, setUser, setSession]);
|
|
177
|
+
// TODO (SDK-813): Remove this when we have a single top-level onChange handler
|
|
178
|
+
const finalSess = !!session.session === !!user.user ? session : initialSession;
|
|
179
|
+
const finalUser = !!session.session === !!user.user ? user : initialUser;
|
|
180
|
+
return (React__default['default'].createElement(StytchContext.Provider, { value: ctx },
|
|
181
|
+
React__default['default'].createElement(StytchUserContext.Provider, { value: finalUser },
|
|
182
|
+
React__default['default'].createElement(StytchSessionContext.Provider, { value: finalSess }, children))));
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// cc https://medium.com/@alexandereardon/uselayouteffect-and-ssr-192986cdcf7a
|
|
186
|
+
const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The Stytch Login Screen component.
|
|
190
|
+
* This component can only be used with the Stytch UI Client - use {@link createStytchUIClient}
|
|
191
|
+
* in your `_app.jsx` file.
|
|
192
|
+
*
|
|
193
|
+
* See the {@link https://stytch.com/docs/sdks/javascript-sdk online reference}
|
|
194
|
+
* and {@link https://storybook.stytch.com interactive examples} for more.
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* <StytchLogin
|
|
198
|
+
* config={{
|
|
199
|
+
* products: ['emailMagicLinks', 'oauth'],
|
|
200
|
+
* emailMagicLinksOptions: {
|
|
201
|
+
* loginRedirectURL: 'https://example.com/authenticate',
|
|
202
|
+
* signupRedirectURL: 'https://example.com/authenticate',
|
|
203
|
+
* },
|
|
204
|
+
* oauthOptions: {
|
|
205
|
+
* providers: [{ type: OAuthProviders.Google }, { type: OAuthProviders.Microsoft }],
|
|
206
|
+
* },
|
|
207
|
+
* }}
|
|
208
|
+
* styles={{
|
|
209
|
+
* fontFamily: '"Helvetica New", Helvetica, sans-serif',
|
|
210
|
+
* primaryColor: '#0577CA',
|
|
211
|
+
* width: '321px',
|
|
212
|
+
* }}
|
|
213
|
+
* callbacks={{
|
|
214
|
+
* onEvent: (event) => console.log(event)
|
|
215
|
+
* }}
|
|
216
|
+
* />
|
|
217
|
+
*
|
|
218
|
+
* @param config - A {@link StytchLoginConfig} object
|
|
219
|
+
* @param styles - An optional {@link StyleConfig} to customize the look and feel of the screen.
|
|
220
|
+
* @param callbacks - An optional {@link Callbacks} object
|
|
221
|
+
*/
|
|
222
|
+
const StytchLogin = ({ config, styles, callbacks }) => {
|
|
223
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('<StytchLogin />'));
|
|
224
|
+
const stytchClient = useStytch();
|
|
225
|
+
const containerEl = React.useRef(null);
|
|
226
|
+
useIsomorphicLayoutEffect(() => {
|
|
227
|
+
if (!isUIClient(stytchClient)) {
|
|
228
|
+
throw Error(noHeadlessClientError);
|
|
229
|
+
}
|
|
230
|
+
if (!containerEl.current) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
if (!containerEl.current.id) {
|
|
234
|
+
const randId = Math.floor(Math.random() * 1e6);
|
|
235
|
+
containerEl.current.id = `stytch-magic-link-${randId}`;
|
|
236
|
+
}
|
|
237
|
+
stytchClient.mountLogin({
|
|
238
|
+
config,
|
|
239
|
+
callbacks,
|
|
240
|
+
elementId: `#${containerEl.current.id}`,
|
|
241
|
+
styles,
|
|
242
|
+
});
|
|
243
|
+
}, [stytchClient, config, styles, callbacks]);
|
|
244
|
+
return React__default['default'].createElement("div", { ref: containerEl });
|
|
245
|
+
};
|
|
246
|
+
/**
|
|
247
|
+
* The Stytch Reset Password component.
|
|
248
|
+
* This component can only be used with a {@link StytchUIClient} client constructor
|
|
249
|
+
* passed into the {@link StytchProvider}
|
|
250
|
+
*
|
|
251
|
+
* See the {@link https://stytch.com/docs/sdks/javascript-sdk online reference}
|
|
252
|
+
* and {@link https://storybook.stytch.com interactive examples} for more.
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* <StytchPasswordReset
|
|
256
|
+
* config={{
|
|
257
|
+
* products: ['emailMagicLinks', 'oauth'],
|
|
258
|
+
* emailMagicLinksOptions: {
|
|
259
|
+
* loginRedirectURL: 'https://example.com/authenticate',
|
|
260
|
+
* signupRedirectURL: 'https://example.com/authenticate',
|
|
261
|
+
* },
|
|
262
|
+
* oauthOptions: {
|
|
263
|
+
* providers: [{ type: OAuthProviders.Google }, { type: OAuthProviders.Microsoft }],
|
|
264
|
+
* },
|
|
265
|
+
* }}
|
|
266
|
+
* passwordResetToken="PvC5UudZ7TPZbELt95yXAQ-8MeEUCRob8bUQ-g52fIJs"
|
|
267
|
+
* styles={{
|
|
268
|
+
* fontFamily: '"Helvetica New", Helvetica, sans-serif',
|
|
269
|
+
* primaryColor: '#0577CA',
|
|
270
|
+
* width: '321px',
|
|
271
|
+
* }}
|
|
272
|
+
* callbacks={{
|
|
273
|
+
* onEvent: (event) => console.log(event)
|
|
274
|
+
* }}
|
|
275
|
+
* />
|
|
276
|
+
*
|
|
277
|
+
* @param config - A {@link StytchLoginConfig} object
|
|
278
|
+
* @param passwordResetToken - A Stytch password reset token
|
|
279
|
+
* @param styles - An optional {@link StyleConfig} to customize the look and feel of the screen.
|
|
280
|
+
* @param callbacks - An optional {@link Callbacks} object
|
|
281
|
+
*/
|
|
282
|
+
const StytchPasswordReset = ({ config, styles, callbacks, passwordResetToken }) => {
|
|
283
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('<StytchResetPassword />'));
|
|
284
|
+
const stytchClient = useStytch();
|
|
285
|
+
const containerEl = React.useRef(null);
|
|
286
|
+
useIsomorphicLayoutEffect(() => {
|
|
287
|
+
if (!isUIClient(stytchClient)) {
|
|
288
|
+
throw Error(noHeadlessClientError);
|
|
289
|
+
}
|
|
290
|
+
if (!containerEl.current) {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
if (!containerEl.current.id) {
|
|
294
|
+
const randId = Math.floor(Math.random() * 1e6);
|
|
295
|
+
containerEl.current.id = `stytch-reset-password-${randId}`;
|
|
296
|
+
}
|
|
297
|
+
if (passwordResetToken) {
|
|
298
|
+
stytchClient.mountResetPassword({
|
|
299
|
+
config,
|
|
300
|
+
callbacks,
|
|
301
|
+
elementId: `#${containerEl.current.id}`,
|
|
302
|
+
styles,
|
|
303
|
+
passwordResetToken,
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
}, [stytchClient, config, styles, callbacks, passwordResetToken]);
|
|
307
|
+
return React__default['default'].createElement("div", { ref: containerEl });
|
|
308
|
+
};
|
|
309
|
+
/**
|
|
310
|
+
* The Stytch Passkey Registration component.
|
|
311
|
+
* This component can only be used with a {@link StytchUIClient} client constructor
|
|
312
|
+
* passed into the {@link StytchProvider}
|
|
313
|
+
*
|
|
314
|
+
* See the {@link https://stytch.com/docs/sdks/javascript-sdk online reference}
|
|
315
|
+
* and {@link https://storybook.stytch.com interactive examples} for more.
|
|
316
|
+
*
|
|
317
|
+
* @example
|
|
318
|
+
* const styles = {
|
|
319
|
+
* container: {
|
|
320
|
+
* backgroundColor: '#e11e1e',
|
|
321
|
+
* },
|
|
322
|
+
* colors: {
|
|
323
|
+
* primary: '#ff00f7',
|
|
324
|
+
* secondary: '#5C727D',
|
|
325
|
+
* },
|
|
326
|
+
* }
|
|
327
|
+
*
|
|
328
|
+
* <StytchPasskeyRegistration
|
|
329
|
+
* styles={styles}
|
|
330
|
+
* callbacks={{
|
|
331
|
+
* onEvent: (event) => console.log(event)
|
|
332
|
+
* }}
|
|
333
|
+
* />
|
|
334
|
+
* />
|
|
335
|
+
*
|
|
336
|
+
* @param styles - An optional {@link StyleConfig} to customize the look and feel of the screen.
|
|
337
|
+
* @param callbacks - An optional {@link Callbacks} object
|
|
338
|
+
*/
|
|
339
|
+
const StytchPasskeyRegistration = ({ config, styles, callbacks }) => {
|
|
340
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('<StytchPasskeyRegistration />'));
|
|
341
|
+
const stytchClient = useStytch();
|
|
342
|
+
const containerEl = React.useRef(null);
|
|
343
|
+
const user = useStytchUser();
|
|
344
|
+
useIsomorphicLayoutEffect(() => {
|
|
345
|
+
if (!isUIClient(stytchClient)) {
|
|
346
|
+
throw Error(noHeadlessClientError);
|
|
347
|
+
}
|
|
348
|
+
if (!containerEl.current) {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
if (!containerEl.current.id) {
|
|
352
|
+
const randId = Math.floor(Math.random() * 1e6);
|
|
353
|
+
containerEl.current.id = `stytch-passkey-registration-${randId}`;
|
|
354
|
+
}
|
|
355
|
+
stytchClient.mountPasskeyRegistration({
|
|
356
|
+
config,
|
|
357
|
+
callbacks,
|
|
358
|
+
elementId: `#${containerEl.current.id}`,
|
|
359
|
+
styles,
|
|
360
|
+
});
|
|
361
|
+
}, [stytchClient, config, styles, callbacks, user]);
|
|
362
|
+
return React__default['default'].createElement("div", { ref: containerEl });
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
exports.StytchLogin = StytchLogin;
|
|
366
|
+
exports.StytchPasskeyRegistration = StytchPasskeyRegistration;
|
|
367
|
+
exports.StytchPasswordReset = StytchPasswordReset;
|
|
368
|
+
exports.StytchProvider = StytchProvider;
|
|
369
|
+
exports.useStytch = useStytch;
|
|
370
|
+
exports.useStytchSession = useStytchSession;
|
|
371
|
+
exports.useStytchUser = useStytchUser;
|
|
372
|
+
exports.withStytch = withStytch;
|
|
373
|
+
exports.withStytchSession = withStytchSession;
|
|
374
|
+
exports.withStytchUser = withStytchUser;
|
package/dist/index.ui.d.ts
CHANGED
|
@@ -1 +1,17 @@
|
|
|
1
|
-
|
|
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: (_PUBLIC_TOKEN: string, options?: import("@stytch/core/dist/public").StytchClientOptions | undefined) => StytchUIClient;
|
|
17
|
+
export { createStytchUIClient };
|
package/dist/index.ui.js
CHANGED
|
@@ -1,18 +1,66 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var vanillaJs = require('@stytch/vanilla-js');
|
|
6
|
+
|
|
7
|
+
const cannotInvokeMethodOnServerError = (path) => `[Stytch] Invalid serverside function call to ${path}.
|
|
8
|
+
The Stytch Javascript SDK is intended to ony be used on the client side.
|
|
9
|
+
Make sure to wrap your API calls in a hook to ensure they are executed on the client.
|
|
10
|
+
\`\`\`
|
|
11
|
+
const myComponent = () => {
|
|
12
|
+
const stytch = useStytch();
|
|
13
|
+
// This will error out on the server.
|
|
14
|
+
stytch.magicLinks.authenticate(...);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
// This will work well
|
|
17
|
+
stytch.magicLinks.authenticate(...);
|
|
18
|
+
}, []);
|
|
19
|
+
}
|
|
20
|
+
\`\`\`
|
|
21
|
+
|
|
22
|
+
If you want to make API calls from server environments, please use the Stytch Node Library
|
|
23
|
+
https://www.npmjs.com/package/stytch.
|
|
24
|
+
`;
|
|
25
|
+
|
|
26
|
+
const SSRStubKey = Symbol('__stytch_SSRStubKey');
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
28
|
+
const createProxy = (path) => {
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
30
|
+
const noop = () => { };
|
|
31
|
+
return new Proxy(noop, {
|
|
32
|
+
get(target, p) {
|
|
33
|
+
if (p === SSRStubKey) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
return createProxy(path + '.' + String(p));
|
|
37
|
+
},
|
|
38
|
+
apply() {
|
|
39
|
+
throw new Error(cannotInvokeMethodOnServerError(path));
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
const createStytchSSRProxy = () => createProxy('stytch');
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Creates a Stytch UI client object to call the Stytch APIs and render Stytch UI components.
|
|
47
|
+
* The Stytch client is not available serverside.
|
|
48
|
+
* If you do not use Stytch UI components, use {@link createStytchHeadlessClient} to reduce your bundle size.
|
|
49
|
+
* @example
|
|
50
|
+
* const stytch = createStytchUIClient('public-token-<find yours in the stytch dashboard>')
|
|
51
|
+
*
|
|
52
|
+
* return (
|
|
53
|
+
* <StytchProvider stytch={stytch}>
|
|
54
|
+
* <App />
|
|
55
|
+
* </StytchProvider>
|
|
56
|
+
* )
|
|
57
|
+
* @returns A {@link StytchUIClient}
|
|
58
|
+
*/
|
|
59
|
+
const createStytchUIClient = (...args) => {
|
|
60
|
+
if (typeof window === 'undefined') {
|
|
61
|
+
return createStytchSSRProxy();
|
|
7
62
|
}
|
|
8
|
-
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
63
|
+
return new vanillaJs.StytchUIClient(...args);
|
|
15
64
|
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
//# sourceMappingURL=index.ui.js.map
|
|
65
|
+
|
|
66
|
+
exports.createStytchUIClient = createStytchUIClient;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stytch/nextjs",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "14.0.0",
|
|
4
4
|
"description": "Stytch's official Next.js Library",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.esm.js",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"author": "Stytch",
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@babel/runtime": "7.18.6",
|
|
25
|
-
"@stytch/vanilla-js": "3.
|
|
25
|
+
"@stytch/vanilla-js": "3.2.0",
|
|
26
26
|
"@testing-library/react": "14.0.0",
|
|
27
27
|
"eslint-config-custom": "0.0.1",
|
|
28
28
|
"react-dom": ">= 17.0.2",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"typescript": "4.7.4"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@stytch/vanilla-js": "^3.
|
|
34
|
+
"@stytch/vanilla-js": "^3.2.0",
|
|
35
35
|
"react": ">= 17.0.2",
|
|
36
36
|
"react-dom": ">= 17.0.2"
|
|
37
37
|
},
|