Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing an intial user from the server to <Auth0Provider user={session?.user}> with Pages Router #1916

Open
5 tasks done
florisdg opened this issue Feb 13, 2025 · 4 comments
Labels

Comments

@florisdg
Copy link

florisdg commented Feb 13, 2025

Checklist

  • I have looked into the Readme, Examples, and FAQ and have not found a suitable solution or answer.
  • I have looked into the API documentation and have not found a suitable solution or answer.
  • I have searched the issues and have not found a suitable solution or answer.
  • I have searched the Auth0 Community forums and have not found a suitable solution or answer.
  • I agree to the terms within the Auth0 Code of Conduct.

Describe the problem you'd like to have solved

In v3 I used to have this in my Next.js app with Pages router, in the file src/pages/_app.tsx:

export function App({ Component, pageProps, router }: AppPropsWithLayout) {
  const getLayout = Component.getLayout ?? ((page: ReactNode) => page)

  return <UserProvider user={pageProps.user}>
...

Every page using getServerSideProps was wrapped with withPageAuthRequired, which from my understanding would populate this 'user' property in the pageProps shown above. This would make the 'user' object immediately available on first-render with pages that use getServerSideProps.

In v4 this is obviously gone, and the V4_MIGRATION_GUIDE.md (nearly all of the links in this guide are broken) points towards this in EXAMPLES.md:

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>
  )
}

However, when I do what the example suggests in a _app.tsx file like this...:

export function App({ Component, pageProps, router }: AppPropsWithLayout) {
  const getLayout = Component.getLayout ?? ((page: ReactNode) => page)

  const session = await auth0.getSession()

  return <Auth0Provider user={session?.user}>
...

... it won't work because this file cannot be asynchronous. The same goes for when I attempt aynthing like it my Layout files. (Here in the Next.js docs you see no async in any of the layout files or _app.tsx)

I am clueless how else I can pass an initial user from the server. I need this back so users do not see a 'flash' of empty content until the useUser() hook has retrieved the user.

Describe the ideal solution

Get the same functionality from v3 back in v4's for apps with Page Router

Alternatives and current workarounds

The only thing I could think of is having to add this to all of our pages with getSeverSideProps:

export async function getServerSideProps(ctx: GetServerSidePropsContext) {
  const session =  await auth0.getSession(ctx.req)

  if(!session?.user) {
    return {
      redirect: {
        destination: '/auth/login',
        permanent: false,
      },
    }
  }

  return {
    props: {
      user: session.user,
    },
  }
}

export default function MyPage({user}: InferGetServerSidePropsType<typeof getServerSideProps>) {...}

But this does populate the the user returned by the useUser() hook...

Additional context

No response

@tusharpandey13
Copy link
Contributor

Thank you for reporting this, we are actively looking into this. We will post an update here once we have found a resolution.

@wjessup
Copy link

wjessup commented Feb 16, 2025

coming here to comment that the link:

If you would like to pass an initial user during server rendering to be available to the useUser() hook, you can wrap your components with the new (see example).

in the

is broken.

infact, even the link to the guide in the release is broken... so finding the migration guide was a problem.

@wjessup
Copy link

wjessup commented Feb 16, 2025

could you please explain in the documentation what this means "If you would like to pass an initial user during server rendering to be available to the useUser() hook"

starting from what goal a developer is trying to achieve? "if you want to achieve X then do Y..."

@florisdg
Copy link
Author

florisdg commented Feb 17, 2025

@wjessup the way it used to work is that every page with getServerSideProps would add a 'user' object to the page props, making it available on first render.

Additionally if somewhere in the page or app's tree the useUser() hook is used, the page props user object would pre-populate the useUser()'s value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants