Skip to content

Commit

Permalink
4.0.0-beta.9 (#1827)
Browse files Browse the repository at this point in the history
  • Loading branch information
guabu authored Dec 3, 2024
1 parent 7a8e677 commit 726a8ed
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 7 deletions.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
### 1. Install the SDK

```shell
npm i @auth0/[email protected].8
npm i @auth0/[email protected].9
```

### 2. Add the environment variables
Expand Down Expand Up @@ -322,6 +322,36 @@ export default async function handler(
}
```

## `<Auth0Provider />`

### Passing an initial user from the server

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:

```tsx
import { Auth0Provider } from "@auth0/nextjs-auth0"

import { auth0 } from "@/lib/auth0"

export default async function RootLayout({
children,
}: Readonly<{
children: React.ReactNode
}>) {
const session = await auth0.getSession()

return (
<html lang="en">
<body>
<Auth0Provider user={session?.user}>{children}</Auth0Provider>
</body>
</html>
)
}
```

The loaded user will then be used as a fallback in `useUser()` hook.

## Hooks

The SDK exposes hooks to enable you to provide custom logic that would be run at certain lifecycle events.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@auth0/nextjs-auth0",
"version": "4.0.0-beta.8",
"version": "4.0.0-beta.9",
"description": "Auth0 Next.js SDK",
"main": "dist/index.js",
"scripts": {
Expand Down
9 changes: 9 additions & 0 deletions src/client/hooks/use-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ export function useUser() {
})
)

// if we have the user loaded via the provider, return it
if (data) {
return {
user: data,
isLoading: false,
error: null,
}
}

if (error) {
return {
user: null,
Expand Down
1 change: 1 addition & 0 deletions src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { useUser } from "./hooks/use-user"
export { getAccessToken } from "./helpers/get-access-token"
export { Auth0Provider } from "./providers/auth0-provider"
26 changes: 26 additions & 0 deletions src/client/providers/auth0-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use client"

import React from "react"
import { SWRConfig } from "swr"

import { User } from "../../types"

export function Auth0Provider({
user,
children,
}: {
user?: User
children: React.ReactNode
}) {
return (
<SWRConfig
value={{
fallback: {
"/auth/profile": user,
},
}}
>
{children}
</SWRConfig>
)
}
5 changes: 5 additions & 0 deletions src/server/auth-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,11 @@ describe("Authentication Client", async () => {
// query parameters
expect(logoutUrl.searchParams.get("client_id")).toEqual(DEFAULT.clientId)
expect(logoutUrl.searchParams.get("returnTo")).toEqual(DEFAULT.appBaseUrl)

// session cookie is cleared
const cookie = response.cookies.get("__session")
expect(cookie?.value).toEqual("")
expect(cookie?.expires).toEqual(new Date("1970-01-01T00:00:00.000Z"))
})

it("should return an error if the discovery endpoint could not be fetched", async () => {
Expand Down
5 changes: 4 additions & 1 deletion src/server/auth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,10 @@ export class AuthClient {
const url = new URL("/v2/logout", this.issuer)
url.searchParams.set("returnTo", returnTo)
url.searchParams.set("client_id", this.clientMetadata.client_id)
return NextResponse.redirect(url)

const res = NextResponse.redirect(url)
await this.sessionStore.delete(req.cookies, res.cookies)
return res
}

const url = new URL(authorizationServerMetadata.end_session_endpoint)
Expand Down
8 changes: 4 additions & 4 deletions src/server/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export class Auth0Client {
*
* This method can be used in Server Components, Server Actions, Route Handlers, and middleware in the **App Router**.
*/
async getAccessToken(): Promise<{ token: string; expiresAt: number } | null>
async getAccessToken(): Promise<{ token: string; expiresAt: number }>

/**
* getAccessToken returns the access token.
Expand All @@ -189,14 +189,14 @@ export class Auth0Client {
*/
async getAccessToken(
req: PagesRouterRequest
): Promise<{ token: string; expiresAt: number } | null>
): Promise<{ token: string; expiresAt: number }>

/**
* getAccessToken returns the access token.
*/
async getAccessToken(
req?: PagesRouterRequest
): Promise<{ token: string; expiresAt: number } | null> {
): Promise<{ token: string; expiresAt: number }> {
let session: SessionData | null = null

if (req) {
Expand All @@ -212,7 +212,7 @@ export class Auth0Client {
)
}

// if access token has expired, return null
// if access token has expired, throw an error
if (session.tokenSet.expiresAt <= Date.now() / 1000) {
if (!session.tokenSet.refreshToken) {
throw new AccessTokenError(
Expand Down

0 comments on commit 726a8ed

Please sign in to comment.