-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
81 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters