Skip to content

Commit a0b0ac5

Browse files
committed
Rearrange exports for RSC and add experimental RSC route to example
1 parent 8fb6bde commit a0b0ac5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+852
-455
lines changed

EXAMPLES.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Wrap your `pages/_app.jsx` component in the `UserProvider` component.
4040
```jsx
4141
// pages/_app.jsx
4242
import React from 'react';
43-
import { UserProvider } from '@auth0/nextjs-auth0';
43+
import { UserProvider } from '@auth0/nextjs-auth0/client';
4444

4545
export default function App({ Component, pageProps }) {
4646
// You can optionally pass the `user` prop from pages that require server-side
@@ -59,7 +59,7 @@ Check the user's authentication state and log them in or out from the front end
5959

6060
```jsx
6161
// pages/index.jsx
62-
import { useUser } from '@auth0/nextjs-auth0';
62+
import { useUser } from '@auth0/nextjs-auth0/client';
6363

6464
export default () => {
6565
const { user, error, isLoading } = useUser();
@@ -167,7 +167,7 @@ Requests to `/pages/profile` without a valid session cookie will be redirected t
167167

168168
```jsx
169169
// pages/profile.js
170-
import { withPageAuthRequired } from '@auth0/nextjs-auth0';
170+
import { withPageAuthRequired } from '@auth0/nextjs-auth0/client';
171171

172172
export default withPageAuthRequired(function Profile({ user }) {
173173
return <div>Hello {user.name}</div>;
@@ -195,7 +195,7 @@ Then you can access your API from the frontend with a valid session cookie.
195195
```jsx
196196
// pages/products
197197
import useSWR from 'swr';
198-
import { withPageAuthRequired } from '@auth0/nextjs-auth0';
198+
import { withPageAuthRequired } from '@auth0/nextjs-auth0/client';
199199

200200
const fetcher = async (uri) => {
201201
const response = await fetch(uri);
@@ -221,7 +221,7 @@ To protect all your routes:
221221

222222
```js
223223
// middleware.js
224-
import { withMiddlewareAuthRequired } from '@auth0/nextjs-auth0/middleware';
224+
import { withMiddlewareAuthRequired } from '@auth0/nextjs-auth0/edge';
225225

226226
export default withMiddlewareAuthRequired();
227227
```
@@ -230,7 +230,7 @@ To protect specific routes:
230230

231231
```js
232232
// middleware.js
233-
import { withMiddlewareAuthRequired } from '@auth0/nextjs-auth0/middleware';
233+
import { withMiddlewareAuthRequired } from '@auth0/nextjs-auth0/edge';
234234

235235
export default withMiddlewareAuthRequired();
236236

@@ -245,7 +245,7 @@ To run custom middleware for authenticated users:
245245

246246
```js
247247
// middleware.js
248-
import { withMiddlewareAuthRequired, getSession } from '@auth0/nextjs-auth0/middleware';
248+
import { withMiddlewareAuthRequired, getSession } from '@auth0/nextjs-auth0/edge';
249249

250250
export default withMiddlewareAuthRequired(async function middleware(req) {
251251
const res = NextResponse.next();
@@ -262,8 +262,8 @@ For using middleware with your own instance of the SDK:
262262
import {
263263
withMiddlewareAuthRequired,
264264
getSession,
265-
initAuth0 // note the mw specific `initAuth0`
266-
} from '@auth0/nextjs-auth0/middleware';
265+
initAuth0 // note the edge runtime specific `initAuth0`
266+
} from '@auth0/nextjs-auth0/edge';
267267

268268
const auth0 = initAuth0({ ... });
269269

README.md

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ Wrap your `pages/_app.js` component with the `UserProvider` component:
127127
```jsx
128128
// pages/_app.js
129129
import React from 'react';
130-
import { UserProvider } from '@auth0/nextjs-auth0';
130+
import { UserProvider } from '@auth0/nextjs-auth0/client';
131131

132132
export default function App({ Component, pageProps }) {
133133
return (
@@ -144,7 +144,7 @@ You can now determine if a user is authenticated by checking that the `user` obj
144144

145145
```jsx
146146
// pages/index.js
147-
import { useUser } from '@auth0/nextjs-auth0';
147+
import { useUser } from '@auth0/nextjs-auth0/client';
148148

149149
export default function Index() {
150150
const { user, error, isLoading } = useUser();
@@ -174,30 +174,47 @@ For other comprehensive examples, see the [EXAMPLES.md](./EXAMPLES.md) document.
174174

175175
### API Reference
176176

177-
- [Configuration Options](https://auth0.github.io/nextjs-auth0/modules/config.html)
177+
#### Server (for Node.js)
178178

179-
**Server-side methods**:
179+
`import * from @auth0/nextjs-auth0`
180180

181+
- [Configuration Options and Environment variables](https://auth0.github.io/nextjs-auth0/modules/config.html)
182+
- [initAuth0](https://auth0.github.io/nextjs-auth0/modules/index.html#initauth0)
181183
- [handleAuth](https://auth0.github.io/nextjs-auth0/modules/handlers_auth.html)
182184
- [handleLogin](https://auth0.github.io/nextjs-auth0/modules/handlers_login.html#handlelogin)
183185
- [handleCallback](https://auth0.github.io/nextjs-auth0/modules/handlers_callback.html)
184186
- [handleLogout](https://auth0.github.io/nextjs-auth0/modules/handlers_logout.html)
185187
- [handleProfile](https://auth0.github.io/nextjs-auth0/modules/handlers_profile.html)
186188
- [withApiAuthRequired](https://auth0.github.io/nextjs-auth0/modules/helpers_with_api_auth_required.html)
187189
- [withPageAuthRequired](https://auth0.github.io/nextjs-auth0/modules/helpers_with_page_auth_required.html#withpageauthrequired)
188-
- [withMiddlewareAuthRequired](https://auth0.github.io/nextjs-auth0/modules/helpers_with_middleware_auth_required.html)
189190
- [getSession](https://auth0.github.io/nextjs-auth0/modules/session_get_session.html)
190191
- [updateSession](https://auth0.github.io/nextjs-auth0/modules/session_update_session.html)
191192
- [getAccessToken](https://auth0.github.io/nextjs-auth0/modules/session_get_access_token.html)
192-
- [initAuth0](https://auth0.github.io/nextjs-auth0/modules/instance.html)
193193

194-
**Client-side methods/components**:
194+
#### Edge (for Middleware and the Edge runtime)
195+
196+
`import * from @auth0/nextjs-auth0/edge`
197+
198+
- [Configuration Options and Environment variables](https://auth0.github.io/nextjs-auth0/modules/config.html)
199+
- [initAuth0](https://auth0.github.io/nextjs-auth0/modules/edge.html#initauth0-1)
200+
- [withMiddlewareAuthRequired](https://auth0.github.io/nextjs-auth0/modules/helpers_with_middleware_auth_required.html)
201+
- [getSession](https://auth0.github.io/nextjs-auth0/modules/edge.html#getsession-1)
202+
203+
#### Client (for the Browser)
204+
205+
`import * from @auth0/nextjs-auth0/client`
206+
207+
- [UserProvider](https://auth0.github.io/nextjs-auth0/modules/client_use_user.html#userprovider)
208+
- [useUser](https://auth0.github.io/nextjs-auth0/modules/client_use_user.html)
209+
- [withPageAuthRequired](https://auth0.github.io/nextjs-auth0/modules/client_with_page_auth_required.html)
210+
211+
#### Testing helpers
212+
213+
`import * from @auth0/nextjs-auth0/testing`
195214

196-
- [UserProvider](https://auth0.github.io/nextjs-auth0/modules/frontend_use_user.html#userprovider)
197-
- [useUser](https://auth0.github.io/nextjs-auth0/modules/frontend_use_user.html)
198-
- [withPageAuthRequired](https://auth0.github.io/nextjs-auth0/modules/frontend_with_page_auth_required.html)
215+
- [generateSessionCookie](https://auth0.github.io/nextjs-auth0/modules/helpers_testing.html#generatesessioncookie)
199216

200-
Visit the auto-generated [API Docs](https://auth0.github.io/nextjs-auth0/) for more details.
217+
Visit the auto-generated [API Docs](https://auth0.github.io/nextjs-auth0/) for more details
201218

202219
### Cookies and Security
203220

V2_MIGRATION_GUIDE.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Guide to migrating from `1.x` to `2.x`
44

55
- [Node 10 is no longer supported](#node-10-is-no-longer-supported)
66
- [`getSession` now returns a `Promise`](#getsession-now-returns-a-promise)
7+
- [Client methods and components are now exported under /client](#client-methods-and-components-are-now-exported-under-client)
78
- [`updateSession` has been added](#updatesession-has-been-added)
89
- [`getServerSidePropsWrapper` has been removed](#getserversidepropswrapper-has-been-removed)
910
- [Profile API route no longer returns a 401](#profile-api-route-no-longer-returns-a-401)
@@ -42,6 +43,92 @@ async function myApiRoute(req, res) {
4243
}
4344
```
4445

46+
## Client methods and components are now exported under /client
47+
48+
All methods and components for the browser should now be accessed under `/client`
49+
50+
### Before
51+
52+
```js
53+
// pages/_app.js
54+
import React from 'react';
55+
import { UserProvider } from '@auth0/nextjs-auth0';
56+
57+
export default function App({ Component, pageProps }) {
58+
return (
59+
<UserProvider>
60+
<Component {...pageProps} />
61+
</UserProvider>
62+
);
63+
}
64+
```
65+
66+
```js
67+
// pages/index.js
68+
import { useUser } from '@auth0/nextjs-auth0';
69+
70+
export default function Index() {
71+
const { user, error, isLoading } = useUser();
72+
73+
if (isLoading) return <div>Loading...</div>;
74+
if (error) return <div>{error.message}</div>;
75+
76+
if (user) {
77+
return (
78+
<div>
79+
Welcome {user.name}! <a href="/api/auth/logout">Logout</a>
80+
</div>
81+
);
82+
}
83+
84+
return <a href="/api/auth/login">Login</a>;
85+
}
86+
```
87+
88+
### After
89+
90+
```js
91+
// pages/_app.js
92+
import React from 'react';
93+
import { UserProvider } from '@auth0/nextjs-auth0/client';
94+
95+
export default function App({ Component, pageProps }) {
96+
return (
97+
<UserProvider>
98+
<Component {...pageProps} />
99+
</UserProvider>
100+
);
101+
}
102+
```
103+
104+
```js
105+
// pages/index.js
106+
import { useUser, withPageAuthRequired as withPageAuthRequiredCSR } from '@auth0/nextjs-auth0/client';
107+
// The SSR version of withPageAuthRequired is still in the root export
108+
import { withPageAuthRequired as withPageAuthRequiredSSR } from '@auth0/nextjs-auth0';
109+
110+
export default withPageAuthRequiredCSR(function Index() {
111+
const { user, error, isLoading } = useUser();
112+
113+
if (isLoading) return <div>Loading...</div>;
114+
if (error) return <div>{error.message}</div>;
115+
116+
if (user) {
117+
return (
118+
<div>
119+
Welcome {user.name}! <a href="/api/auth/logout">Logout</a>
120+
</div>
121+
);
122+
}
123+
124+
return <a href="/api/auth/login">Login</a>;
125+
});
126+
127+
export const getServerSideProps = withPageAuthRequiredSSR();
128+
```
129+
130+
### Before
131+
45132
## `updateSession` has been added
46133

47134
### Before

client.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type * from './dist/client';

client.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./dist/client');

edge.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type * from './dist/edge';

edge.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./dist/edge');

examples/basic-example/components/header.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import Link from 'next/link';
3-
import { useUser } from '@auth0/nextjs-auth0';
3+
import { useUser } from '@auth0/nextjs-auth0/client';
44

55
const Header = () => {
66
const { user } = useUser();

examples/basic-example/pages/_app.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { UserProvider } from '@auth0/nextjs-auth0';
2+
import { UserProvider } from '@auth0/nextjs-auth0/client';
33

44
export default function App({ Component, pageProps }) {
55
// If you've used `withAuth`, pageProps.user can pre-populate the hook

examples/basic-example/pages/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { useUser } from '@auth0/nextjs-auth0';
2+
import { useUser } from '@auth0/nextjs-auth0/client';
33

44
import Layout from '../components/layout';
55

examples/basic-example/pages/protected-page.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
2-
import { useUser, withPageAuthRequired } from '@auth0/nextjs-auth0';
2+
import { withPageAuthRequired } from '@auth0/nextjs-auth0';
3+
import { useUser } from '@auth0/nextjs-auth0/client';
34

45
import Layout from '../components/layout';
56

examples/kitchen-sink-example/.eslintrc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
"eslint:recommended",
66
"plugin:@typescript-eslint/eslint-recommended",
77
"plugin:@typescript-eslint/recommended",
8-
"plugin:import/recommended",
9-
"plugin:react/recommended",
8+
"plugin:@next/next/recommended",
109
"plugin:prettier/recommended"
1110
],
1211
"settings": {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
body {
2+
margin: 0;
3+
color: #333;
4+
font-family: -apple-system, 'Segoe UI';
5+
}
6+
.container {
7+
max-width: 42rem;
8+
margin: 1.5rem auto;
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
3+
export default function Head() {
4+
return (
5+
<>
6+
<title>Next.js with Auth0</title>
7+
</>
8+
);
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react';
2+
import './global.css';
3+
import Header from '../components/header';
4+
import { UserProvider } from '@auth0/nextjs-auth0/client';
5+
6+
export default function RootLayout({ children }: { children: React.ReactNode }) {
7+
return (
8+
<html lang="en">
9+
<UserProvider>
10+
<body>
11+
<Header />
12+
<div className="container">{children}</div>
13+
</body>
14+
</UserProvider>
15+
</html>
16+
);
17+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { headers } from 'next/headers';
2+
import { getSession as auth0GetSession } from '@auth0/nextjs-auth0';
3+
import { IncomingMessage, ServerResponse } from 'http';
4+
import { Socket } from 'net';
5+
6+
// Note: This is an experiment to test that the SDK works in the experimental app directory.
7+
// You should not rely on this code (or the app directory) in production.
8+
const reqRes = () => {
9+
const req = new IncomingMessage(new Socket());
10+
headers().forEach((v, k) => {
11+
req.headers[k] = v;
12+
});
13+
const res = new ServerResponse(req);
14+
return { req, res };
15+
};
16+
17+
export function getSession() {
18+
const { req, res } = reqRes();
19+
return auth0GetSession(req, res);
20+
}
21+
22+
export default async function ExperimentalRscPage() {
23+
const session = await getSession();
24+
// const session = {};
25+
return (
26+
<div>
27+
<h1>Profile</h1>
28+
<h4>Profile</h4>
29+
<pre>{JSON.stringify(session || {}, null, 2)}</pre>
30+
</div>
31+
);
32+
}

0 commit comments

Comments
 (0)