-
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.
- Loading branch information
1 parent
5805ae8
commit e523099
Showing
13 changed files
with
181 additions
and
89 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
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); |
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,53 @@ | ||
import { login, setup, teardown } from '../fixtures/setup'; | ||
import { withoutApi } from '../fixtures/default-settings'; | ||
import { get } from '../auth0-session/fixtures/helpers'; | ||
|
||
describe('get-server-side-props-wrapper', () => { | ||
afterEach(teardown); | ||
|
||
test('wrap getServerSideProps', async () => { | ||
const baseUrl = await setup(withoutApi); | ||
|
||
const { | ||
res: { statusCode }, | ||
data | ||
} = await get(baseUrl, '/wrapped-get-server-side-props', { fullResponse: true }); | ||
expect(statusCode).toBe(200); | ||
expect(data).toMatch(/isAuthenticated: .*false/); | ||
}); | ||
|
||
test('wrap getServerSideProps with session', async () => { | ||
const baseUrl = await setup(withoutApi); | ||
const cookieJar = await login(baseUrl); | ||
|
||
const { | ||
res: { statusCode }, | ||
data | ||
} = await get(baseUrl, '/wrapped-get-server-side-props', { fullResponse: true, cookieJar }); | ||
expect(statusCode).toBe(200); | ||
expect(data).toMatch(/isAuthenticated: .*true/); | ||
}); | ||
|
||
test('wrap getServerSideProps with async props', async () => { | ||
const baseUrl = await setup(withoutApi, { asyncProps: true }); | ||
|
||
const { | ||
res: { statusCode }, | ||
data | ||
} = await get(baseUrl, '/wrapped-get-server-side-props', { fullResponse: true }); | ||
expect(statusCode).toBe(200); | ||
expect(data).toMatch(/isAuthenticated: .*false/); | ||
}); | ||
|
||
test('wrap getServerSideProps with async props and session', async () => { | ||
const baseUrl = await setup(withoutApi, { asyncProps: true }); | ||
const cookieJar = await login(baseUrl); | ||
|
||
const { | ||
res: { statusCode }, | ||
data | ||
} = await get(baseUrl, '/wrapped-get-server-side-props', { fullResponse: true, cookieJar }); | ||
expect(statusCode).toBe(200); | ||
expect(data).toMatch(/isAuthenticated: .*true/); | ||
}); | ||
}); |
Oops, something went wrong.