-
Notifications
You must be signed in to change notification settings - Fork 432
[SDK-3332] Constrain session lifecycle to withPageAuthrequired
to avoid Next warning
#664
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
17f38cf
Constrain session lifecycle to withPageAuthrequired to avoid Next war…
adamjmcgrath fdfacd1
fix build
adamjmcgrath 584534c
fix lint
adamjmcgrath 5805ae8
iat's are unique to request
adamjmcgrath e523099
Add `getServerSidePropsWrapper`
adamjmcgrath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,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 <div>Protected content</div>; | ||
* } | ||
* | ||
* export const getServerSideProps = getServerSidePropsWrapper(async (ctx) => { | ||
* const session = getSession(ctx.req, ctx.res); | ||
* if (session) { | ||
* // Use is authenticated | ||
* } else { | ||
* // User is not authenticated | ||
* } | ||
* }); | ||
* ``` | ||
* | ||
* @category Server | ||
*/ | ||
export type GetServerSidePropsWrapper = (getServerSideProps: GetServerSideProps) => GetServerSideProps; | ||
|
||
/** | ||
* @ignore | ||
*/ | ||
export default function getServerSidePropsWrapperFactory(getSessionCache: () => SessionCache) { | ||
return function getServerSidePropsWrapper(getServerSideProps: GetServerSideProps): GetServerSideProps { | ||
return function wrappedGetServerSideProps(...args) { | ||
const sessionCache = getSessionCache(); | ||
const [ctx] = args; | ||
sessionCache.init(ctx.req, ctx.res, false); | ||
const ret = getServerSideProps(...args); | ||
sessionCache.save(ctx.req, ctx.res); | ||
return ret; | ||
}; | ||
}; | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,8 +1,8 @@ | ||
import React from 'react'; | ||
import { NextPageContext } from 'next'; | ||
|
||
export default function protectedPage(): React.ReactElement { | ||
return <div>Protected Page</div>; | ||
export default function protectedPage({ user }: { user?: { sub: string } }): React.ReactElement { | ||
return <div>Protected Page {user ? user.sub : ''}</div>; | ||
} | ||
|
||
export const getServerSideProps = (ctx: NextPageContext): any => (global as any).withPageAuthRequired()(ctx); |
18 changes: 18 additions & 0 deletions
18
tests/fixtures/test-app/pages/wrapped-get-server-side-props.tsx
This file contains hidden or 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,18 @@ | ||
import React from 'react'; | ||
import { NextPageContext } from 'next'; | ||
|
||
export default function wrappedGetServerSidePropsPage({ | ||
isAuthenticated | ||
}: { | ||
isAuthenticated?: boolean; | ||
}): React.ReactElement { | ||
return <div>isAuthenticated: {String(isAuthenticated)}</div>; | ||
} | ||
|
||
export const getServerSideProps = (_ctx: NextPageContext): any => | ||
(global as any).getServerSidePropsWrapper(async (ctx: NextPageContext) => { | ||
const session = (global as any).getSession(ctx.req, ctx.res); | ||
const asyncProps = (global as any).asyncProps; | ||
const props = { isAuthenticated: !!session }; | ||
return { props: asyncProps ? Promise.resolve(props) : props }; | ||
})(_ctx); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.