If you have any server-side rendered pages (using getServerSideProps or Server Components), you should get the
user from the server-side session and pass it to the <UserProvider> component via the user
prop. This will prefill the useUser hook with the UserProfile object.
For example:
exportdefaultfunctionApp({ Component, pageProps }) { // If you've used `withPageAuthRequired`, `pageProps.user` can prefill the hook // if you haven't used `withPageAuthRequired`, `pageProps.user` is undefined so the hook // fetches the user from the API route const { user } = pageProps;
exportdefaultasyncfunctionRootLayout({ children }) { // this will emit a warning because Server Components cannot write to cookies // see https://github.com/auth0/nextjs-auth0#using-this-sdk-with-react-server-components constsession = awaitgetSession();
In client-side rendered pages, the useUser hook uses a UserFetcher to fetch the
user from the profile API route. If needed, you can specify a custom fetcher here in the
fetcher option.
IMPORTANT If you have used a custom url for your HandleProfile API route handler
(the default is /api/auth/me) then you need to specify it here in the profileUrl option.
Configure the UserProvider component.
If you have any server-side rendered pages (using
getServerSideProps
or Server Components), you should get the user from the server-side session and pass it to the<UserProvider>
component via theuser
prop. This will prefill the useUser hook with the UserProfile object. For example:or
In client-side rendered pages, the useUser hook uses a UserFetcher to fetch the user from the profile API route. If needed, you can specify a custom fetcher here in the
fetcher
option.IMPORTANT If you have used a custom url for your HandleProfile API route handler (the default is
/api/auth/me
) then you need to specify it here in theprofileUrl
option.