@stytch/nextjs 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
@@ -63,3 +63,98 @@ const App = () => {
63
63
  ## Typescript Support
64
64
 
65
65
  There are built in typescript definitions in the npm package.
66
+
67
+ ## Migrating from @stytch/stytch-react
68
+
69
+ If you are migrating from [@stytch/stytch-react](https://www.npmjs.com/package/@stytch/stytch-react), follow the steps below:
70
+
71
+ ### Step 1: Install the new libraries
72
+
73
+ - The core SDK is now bundled in its own module - [@stytch/vanilla-js](https://www.npmjs.com/package/@stytch/vanilla-js)
74
+ - We now have a library specifically for NextJS bindings - [@stytch/nextjs](https://www.npmjs.com/package/@stytch/nextjs)
75
+
76
+ ```shell
77
+ # NPM
78
+ npm install @stytch/vanilla-js @stytch/nextjs
79
+ # Yarn
80
+ yarn add @stytch/vanilla-js @stytch/nextjs
81
+ ```
82
+
83
+ ### Step 2: Remove the old SDK
84
+
85
+ - Remove the `@stytch/stytch-react` package
86
+ - If you are explicitly loading the `stytch.js` script via a blocking `beforeInteractive` tag in the document header, remove it. It isn't needed anymore.
87
+
88
+ ```shell
89
+ # NPM
90
+ npm rm @stytch/stytch-react
91
+ # Yarn
92
+ yarn remove @stytch/stytch-react
93
+ ```
94
+
95
+ ### Step 3: Initialize the Stytch client in `_app.js`
96
+
97
+ - 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.
98
+ - To create the Stytch Headless client, use `createStytchHeadlessClient` from `@stytch/nextjs/headless`
99
+ - To create the Stytch UI client, use `createStytchUIClient` from `@stytch/nextjs/ui`
100
+ - Pass it into `<StytchProvider />` component from `@stytch/nextjs`
101
+
102
+ ```jsx
103
+ import React from 'react';
104
+ import { StytchProvider } from '@stytch/nextjs';
105
+ import { createStytchHeadlessClient } from '@stytch/nextjs/headless';
106
+ // Or alternately
107
+ // import { createStytchUIClient } from '@stytch/nextjs/ui';
108
+
109
+ const stytch = createStytchHeadlessClient(process.env.NEXT_PUBLIC_STYTCH_PUBLIC_TOKEN);
110
+
111
+ function MyApp({ Component, pageProps }) {
112
+ return (
113
+ <StytchProvider stytch={stytch}>
114
+ <Component {...pageProps} />
115
+ </StytchProvider>
116
+ );
117
+ }
118
+
119
+ export default MyApp;
120
+ ```
121
+
122
+ ### Step 4: Update calls to `useStytchUser` , `useStytchSession`, and `useStytchLazy`
123
+
124
+ - `useStytchUser` and `useStytchSession` hooks now return envelope objects - `{ user, isInitialized, isCached }` and `{ session, isInitialized, isCached }` respectively.
125
+ - In SSR/SSG contexts, as well as the first clientside render, `user`/`session` will be null and `isInitialized` will be false
126
+ - The SDK will read `user`/`session` out of persistent storage, and rerender with `isCached: true` - at this point you’re reading the [stale-while-revalidating](https://swr.vercel.app/) value
127
+ - The SDK will make network requests to pull the most up-to-date user and session objects, and serve them with `isCached: false`
128
+ - `useStytchLazy` is no longer required - you may always call `useStytch` now
129
+
130
+ ```jsx
131
+ import React, { useEffect } from 'react';
132
+ import { useRouter } from 'next/router';
133
+ import { useStytchUser } from '@stytch/nextjs';
134
+
135
+ export default function Profile() {
136
+ const router = useRouter();
137
+ const { user, isInitialized } = useStytchUser();
138
+
139
+ useEffect(() => {
140
+ if (isInitialized && user === null) {
141
+ router.push('/login');
142
+ }
143
+ }, [user, isInitialized]);
144
+
145
+ return (
146
+ <Layout>
147
+ <h1>Your Profile</h1>
148
+ <pre>{JSON.stringify(user, null, 2)}</pre>
149
+ </Layout>
150
+ );
151
+ }
152
+ ```
153
+
154
+ ### Step 5: UI Naming Changes
155
+
156
+ We've made a number of small changes to our naming conventions to make the API cleaner and easier to understand.
157
+
158
+ - The `<Stytch />` login component is now called `<StytchLogin />`
159
+ - The `OAuthProvidersTypes` enum is now called `OAuthProviders`
160
+ - The `SDKProductTypes` enum is now called `Products`
package/dist/index.esm.js CHANGED
@@ -221,19 +221,17 @@ const StytchLogin = ({ config, styles, callbacks }) => {
221
221
  if (!containerEl.current) {
222
222
  return;
223
223
  }
224
- if (containerEl.current.id) {
225
- return;
224
+ if (!containerEl.current.id) {
225
+ const randId = Math.floor(Math.random() * 1e6);
226
+ containerEl.current.id = `stytch-magic-link-${randId}`;
226
227
  }
227
- const randId = Math.floor(Math.random() * 1e6);
228
- const elementId = `stytch-magic-link-${randId}`;
229
- containerEl.current.id = elementId;
230
228
  stytchClient.mountLogin({
231
229
  config,
232
230
  callbacks,
233
- elementId: `#${elementId}`,
231
+ elementId: `#${containerEl.current.id}`,
234
232
  styles,
235
233
  });
236
- }, [stytchClient]);
234
+ }, [stytchClient, config, styles, callbacks]);
237
235
  return React.createElement("div", { ref: containerEl });
238
236
  };
239
237
 
package/dist/index.js CHANGED
@@ -229,19 +229,17 @@ const StytchLogin = ({ config, styles, callbacks }) => {
229
229
  if (!containerEl.current) {
230
230
  return;
231
231
  }
232
- if (containerEl.current.id) {
233
- return;
232
+ if (!containerEl.current.id) {
233
+ const randId = Math.floor(Math.random() * 1e6);
234
+ containerEl.current.id = `stytch-magic-link-${randId}`;
234
235
  }
235
- const randId = Math.floor(Math.random() * 1e6);
236
- const elementId = `stytch-magic-link-${randId}`;
237
- containerEl.current.id = elementId;
238
236
  stytchClient.mountLogin({
239
237
  config,
240
238
  callbacks,
241
- elementId: `#${elementId}`,
239
+ elementId: `#${containerEl.current.id}`,
242
240
  styles,
243
241
  });
244
- }, [stytchClient]);
242
+ }, [stytchClient, config, styles, callbacks]);
245
243
  return React__default["default"].createElement("div", { ref: containerEl });
246
244
  };
247
245
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stytch/nextjs",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Stytch's official Next.js Library",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",
@@ -21,13 +21,13 @@
21
21
  "author": "Stytch",
22
22
  "devDependencies": {
23
23
  "@babel/runtime": "^7.18.6",
24
- "@stytch/vanilla-js": "0.1.0",
25
- "eslint-config-custom": "0.0.0",
24
+ "@stytch/vanilla-js": "workspace:*",
25
+ "eslint-config-custom": "workspace:*",
26
26
  "rollup": "^2.56.3",
27
27
  "typescript": "4.7.4"
28
28
  },
29
29
  "peerDependencies": {
30
- "@stytch/vanilla-js": "^0.1.0",
30
+ "@stytch/vanilla-js": "^0.2.0",
31
31
  "react": ">= 17.0.2",
32
32
  "react-dom": ">= 17.0.2"
33
33
  },
@@ -43,4 +43,4 @@
43
43
  "jwt",
44
44
  "user"
45
45
  ]
46
- }
46
+ }