-
Notifications
You must be signed in to change notification settings - Fork 391
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #664 from auth0/session-lifecycle
[SDK-3332] Constrain session lifecycle to `withPageAuthrequired` to avoid Next warning
- Loading branch information
Showing
16 changed files
with
241 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 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.