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

Add example for getting the session in getInitialProps #4713

Conversation

chrisrhymes
Copy link

☕️ Reasoning

Adding an example to the docs for how to get the session in getInitialProps as this took me a while to figure it out and thought it would be useful for other users.

As the docs already says, this is useful if you want to protect every page in your site, so it seems like it would be a common usage scenario.

🧢 Checklist

  • Documentation
  • Tests
  • Ready to be merged

🎫 Affected issues

Please scout and link issues that might be solved by this PR.

Fixes: Example with getSession() in _app.js?

📌 Resources

@vercel
Copy link

vercel bot commented Jun 15, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
next-auth ✅ Ready (Inspect) Visit Preview Jun 15, 2022 at 2:58PM (UTC)

@vercel vercel bot temporarily deployed to Preview June 15, 2022 08:01 Inactive
import App from "next/app";
import { getSession } from "next-auth/react";
import { Session } from "next-auth";

Copy link
Member

@ndom91 ndom91 Jun 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe put the whole

export default function App({
  Component,
  pageProps: { session, ...pageProps },
}) {
  return (
    // `session` comes from `getServerSideProps` or `getInitialProps`.
    // Avoids flickering/session loading on first load.
    <SessionProvider session={session} refetchInterval={5 * 60}>
      <Component {...pageProps} />
    </SessionProvider>
  )
}

Part back into here to make it more straightforward to follow?

@vercel vercel bot temporarily deployed to Preview June 15, 2022 14:58 Inactive
@balazsorban44
Copy link
Member

As getInitialProps is not pushed by the Next.js team, I don't think we should either. Thanks for the PR though!

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

Successfully merging this pull request may close these issues.

Example with getSession() in _app.js?
3 participants