Skip to content

Commit 726a8ed

Browse files
authored
4.0.0-beta.9 (#1827)
1 parent 7a8e677 commit 726a8ed

File tree

8 files changed

+81
-7
lines changed

8 files changed

+81
-7
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
### 1. Install the SDK
66

77
```shell
8-
npm i @auth0/nextjs-auth0@4.0.0-beta.8
8+
npm i @auth0/nextjs-auth0@4.0.0-beta.9
99
```
1010

1111
### 2. Add the environment variables
@@ -322,6 +322,36 @@ export default async function handler(
322322
}
323323
```
324324

325+
## `<Auth0Provider />`
326+
327+
### Passing an initial user from the server
328+
329+
You can wrap your components in an `<Auth0Provider />` and pass an initial user object to make it available to your components using the `useUser()` hook. For example:
330+
331+
```tsx
332+
import { Auth0Provider } from "@auth0/nextjs-auth0"
333+
334+
import { auth0 } from "@/lib/auth0"
335+
336+
export default async function RootLayout({
337+
children,
338+
}: Readonly<{
339+
children: React.ReactNode
340+
}>) {
341+
const session = await auth0.getSession()
342+
343+
return (
344+
<html lang="en">
345+
<body>
346+
<Auth0Provider user={session?.user}>{children}</Auth0Provider>
347+
</body>
348+
</html>
349+
)
350+
}
351+
```
352+
353+
The loaded user will then be used as a fallback in `useUser()` hook.
354+
325355
## Hooks
326356

327357
The SDK exposes hooks to enable you to provide custom logic that would be run at certain lifecycle events.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@auth0/nextjs-auth0",
3-
"version": "4.0.0-beta.8",
3+
"version": "4.0.0-beta.9",
44
"description": "Auth0 Next.js SDK",
55
"main": "dist/index.js",
66
"scripts": {

src/client/hooks/use-user.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ export function useUser() {
1717
})
1818
)
1919

20+
// if we have the user loaded via the provider, return it
21+
if (data) {
22+
return {
23+
user: data,
24+
isLoading: false,
25+
error: null,
26+
}
27+
}
28+
2029
if (error) {
2130
return {
2231
user: null,

src/client/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { useUser } from "./hooks/use-user"
22
export { getAccessToken } from "./helpers/get-access-token"
3+
export { Auth0Provider } from "./providers/auth0-provider"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"use client"
2+
3+
import React from "react"
4+
import { SWRConfig } from "swr"
5+
6+
import { User } from "../../types"
7+
8+
export function Auth0Provider({
9+
user,
10+
children,
11+
}: {
12+
user?: User
13+
children: React.ReactNode
14+
}) {
15+
return (
16+
<SWRConfig
17+
value={{
18+
fallback: {
19+
"/auth/profile": user,
20+
},
21+
}}
22+
>
23+
{children}
24+
</SWRConfig>
25+
)
26+
}

src/server/auth-client.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,11 @@ describe("Authentication Client", async () => {
13371337
// query parameters
13381338
expect(logoutUrl.searchParams.get("client_id")).toEqual(DEFAULT.clientId)
13391339
expect(logoutUrl.searchParams.get("returnTo")).toEqual(DEFAULT.appBaseUrl)
1340+
1341+
// session cookie is cleared
1342+
const cookie = response.cookies.get("__session")
1343+
expect(cookie?.value).toEqual("")
1344+
expect(cookie?.expires).toEqual(new Date("1970-01-01T00:00:00.000Z"))
13401345
})
13411346

13421347
it("should return an error if the discovery endpoint could not be fetched", async () => {

src/server/auth-client.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,10 @@ export class AuthClient {
287287
const url = new URL("/v2/logout", this.issuer)
288288
url.searchParams.set("returnTo", returnTo)
289289
url.searchParams.set("client_id", this.clientMetadata.client_id)
290-
return NextResponse.redirect(url)
290+
291+
const res = NextResponse.redirect(url)
292+
await this.sessionStore.delete(req.cookies, res.cookies)
293+
return res
291294
}
292295

293296
const url = new URL(authorizationServerMetadata.end_session_endpoint)

src/server/client.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export class Auth0Client {
180180
*
181181
* This method can be used in Server Components, Server Actions, Route Handlers, and middleware in the **App Router**.
182182
*/
183-
async getAccessToken(): Promise<{ token: string; expiresAt: number } | null>
183+
async getAccessToken(): Promise<{ token: string; expiresAt: number }>
184184

185185
/**
186186
* getAccessToken returns the access token.
@@ -189,14 +189,14 @@ export class Auth0Client {
189189
*/
190190
async getAccessToken(
191191
req: PagesRouterRequest
192-
): Promise<{ token: string; expiresAt: number } | null>
192+
): Promise<{ token: string; expiresAt: number }>
193193

194194
/**
195195
* getAccessToken returns the access token.
196196
*/
197197
async getAccessToken(
198198
req?: PagesRouterRequest
199-
): Promise<{ token: string; expiresAt: number } | null> {
199+
): Promise<{ token: string; expiresAt: number }> {
200200
let session: SessionData | null = null
201201

202202
if (req) {
@@ -212,7 +212,7 @@ export class Auth0Client {
212212
)
213213
}
214214

215-
// if access token has expired, return null
215+
// if access token has expired, throw an error
216216
if (session.tokenSet.expiresAt <= Date.now() / 1000) {
217217
if (!session.tokenSet.refreshToken) {
218218
throw new AccessTokenError(

0 commit comments

Comments
 (0)