@stytch/react 0.0.0-idp-20241212005436

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.
@@ -0,0 +1,296 @@
1
+ /// <reference types="react" />
2
+ import React from "react";
3
+ import { ReactNode } from "react";
4
+ // We need to import the StytchUIClient type to give the TSDoc parser a hint as to where it is from
5
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
6
+ import { Callbacks, StytchLoginConfig, StyleConfig, Session, StytchUIClient, User } from "@stytch/vanilla-js";
7
+ import { StytchHeadlessClient } from "@stytch/vanilla-js/headless";
8
+ interface StytchProps {
9
+ /**
10
+ * A {@link StytchLoginConfig} object. Add products and product-specific config to this object to change the login methods shown.
11
+ *
12
+ * @example
13
+ * {
14
+ * products: ['crypto', 'otps']
15
+ * }
16
+ *
17
+ * @example
18
+ * {
19
+ * products: ['emailMagicLinks'>]
20
+ * emailMagicLinksOptions: {
21
+ * loginRedirectURL: 'https://example.com/authenticate',
22
+ * signupRedirectURL: 'https://example.com/authenticate',
23
+ * }
24
+ * }
25
+ *
26
+ * @example
27
+ * {
28
+ * products: ['oauth'>]
29
+ * oauthOptions: {
30
+ * providers: [
31
+ * { type: 'google', one_tap: true, position: 'embedded' },
32
+ * { type: 'microsoft' },
33
+ * { type: 'apple' },
34
+ * { type: 'facebook' },
35
+ * ],
36
+ * }
37
+ * }
38
+ */
39
+ config: StytchLoginConfig;
40
+ /**
41
+ * An optional {@link StyleConfig} to customize the look and feel of the screen.
42
+ *
43
+ * @example
44
+ * {
45
+ * fontFamily: 'Arial, Helvetica, sans-serif',
46
+ * width: '360px',
47
+ * primaryColor: '#19303D',
48
+ * }
49
+ */
50
+ styles?: StyleConfig;
51
+ /**
52
+ * An optional {@link Callbacks} object.
53
+ *
54
+ * @example
55
+ * {
56
+ * onError: ({message}) => {
57
+ * console.error('Stytch login screen error', message)
58
+ * }
59
+ * }
60
+ *
61
+ * @example
62
+ * {
63
+ * onEvent: ({type, data}) => {
64
+ * if(type === StytchEventType.CryptoWalletAuthenticate) {
65
+ * console.log('Logged in with crypto wallet', data);
66
+ * }
67
+ * }
68
+ * }
69
+ */
70
+ callbacks?: Callbacks;
71
+ }
72
+ /**
73
+ * The Stytch Login Screen component.
74
+ * This component can only be used with a {@link StytchUIClient} client constructor
75
+ * passed into the {@link StytchProvider}
76
+ *
77
+ * See the {@link https://stytch.com/docs/sdks/javascript-sdk online reference}
78
+ *
79
+ * @example
80
+ * <StytchLogin
81
+ * config={{
82
+ * products: ['emailMagicLinks', 'oauth'],
83
+ * emailMagicLinksOptions: {
84
+ * loginRedirectURL: 'https://example.com/authenticate',
85
+ * signupRedirectURL: 'https://example.com/authenticate',
86
+ * },
87
+ * oauthOptions: {
88
+ * providers: [{ type: OAuthProviders.Google }, { type: OAuthProviders.Microsoft }],
89
+ * },
90
+ * }}
91
+ * styles={{
92
+ * fontFamily: '"Helvetica New", Helvetica, sans-serif',
93
+ * primaryColor: '#0577CA',
94
+ * width: '321px',
95
+ * }}
96
+ * callbacks={{
97
+ * onEvent: (event) => console.log(event)
98
+ * }}
99
+ * />
100
+ * @param props {@link StytchProps}
101
+ */
102
+ declare const StytchLogin: ({ config, styles, callbacks }: StytchProps) => React.JSX.Element;
103
+ interface StytchResetPasswordProps extends StytchProps {
104
+ passwordResetToken: string;
105
+ }
106
+ type StytchIDPProps = Omit<StytchProps, "config">;
107
+ /**
108
+ * The Stytch Reset Password component.
109
+ * This component can only be used with a {@link StytchUIClient} client constructor
110
+ * passed into the {@link StytchProvider}
111
+ *
112
+ * See the {@link https://stytch.com/docs/sdks/javascript-sdk online reference}
113
+ *
114
+ * @example
115
+ * <StytchPasswordReset
116
+ * config={{
117
+ * products: ['emailMagicLinks', 'oauth'],
118
+ * emailMagicLinksOptions: {
119
+ * loginRedirectURL: 'https://example.com/authenticate',
120
+ * signupRedirectURL: 'https://example.com/authenticate',
121
+ * },
122
+ * oauthOptions: {
123
+ * providers: [{ type: OAuthProviders.Google }, { type: OAuthProviders.Microsoft }],
124
+ * },
125
+ * }}
126
+ * passwordResetToken="PvC5UudZ7TPZbELt95yXAQ-8MeEUCRob8bUQ-g52fIJs"
127
+ * styles={{
128
+ * fontFamily: '"Helvetica New", Helvetica, sans-serif',
129
+ * primaryColor: '#0577CA',
130
+ * width: '321px',
131
+ * }}
132
+ * callbacks={{
133
+ * onEvent: (event) => console.log(event)
134
+ * }}
135
+ * />
136
+ *
137
+ * @param config - A {@link StytchLoginConfig} object
138
+ * @param passwordResetToken - A Stytch password reset token
139
+ * @param styles - An optional {@link StyleConfig} to customize the look and feel of the screen.
140
+ * @param callbacks - An optional {@link Callbacks} object
141
+ */
142
+ declare const StytchPasswordReset: ({ config, styles, callbacks, passwordResetToken }: StytchResetPasswordProps) => React.JSX.Element;
143
+ /**
144
+ * The Stytch Passkey Registration component.
145
+ * This component can only be used with a {@link StytchUIClient} client constructor
146
+ * passed into the {@link StytchProvider}
147
+ *
148
+ * See the {@link https://stytch.com/docs/sdks/javascript-sdk online reference}
149
+ *
150
+ *
151
+ * @example
152
+ * const styles = {
153
+ * container: {
154
+ * backgroundColor: '#e11e1e',
155
+ * },
156
+ * colors: {
157
+ * primary: '#ff00f7',
158
+ * secondary: '#5C727D',
159
+ * },
160
+ * }
161
+ *
162
+ * <StytchPasskeyRegistration
163
+ * config={{
164
+ * products: ['passkey'],
165
+ * }}
166
+ * styles={styles}
167
+ * callbacks={{
168
+ * onEvent: (event) => console.log(event)
169
+ * }}
170
+ * />
171
+ * @param config - A {@link StytchLoginConfig} object
172
+ * @param styles - An optional {@link StyleConfig} to customize the look and feel of the screen.
173
+ * @param callbacks - An optional {@link Callbacks} object
174
+ */
175
+ declare const StytchPasskeyRegistration: ({ config, styles, callbacks }: StytchProps) => React.JSX.Element;
176
+ /**
177
+ * The Stytch IDP component.
178
+ * Parses OAuth Authorization params (client_id, scope, nonce, etc.) out of the page URL.
179
+ * Requires the user to be logged in.
180
+ * This component can only be used with a {@link StytchUIClient} client constructor
181
+ * passed into the {@link StytchProvider}
182
+ *
183
+ * See the {@link https://stytch.com/docs/sdks/javascript-sdk online reference}
184
+ *
185
+ *
186
+ * @example
187
+ * const styles = {
188
+ * container: {
189
+ * backgroundColor: '#e11e1e',
190
+ * },
191
+ * colors: {
192
+ * primary: '#ff00f7',
193
+ * secondary: '#5C727D',
194
+ * },
195
+ * }
196
+ *
197
+ * <StytchIDP
198
+ * styles={styles}
199
+ * callbacks={{
200
+ * onEvent: (event) => console.log(event)
201
+ * }}
202
+ * />
203
+ * @param styles - An optional {@link StyleConfig} to customize the look and feel of the screen.
204
+ * @param callbacks - An optional {@link Callbacks} object
205
+ */
206
+ declare const StytchIDP: ({ styles, callbacks }: StytchIDPProps) => React.JSX.Element;
207
+ /**
208
+ * The Stytch Client object passed in to <StytchProvider /> in your application root.
209
+ * Either a StytchUIClient or StytchHeadlessClient.
210
+ */
211
+ type StytchClient = StytchUIClient | StytchHeadlessClient;
212
+ type SWRUser = {
213
+ /**
214
+ * Either the active {@link User} object, or null if the user is not logged in.
215
+ */
216
+ user: User | null;
217
+ /**
218
+ * If true, indicates that the value returned is from the application cache and a state refresh is in progress.
219
+ */
220
+ fromCache: boolean;
221
+ };
222
+ type SWRSession = {
223
+ /**
224
+ * Either the active {@link Session} object, or null if the user is not logged in.
225
+ */
226
+ session: Session | null;
227
+ /**
228
+ * If true, indicates that the value returned is from the application cache and a state refresh is in progress.
229
+ */
230
+ fromCache: boolean;
231
+ };
232
+ /**
233
+ * Returns the active User.
234
+ * Check the fromCache property to determine if the user data is from persistent storage.
235
+ * @example
236
+ * const {user} = useStytchUser();
237
+ * return (<h1>Welcome, {user.name.first_name}</h1>);
238
+ * @returns A {@link SWRUser}
239
+ */
240
+ declare const useStytchUser: () => SWRUser;
241
+ /**
242
+ * Returns the active user's Stytch session.
243
+ * @example
244
+ * const {session} = useStytchSession();
245
+ * useEffect(() => {
246
+ * if (!session) {
247
+ * router.replace('/login')
248
+ * }
249
+ * }, [session]);
250
+ * @returns A {@link SWRSession}
251
+ */
252
+ declare const useStytchSession: () => SWRSession;
253
+ /**
254
+ * Returns the Stytch client stored in the Stytch context.
255
+ *
256
+ * @example
257
+ * const stytch = useStytch();
258
+ * useEffect(() => {
259
+ * stytch.magicLinks.authenticate('...')
260
+ * }, [stytch]);
261
+ */
262
+ declare const useStytch: () => StytchClient;
263
+ declare const withStytch: <T extends object>(Component: React.ComponentType<T & {
264
+ stytch: StytchClient;
265
+ }>) => React.ComponentType<T>;
266
+ declare const withStytchUser: <T extends object>(Component: React.ComponentType<T & {
267
+ stytchUser: User | null;
268
+ stytchUserIsFromCache: boolean;
269
+ }>) => React.ComponentType<T>;
270
+ declare const withStytchSession: <T extends object>(Component: React.ComponentType<T & {
271
+ stytchSession: Session | null;
272
+ stytchSessionIsFromCache: boolean;
273
+ }>) => React.ComponentType<T>;
274
+ type StytchProviderProps = {
275
+ /**
276
+ * A Stytch client instance, either a {@link StytchUIClient} or {@link StytchHeadlessClient}
277
+ */
278
+ stytch: StytchClient;
279
+ children?: ReactNode;
280
+ };
281
+ /**
282
+ * The Stytch Context Provider.
283
+ * Wrap your application with this component in the root file in order to use Stytch everywhere in your app.
284
+ * @example
285
+ * const stytch = new StytchHeadlessClient('public-token-<find yours in the stytch dashboard>')
286
+ *
287
+ * ReactDOM.render(
288
+ * <StytchProvider stytch={stytch}>
289
+ * <App />
290
+ * </StytchProvider>,
291
+ * document.getElementById('root'),
292
+ * )
293
+ */
294
+ declare const StytchProvider: ({ stytch, children }: StytchProviderProps) => JSX.Element;
295
+ export { StytchLogin, StytchPasswordReset, StytchPasskeyRegistration, StytchIDP, StytchProvider, useStytch, useStytchSession, useStytchUser, withStytch, withStytchSession, withStytchUser };
296
+ export type { StytchProviderProps };
@@ -0,0 +1,349 @@
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';
3
+
4
+ const initialUser = {
5
+ user: null,
6
+ fromCache: false,
7
+ };
8
+ const initialSession = {
9
+ session: null,
10
+ fromCache: false,
11
+ };
12
+ const StytchContext = createContext({ isMounted: false });
13
+ const StytchUserContext = createContext(initialUser);
14
+ const StytchSessionContext = createContext(initialSession);
15
+ const useIsMounted__INTERNAL = () => useContext(StytchContext).isMounted;
16
+ const isUIClient = (client) => {
17
+ return client.mountLogin !== undefined;
18
+ };
19
+ /**
20
+ * Returns the active User.
21
+ * Check the fromCache property to determine if the user data is from persistent storage.
22
+ * @example
23
+ * const {user} = useStytchUser();
24
+ * return (<h1>Welcome, {user.name.first_name}</h1>);
25
+ * @returns A {@link SWRUser}
26
+ */
27
+ const useStytchUser = () => {
28
+ invariant(useIsMounted__INTERNAL(), noProviderError('useStytchUser'));
29
+ return useContext(StytchUserContext);
30
+ };
31
+ /**
32
+ * Returns the active user's Stytch session.
33
+ * @example
34
+ * const {session} = useStytchSession();
35
+ * useEffect(() => {
36
+ * if (!session) {
37
+ * router.replace('/login')
38
+ * }
39
+ * }, [session]);
40
+ * @returns A {@link SWRSession}
41
+ */
42
+ const useStytchSession = () => {
43
+ invariant(useIsMounted__INTERNAL(), noProviderError('useStytchSession'));
44
+ return useContext(StytchSessionContext);
45
+ };
46
+ /**
47
+ * Returns the Stytch client stored in the Stytch context.
48
+ *
49
+ * @example
50
+ * const stytch = useStytch();
51
+ * useEffect(() => {
52
+ * stytch.magicLinks.authenticate('...')
53
+ * }, [stytch]);
54
+ */
55
+ const useStytch = () => {
56
+ const ctx = useContext(StytchContext);
57
+ invariant(ctx.isMounted, noProviderError('useStytch'));
58
+ return ctx.client;
59
+ };
60
+ const withStytch = (Component) => {
61
+ const WithStytch = (props) => {
62
+ invariant(useIsMounted__INTERNAL(), noProviderError('withStytch'));
63
+ return React.createElement(Component, Object.assign({}, props, { stytch: useStytch() }));
64
+ };
65
+ WithStytch.displayName = `withStytch(${Component.displayName || Component.name || 'Component'})`;
66
+ return WithStytch;
67
+ };
68
+ const withStytchUser = (Component) => {
69
+ const WithStytchUser = (props) => {
70
+ invariant(useIsMounted__INTERNAL(), noProviderError('withStytchUser'));
71
+ const { user, fromCache } = useStytchUser();
72
+ return React.createElement(Component, Object.assign({}, props, { stytchUser: user, stytchUserIsFromCache: fromCache }));
73
+ };
74
+ WithStytchUser.displayName = `withStytchUser(${Component.displayName || Component.name || 'Component'})`;
75
+ return WithStytchUser;
76
+ };
77
+ const withStytchSession = (Component) => {
78
+ const WithStytchSession = (props) => {
79
+ invariant(useIsMounted__INTERNAL(), noProviderError('withStytchSession'));
80
+ const { session, fromCache } = useStytchSession();
81
+ return React.createElement(Component, Object.assign({}, props, { stytchSession: session, stytchSessionIsFromCache: fromCache }));
82
+ };
83
+ WithStytchSession.displayName = `withStytchSession(${Component.displayName || Component.name || 'Component'})`;
84
+ return WithStytchSession;
85
+ };
86
+ /**
87
+ * The Stytch Context Provider.
88
+ * Wrap your application with this component in the root file in order to use Stytch everywhere in your app.
89
+ * @example
90
+ * const stytch = new StytchHeadlessClient('public-token-<find yours in the stytch dashboard>')
91
+ *
92
+ * ReactDOM.render(
93
+ * <StytchProvider stytch={stytch}>
94
+ * <App />
95
+ * </StytchProvider>,
96
+ * document.getElementById('root'),
97
+ * )
98
+ */
99
+ const StytchProvider = ({ stytch, children }) => {
100
+ invariant(!useIsMounted__INTERNAL(), providerMustBeUniqueError);
101
+ invariant(typeof window !== 'undefined', noSSRError);
102
+ const ctx = useMemo(() => ({ client: stytch, isMounted: true }), [stytch]);
103
+ const [{ user, session }, setClientState] = useAsyncState({
104
+ session: stytch.session.getInfo(),
105
+ user: stytch.user.getInfo(),
106
+ });
107
+ useEffect(() => stytch.onStateChange(() => {
108
+ setClientState((oldState) => {
109
+ const newState = {
110
+ session: stytch.session.getInfo(),
111
+ user: stytch.user.getInfo(),
112
+ };
113
+ return mergeWithStableProps(oldState, newState);
114
+ });
115
+ }), [setClientState, stytch]);
116
+ return (React.createElement(StytchContext.Provider, { value: ctx },
117
+ React.createElement(StytchUserContext.Provider, { value: user },
118
+ React.createElement(StytchSessionContext.Provider, { value: session }, children))));
119
+ };
120
+
121
+ /**
122
+ * The Stytch Login Screen component.
123
+ * This component can only be used with a {@link StytchUIClient} client constructor
124
+ * passed into the {@link StytchProvider}
125
+ *
126
+ * See the {@link https://stytch.com/docs/sdks/javascript-sdk online reference}
127
+ *
128
+ * @example
129
+ * <StytchLogin
130
+ * config={{
131
+ * products: ['emailMagicLinks', 'oauth'],
132
+ * emailMagicLinksOptions: {
133
+ * loginRedirectURL: 'https://example.com/authenticate',
134
+ * signupRedirectURL: 'https://example.com/authenticate',
135
+ * },
136
+ * oauthOptions: {
137
+ * providers: [{ type: OAuthProviders.Google }, { type: OAuthProviders.Microsoft }],
138
+ * },
139
+ * }}
140
+ * styles={{
141
+ * fontFamily: '"Helvetica New", Helvetica, sans-serif',
142
+ * primaryColor: '#0577CA',
143
+ * width: '321px',
144
+ * }}
145
+ * callbacks={{
146
+ * onEvent: (event) => console.log(event)
147
+ * }}
148
+ * />
149
+ * @param props {@link StytchProps}
150
+ */
151
+ const StytchLogin = ({ config, styles, callbacks }) => {
152
+ invariant(useIsMounted__INTERNAL(), noProviderError('<StytchLogin />'));
153
+ const stytchClient = useStytch();
154
+ const containerEl = useRef(null);
155
+ useLayoutEffect(() => {
156
+ if (!isUIClient(stytchClient)) {
157
+ throw Error(noHeadlessClientError);
158
+ }
159
+ if (!containerEl.current) {
160
+ return;
161
+ }
162
+ if (!containerEl.current.id) {
163
+ const randId = Math.floor(Math.random() * 1e6);
164
+ containerEl.current.id = `stytch-magic-link-${randId}`;
165
+ }
166
+ stytchClient.mountLogin({
167
+ config: config,
168
+ callbacks,
169
+ elementId: `#${containerEl.current.id}`,
170
+ styles,
171
+ });
172
+ }, [stytchClient, config, styles, callbacks]);
173
+ return React.createElement("div", { ref: containerEl });
174
+ };
175
+ /**
176
+ * The Stytch Reset Password component.
177
+ * This component can only be used with a {@link StytchUIClient} client constructor
178
+ * passed into the {@link StytchProvider}
179
+ *
180
+ * See the {@link https://stytch.com/docs/sdks/javascript-sdk online reference}
181
+ *
182
+ * @example
183
+ * <StytchPasswordReset
184
+ * config={{
185
+ * products: ['emailMagicLinks', 'oauth'],
186
+ * emailMagicLinksOptions: {
187
+ * loginRedirectURL: 'https://example.com/authenticate',
188
+ * signupRedirectURL: 'https://example.com/authenticate',
189
+ * },
190
+ * oauthOptions: {
191
+ * providers: [{ type: OAuthProviders.Google }, { type: OAuthProviders.Microsoft }],
192
+ * },
193
+ * }}
194
+ * passwordResetToken="PvC5UudZ7TPZbELt95yXAQ-8MeEUCRob8bUQ-g52fIJs"
195
+ * styles={{
196
+ * fontFamily: '"Helvetica New", Helvetica, sans-serif',
197
+ * primaryColor: '#0577CA',
198
+ * width: '321px',
199
+ * }}
200
+ * callbacks={{
201
+ * onEvent: (event) => console.log(event)
202
+ * }}
203
+ * />
204
+ *
205
+ * @param config - A {@link StytchLoginConfig} object
206
+ * @param passwordResetToken - A Stytch password reset token
207
+ * @param styles - An optional {@link StyleConfig} to customize the look and feel of the screen.
208
+ * @param callbacks - An optional {@link Callbacks} object
209
+ */
210
+ const StytchPasswordReset = ({ config, styles, callbacks, passwordResetToken }) => {
211
+ invariant(useIsMounted__INTERNAL(), noProviderError('<StytchResetPassword />'));
212
+ const stytchClient = useStytch();
213
+ const containerEl = useRef(null);
214
+ useLayoutEffect(() => {
215
+ if (!isUIClient(stytchClient)) {
216
+ throw Error(noHeadlessClientError);
217
+ }
218
+ if (!containerEl.current) {
219
+ return;
220
+ }
221
+ if (!containerEl.current.id) {
222
+ const randId = Math.floor(Math.random() * 1e6);
223
+ containerEl.current.id = `stytch-reset-password-${randId}`;
224
+ }
225
+ if (passwordResetToken) {
226
+ stytchClient.mountResetPassword({
227
+ config: config,
228
+ callbacks,
229
+ elementId: `#${containerEl.current.id}`,
230
+ styles,
231
+ passwordResetToken,
232
+ });
233
+ }
234
+ }, [stytchClient, config, styles, callbacks, passwordResetToken]);
235
+ return React.createElement("div", { ref: containerEl });
236
+ };
237
+ /**
238
+ * The Stytch Passkey Registration component.
239
+ * This component can only be used with a {@link StytchUIClient} client constructor
240
+ * passed into the {@link StytchProvider}
241
+ *
242
+ * See the {@link https://stytch.com/docs/sdks/javascript-sdk online reference}
243
+ *
244
+ *
245
+ * @example
246
+ * const styles = {
247
+ * container: {
248
+ * backgroundColor: '#e11e1e',
249
+ * },
250
+ * colors: {
251
+ * primary: '#ff00f7',
252
+ * secondary: '#5C727D',
253
+ * },
254
+ * }
255
+ *
256
+ * <StytchPasskeyRegistration
257
+ * config={{
258
+ * products: ['passkey'],
259
+ * }}
260
+ * styles={styles}
261
+ * callbacks={{
262
+ * onEvent: (event) => console.log(event)
263
+ * }}
264
+ * />
265
+ * @param config - A {@link StytchLoginConfig} object
266
+ * @param styles - An optional {@link StyleConfig} to customize the look and feel of the screen.
267
+ * @param callbacks - An optional {@link Callbacks} object
268
+ */
269
+ const StytchPasskeyRegistration = ({ config, styles, callbacks }) => {
270
+ invariant(useIsMounted__INTERNAL(), noProviderError('<StytchPasskeyRegistration />'));
271
+ const stytchClient = useStytch();
272
+ const user = useStytchUser();
273
+ const containerEl = useRef(null);
274
+ useLayoutEffect(() => {
275
+ if (!isUIClient(stytchClient)) {
276
+ throw Error(noHeadlessClientError);
277
+ }
278
+ if (!containerEl.current) {
279
+ return;
280
+ }
281
+ if (!containerEl.current.id) {
282
+ const randId = Math.floor(Math.random() * 1e6);
283
+ containerEl.current.id = `stytch-reset-passkey-${randId}`;
284
+ }
285
+ stytchClient.mountPasskeyRegistration({
286
+ config,
287
+ callbacks,
288
+ elementId: `#${containerEl.current.id}`,
289
+ styles,
290
+ });
291
+ }, [stytchClient, config, styles, callbacks, user]);
292
+ return React.createElement("div", { ref: containerEl });
293
+ };
294
+ /**
295
+ * The Stytch IDP component.
296
+ * Parses OAuth Authorization params (client_id, scope, nonce, etc.) out of the page URL.
297
+ * Requires the user to be logged in.
298
+ * This component can only be used with a {@link StytchUIClient} client constructor
299
+ * passed into the {@link StytchProvider}
300
+ *
301
+ * See the {@link https://stytch.com/docs/sdks/javascript-sdk online reference}
302
+ *
303
+ *
304
+ * @example
305
+ * const styles = {
306
+ * container: {
307
+ * backgroundColor: '#e11e1e',
308
+ * },
309
+ * colors: {
310
+ * primary: '#ff00f7',
311
+ * secondary: '#5C727D',
312
+ * },
313
+ * }
314
+ *
315
+ * <StytchIDP
316
+ * styles={styles}
317
+ * callbacks={{
318
+ * onEvent: (event) => console.log(event)
319
+ * }}
320
+ * />
321
+ * @param styles - An optional {@link StyleConfig} to customize the look and feel of the screen.
322
+ * @param callbacks - An optional {@link Callbacks} object
323
+ */
324
+ const StytchIDP = ({ styles, callbacks }) => {
325
+ invariant(useIsMounted__INTERNAL(), noProviderError('<StytchIDP />'));
326
+ const stytchClient = useStytch();
327
+ const user = useStytchUser();
328
+ const containerEl = useRef(null);
329
+ useLayoutEffect(() => {
330
+ if (!isUIClient(stytchClient)) {
331
+ throw Error(noHeadlessClientError);
332
+ }
333
+ if (!containerEl.current) {
334
+ return;
335
+ }
336
+ if (!containerEl.current.id) {
337
+ const randId = Math.floor(Math.random() * 1e6);
338
+ containerEl.current.id = `stytch-idp-${randId}`;
339
+ }
340
+ stytchClient.mountIDP({
341
+ callbacks,
342
+ elementId: `#${containerEl.current.id}`,
343
+ styles,
344
+ });
345
+ }, [stytchClient, styles, callbacks, user]);
346
+ return React.createElement("div", { ref: containerEl });
347
+ };
348
+
349
+ export { StytchIDP, StytchLogin, StytchPasskeyRegistration, StytchPasswordReset, StytchProvider, useStytch, useStytchSession, useStytchUser, withStytch, withStytchSession, withStytchUser };