@stytch/react 0.1.0 → 0.2.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/README.md CHANGED
@@ -67,3 +67,90 @@ const App = () => {
67
67
  ## Typescript Support
68
68
 
69
69
  There are built in typescript definitions in the npm package.
70
+
71
+ ## Migrating from @stytch/stytch-react
72
+
73
+ If you are migrating from [@stytch/stytch-react](https://www.npmjs.com/package/@stytch/stytch-react), follow the steps below:
74
+
75
+ ### Step 1: Install the new libraries
76
+
77
+ - The core SDK is now bundled in its own module - [@stytch/vanilla-js](https://www.npmjs.com/package/@stytch/vanilla-js)
78
+ - We now have a library specifically for React bindings - [@stytch/react](https://www.npmjs.com/package/@stytch/react)
79
+
80
+ ```shell
81
+ # NPM
82
+ npm install @stytch/vanilla-js @stytch/react
83
+ # Yarn
84
+ yarn add @stytch/vanilla-js @stytch/react
85
+ ```
86
+
87
+ ### Step 2: Remove the old SDK
88
+
89
+ - Remove the `@styth/stytch-react` package
90
+ - If you are explicitly loading the `stytch.js` script in the document header, remove it. It isn't needed anymore.
91
+
92
+ ```shell
93
+ # NPM
94
+ npm rm @stytch/stytch-react
95
+ # Yarn
96
+ yarn remove @stytch/stytch-react
97
+ ```
98
+
99
+ ### Step 3: Initialize the client in your Application Root
100
+
101
+ - Determine if you need the Headless or UI client. If you plan to use the `<StytchLogin />` component in your application then you should use the the UI client. Otherwise use the Headless client to minimize application bundle size.
102
+ - To create the Stytch Headless client, use `StytchHeadlessClient` from `@stytch/vanilla-js/headless`
103
+ - To create the Stytch UI client, use `StytchUIClient` from `@stytch/vanilla-js`
104
+ - Pass it into `<StytchProvider />` component from `@stytch/react`
105
+
106
+ ```jsx
107
+ import React from 'react';
108
+ import { StytchProvider } from '@stytch/react';
109
+ import { StytchHeadlessClient } from '@stytch/vanilla-js/headless';
110
+ // Or alternately
111
+ // import { StytchUIClient } from '@stytch/vanilla-js';
112
+
113
+ const stytch = new StytchHeadlessClient(process.env.REACT_APP_STYTCH_PUBLIC_TOKEN);
114
+
115
+ function WrappedApp() {
116
+ return (
117
+ <StytchProvider stytch={stytch}>
118
+ <App />
119
+ </StytchProvider>
120
+ );
121
+ }
122
+
123
+ export default WrappedApp;
124
+ ```
125
+
126
+ ### Step 4: Update calls to `useStytchUser` , `useStytchSession`, and `useStytchLazy`
127
+
128
+ - `useStytchUser` and `useStytchSession` hooks now return envelope objects - `{ user, isCached }` and `{ session, isCached }` respectively.
129
+ - On first render the SDK will read user/session out of persistent storage, and serve them with `isCached: true` - at this point you’re reading the stale-while-revalidating value
130
+ - The SDK will make network requests to pull the most up-to-date user and session objects, and serve them with `isCached: false`
131
+ - `useStytchLazy` is no longer required - you may always call `useStytch` now
132
+
133
+ ```jsx
134
+ import React, { useEffect } from 'react';
135
+ import { useStytchUser } from '@stytch/react';
136
+
137
+ export default function Profile() {
138
+ const router = useRouter();
139
+ const { user, isCached } = useStytchUser();
140
+
141
+ return (
142
+ <Layout>
143
+ <h1>Your {isCached ? 'Cached' : null} Profile</h1>
144
+ <pre>{JSON.stringify(user, null, 2)}</pre>
145
+ </Layout>
146
+ );
147
+ }
148
+ ```
149
+
150
+ ### Step 5: UI Naming Changes
151
+
152
+ We've made a number of small changes to our naming conventions to make the API cleaner and easier to understand.
153
+
154
+ - The `<Stytch />` login component is now called `<StytchLogin />`
155
+ - The `OAuthProvidersTypes` enum is now called `OAuthProviders`
156
+ - The `SDKProductTypes` enum is now called `Products`
package/dist/index.d.ts CHANGED
@@ -101,6 +101,21 @@ interface StytchProps {
101
101
  * @param props {@link StytchProps}
102
102
  */
103
103
  declare const StytchLogin: ({ config, styles, callbacks }: StytchProps) => JSX.Element;
104
+ interface StytchResetPasswordProps extends StytchProps {
105
+ passwordResetToken: string;
106
+ }
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
+ * and {@link https://storybook.stytch.com interactive examples} for more.
114
+ *
115
+ * @example
116
+ * TODO
117
+ */
118
+ declare const StytchPasswordReset: ({ config, styles, callbacks, passwordResetToken }: StytchResetPasswordProps) => JSX.Element;
104
119
  /**
105
120
  * The Stytch Client object passed in to <StytchProvider /> in your application root.
106
121
  * Either a StytchUIClient or StytchHeadlessClient.
@@ -189,5 +204,5 @@ type StytchProviderProps = {
189
204
  * )
190
205
  */
191
206
  declare const StytchProvider: ({ stytch, children }: StytchProviderProps) => JSX.Element;
192
- export { StytchLogin, StytchProvider, useStytch, useStytchSession, useStytchUser, withStytch, withStytchSession, withStytchUser };
207
+ export { StytchLogin, StytchPasswordReset, StytchProvider, useStytch, useStytchSession, useStytchUser, withStytch, withStytchSession, withStytchUser };
193
208
  export type { StytchProviderProps };
@@ -101,6 +101,21 @@ interface StytchProps {
101
101
  * @param props {@link StytchProps}
102
102
  */
103
103
  declare const StytchLogin: ({ config, styles, callbacks }: StytchProps) => JSX.Element;
104
+ interface StytchResetPasswordProps extends StytchProps {
105
+ passwordResetToken: string;
106
+ }
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
+ * and {@link https://storybook.stytch.com interactive examples} for more.
114
+ *
115
+ * @example
116
+ * TODO
117
+ */
118
+ declare const StytchPasswordReset: ({ config, styles, callbacks, passwordResetToken }: StytchResetPasswordProps) => JSX.Element;
104
119
  /**
105
120
  * The Stytch Client object passed in to <StytchProvider /> in your application root.
106
121
  * Either a StytchUIClient or StytchHeadlessClient.
@@ -189,5 +204,5 @@ type StytchProviderProps = {
189
204
  * )
190
205
  */
191
206
  declare const StytchProvider: ({ stytch, children }: StytchProviderProps) => JSX.Element;
192
- export { StytchLogin, StytchProvider, useStytch, useStytchSession, useStytchUser, withStytch, withStytchSession, withStytchUser };
207
+ export { StytchLogin, StytchPasswordReset, StytchProvider, useStytch, useStytchSession, useStytchUser, withStytch, withStytchSession, withStytchUser };
193
208
  export type { StytchProviderProps };
package/dist/index.esm.js CHANGED
@@ -203,20 +203,56 @@ const StytchLogin = ({ config, styles, callbacks }) => {
203
203
  if (!containerEl.current) {
204
204
  return;
205
205
  }
206
- if (containerEl.current.id) {
207
- return;
206
+ if (!containerEl.current.id) {
207
+ const randId = Math.floor(Math.random() * 1e6);
208
+ containerEl.current.id = `stytch-magic-link-${randId}`;
208
209
  }
209
- const randId = Math.floor(Math.random() * 1e6);
210
- const elementId = `stytch-magic-link-${randId}`;
211
- containerEl.current.id = elementId;
212
210
  stytchClient.mountLogin({
213
211
  config,
214
212
  callbacks,
215
- elementId: `#${elementId}`,
213
+ elementId: `#${containerEl.current.id}`,
216
214
  styles,
217
215
  });
218
- }, [stytchClient]);
216
+ }, [stytchClient, config, styles, callbacks]);
217
+ return React.createElement("div", { ref: containerEl });
218
+ };
219
+ /**
220
+ * The Stytch Reset Password component.
221
+ * This component can only be used with a {@link StytchUIClient} client constructor
222
+ * passed into the {@link StytchProvider}
223
+ *
224
+ * See the {@link https://stytch.com/docs/sdks/javascript-sdk online reference}
225
+ * and {@link https://storybook.stytch.com interactive examples} for more.
226
+ *
227
+ * @example
228
+ * TODO
229
+ */
230
+ const StytchPasswordReset = ({ config, styles, callbacks, passwordResetToken }) => {
231
+ invariant(useIsMounted__INTERNAL(), noProviderError('<StytchResetPassword />'));
232
+ const stytchClient = useStytch();
233
+ const containerEl = useRef(null);
234
+ useLayoutEffect(() => {
235
+ if (!isUIClient(stytchClient)) {
236
+ throw Error(noHeadlessClientError);
237
+ }
238
+ if (!containerEl.current) {
239
+ return;
240
+ }
241
+ if (!containerEl.current.id) {
242
+ const randId = Math.floor(Math.random() * 1e6);
243
+ containerEl.current.id = `stytch-reset-password-${randId}`;
244
+ }
245
+ if (passwordResetToken) {
246
+ stytchClient.mountResetPassword({
247
+ config,
248
+ callbacks,
249
+ elementId: `#${containerEl.current.id}`,
250
+ styles,
251
+ passwordResetToken,
252
+ });
253
+ }
254
+ }, [stytchClient, config, styles, callbacks, passwordResetToken]);
219
255
  return React.createElement("div", { ref: containerEl });
220
256
  };
221
257
 
222
- export { StytchLogin, StytchProvider, useStytch, useStytchSession, useStytchUser, withStytch, withStytchSession, withStytchUser };
258
+ export { StytchLogin, StytchPasswordReset, StytchProvider, useStytch, useStytchSession, useStytchUser, withStytch, withStytchSession, withStytchUser };
package/dist/index.js CHANGED
@@ -211,23 +211,60 @@ const StytchLogin = ({ config, styles, callbacks }) => {
211
211
  if (!containerEl.current) {
212
212
  return;
213
213
  }
214
- if (containerEl.current.id) {
215
- return;
214
+ if (!containerEl.current.id) {
215
+ const randId = Math.floor(Math.random() * 1e6);
216
+ containerEl.current.id = `stytch-magic-link-${randId}`;
216
217
  }
217
- const randId = Math.floor(Math.random() * 1e6);
218
- const elementId = `stytch-magic-link-${randId}`;
219
- containerEl.current.id = elementId;
220
218
  stytchClient.mountLogin({
221
219
  config,
222
220
  callbacks,
223
- elementId: `#${elementId}`,
221
+ elementId: `#${containerEl.current.id}`,
224
222
  styles,
225
223
  });
226
- }, [stytchClient]);
224
+ }, [stytchClient, config, styles, callbacks]);
225
+ return React__default["default"].createElement("div", { ref: containerEl });
226
+ };
227
+ /**
228
+ * The Stytch Reset Password component.
229
+ * This component can only be used with a {@link StytchUIClient} client constructor
230
+ * passed into the {@link StytchProvider}
231
+ *
232
+ * See the {@link https://stytch.com/docs/sdks/javascript-sdk online reference}
233
+ * and {@link https://storybook.stytch.com interactive examples} for more.
234
+ *
235
+ * @example
236
+ * TODO
237
+ */
238
+ const StytchPasswordReset = ({ config, styles, callbacks, passwordResetToken }) => {
239
+ invariant(useIsMounted__INTERNAL(), noProviderError('<StytchResetPassword />'));
240
+ const stytchClient = useStytch();
241
+ const containerEl = React.useRef(null);
242
+ React.useLayoutEffect(() => {
243
+ if (!isUIClient(stytchClient)) {
244
+ throw Error(noHeadlessClientError);
245
+ }
246
+ if (!containerEl.current) {
247
+ return;
248
+ }
249
+ if (!containerEl.current.id) {
250
+ const randId = Math.floor(Math.random() * 1e6);
251
+ containerEl.current.id = `stytch-reset-password-${randId}`;
252
+ }
253
+ if (passwordResetToken) {
254
+ stytchClient.mountResetPassword({
255
+ config,
256
+ callbacks,
257
+ elementId: `#${containerEl.current.id}`,
258
+ styles,
259
+ passwordResetToken,
260
+ });
261
+ }
262
+ }, [stytchClient, config, styles, callbacks, passwordResetToken]);
227
263
  return React__default["default"].createElement("div", { ref: containerEl });
228
264
  };
229
265
 
230
266
  exports.StytchLogin = StytchLogin;
267
+ exports.StytchPasswordReset = StytchPasswordReset;
231
268
  exports.StytchProvider = StytchProvider;
232
269
  exports.useStytch = useStytch;
233
270
  exports.useStytchSession = useStytchSession;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stytch/react",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Stytch's official React Library",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",
@@ -12,7 +12,7 @@
12
12
  "build": "npm run clean && npm run compile",
13
13
  "clean": "rm -rf ./dist",
14
14
  "compile": "rollup -c",
15
- "dev": "tsc -p tsconfig.build.json --watch",
15
+ "dev": "rollup -c -w",
16
16
  "test": "jest"
17
17
  },
18
18
  "license": "MIT",
@@ -31,14 +31,14 @@
31
31
  ],
32
32
  "devDependencies": {
33
33
  "@babel/runtime": "^7.18.6",
34
- "@stytch/vanilla-js": "0.1.0",
35
- "eslint-config-custom": "0.0.0",
34
+ "@stytch/vanilla-js": "workspace:*",
35
+ "eslint-config-custom": "workspace:*",
36
36
  "rollup": "^2.56.3",
37
37
  "typescript": "4.7.4"
38
38
  },
39
39
  "peerDependencies": {
40
- "@stytch/vanilla-js": "^0.1.0",
40
+ "@stytch/vanilla-js": "^0.2.0",
41
41
  "react": ">= 17.0.2",
42
42
  "react-dom": ">= 17.0.2"
43
43
  }
44
- }
44
+ }