diff --git a/FAQ.md b/FAQ.md index 9b4c08e63..fd931db20 100644 --- a/FAQ.md +++ b/FAQ.md @@ -2,6 +2,7 @@ 1. [Why do I get a `checks.state argument is missing` error when logging in from different tabs?](#1-why-do-i-get-a-checks.state-argument-is-missing-error-if-i-try-to-log-in-from-different-tabs) 2. [How can I reduce the cookie size?](#2-how-can-i-reduce-the-cookie-size) +3. [I'm getting the warning/error `You should not access 'res' after getServerSideProps resolves.`](#3-i-m-getting-the-warning-error--you-should-not-access--res--after-getserversideprops-resolves.) ## 1. Why do I get a `checks.state argument is missing` error if I try to log in from different tabs? @@ -63,3 +64,11 @@ export default async function MyHandler(req, res) { ``` > Note: support for custom session stores [is in our roadmap](https://github.com/auth0/nextjs-auth0/issues/279). + +## 3. I'm getting the warning/error `You should not access 'res' after getServerSideProps resolves.` + +Because this SDK provides a rolling session by default, it writes to the header at the end of every request. This can cause the above warning when you use `getSession` or `getAccessToken` in >=Next.js 12, and an error if your `props` are defined as a `Promise`. + +Wrapping your `getServerSideProps` in `getServerSidePropsWrapper` will fix this because it will constrain the lifecycle of the session to the life of `getServerSideProps`. + +> Note: you should not use this if you are already using `withPageAuthenticationRequired` since this should already constrain the lifecycle of the session. diff --git a/README.md b/README.md index c7494add4..cb954d9d0 100644 --- a/README.md +++ b/README.md @@ -183,6 +183,7 @@ For other comprehensive examples, see the [EXAMPLES.md](./EXAMPLES.md) document. - [handleProfile](https://auth0.github.io/nextjs-auth0/modules/handlers_profile.html) - [withApiAuthRequired](https://auth0.github.io/nextjs-auth0/modules/helpers_with_api_auth_required.html) - [withPageAuthRequired](https://auth0.github.io/nextjs-auth0/modules/helpers_with_page_auth_required.html#withpageauthrequired) +- [getServerSidePropsWrapper](https://auth0.github.io/nextjs-auth0/modules/helpers_get_server_side_props_wrapper.html) - [getSession](https://auth0.github.io/nextjs-auth0/modules/session_get_session.html) - [getAccessToken](https://auth0.github.io/nextjs-auth0/modules/session_get_access_token.html) - [initAuth0](https://auth0.github.io/nextjs-auth0/modules/instance.html) diff --git a/src/helpers/get-server-side-props-wrapper.ts b/src/helpers/get-server-side-props-wrapper.ts new file mode 100644 index 000000000..71a953261 --- /dev/null +++ b/src/helpers/get-server-side-props-wrapper.ts @@ -0,0 +1,49 @@ +import { GetServerSideProps } from 'next'; +import SessionCache from '../session/cache'; + +/** + * If you're using >=Next 12 and {@link getSession} or {@link getAccessToken} without `withPageAuthRequired`, because + * you don't want to require authentication on your route, you might get a warning/error: "You should not access 'res' + * after getServerSideProps resolves". You can work around this by wrapping your `getServerSideProps` in + * `getServerSidePropsWrapper`, this ensures that the code that accesses `res` will run within + * the lifecycle of `getServerSideProps`, avoiding the warning/error eg: + * + * **NOTE: you do not need to do this if you're already using {@link WithPageAuthRequired}** + * + * ```js + * // pages/protected-page.js + * import { withPageAuthRequired } from '@auth0/nextjs-auth0'; + * + * export default function ProtectedPage() { + * return