diff --git a/src/helpers/with-page-auth-required.ts b/src/helpers/with-page-auth-required.ts index 4ce73a3a0..9e0063bee 100644 --- a/src/helpers/with-page-auth-required.ts +++ b/src/helpers/with-page-auth-required.ts @@ -1,5 +1,5 @@ import { GetServerSideProps, GetServerSidePropsContext, GetServerSidePropsResult } from 'next'; -import { Claims, Session, SessionCache } from '../session'; +import { Claims, GetSession } from '../session'; import { assertCtx } from '../utils/assert'; import React, { ComponentType } from 'react'; import { WithPageAuthRequiredOptions as WithPageAuthRequiredCSROptions } from '../frontend/with-page-auth-required'; @@ -90,7 +90,7 @@ export type WithPageAuthRequired = { /** * @ignore */ -export default function withPageAuthRequiredFactory(sessionCache: SessionCache): WithPageAuthRequired { +export default function withPageAuthRequiredFactory(getSession: GetSession): WithPageAuthRequired { return ( optsOrComponent: WithPageAuthRequiredOptions | ComponentType = {}, csrOpts?: WithPageAuthRequiredCSROptions @@ -101,13 +101,13 @@ export default function withPageAuthRequiredFactory(sessionCache: SessionCache): const { getServerSideProps, loginUrl = '/api/auth/login' } = optsOrComponent; return async (ctx: GetServerSidePropsContext): Promise => { assertCtx(ctx); - if (!sessionCache.isAuthenticated(ctx.req, ctx.res)) { + const session = getSession(ctx.req, ctx.res); + if (!session?.user) { // 10 - redirect // 9.5.4 - unstable_redirect // 9.4 - res.setHeaders return { redirect: { destination: `${loginUrl}?returnTo=${ctx.resolvedUrl}`, permanent: false } }; } - const session = sessionCache.get(ctx.req, ctx.res) as Session; let ret: any = { props: {} }; if (getServerSideProps) { ret = await getServerSideProps(ctx); diff --git a/src/index.ts b/src/index.ts index 106f3fd6e..2fbec1845 100644 --- a/src/index.ts +++ b/src/index.ts @@ -61,7 +61,7 @@ export const initAuth0: InitAuth0 = (params) => { const getSession = sessionFactory(sessionCache); const getAccessToken = accessTokenFactory(getClient, config, sessionCache); const withApiAuthRequired = withApiAuthRequiredFactory(sessionCache); - const withPageAuthRequired = withPageAuthRequiredFactory(sessionCache); + const withPageAuthRequired = withPageAuthRequiredFactory(getSession); const handleLogin = loginHandler(config, getClient, transientStore); const handleLogout = logoutHandler(config, getClient, sessionCache); const handleCallback = callbackHandler(config, getClient, sessionCache, transientStore); @@ -85,7 +85,7 @@ export const getSession: GetSession = (...args) => getInstance().getSession(...a export const getAccessToken: GetAccessToken = (...args) => getInstance().getAccessToken(...args); export const withApiAuthRequired: WithApiAuthRequired = (...args) => getInstance().withApiAuthRequired(...args); export const withPageAuthRequired: WithPageAuthRequired = (...args: any[]): any => - getInstance().withPageAuthRequired(...args); + withPageAuthRequiredFactory(getSession)(...args); export const handleLogin: HandleLogin = (...args) => getInstance().handleLogin(...args); export const handleLogout: HandleLogout = (...args) => getInstance().handleLogout(...args); export const handleCallback: HandleCallback = (...args) => getInstance().handleCallback(...args); diff --git a/tests/index.test.ts b/tests/index.test.ts new file mode 100644 index 000000000..e8b8026c7 --- /dev/null +++ b/tests/index.test.ts @@ -0,0 +1,11 @@ +import { withPageAuthRequired, withApiAuthRequired } from '../src'; + +describe('index', () => { + test('withPageAuthRequired should not create an SDK instance at build time', () => { + const secret = process.env.AUTH0_SECRET; + delete process.env.AUTH0_SECRET; + expect(() => withApiAuthRequired(jest.fn())).toThrow('"secret" is required'); + expect(() => withPageAuthRequired()).not.toThrow(); + process.env.AUTH0_SECRET = secret; + }); +});