Skip to content
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

[SDK-2259] Remove env var dependency at build time #255

Merged
merged 3 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/helpers/with-page-auth-required.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
Expand All @@ -101,13 +101,13 @@ export default function withPageAuthRequiredFactory(sessionCache: SessionCache):
const { getServerSideProps, loginUrl = '/api/auth/login' } = optsOrComponent;
return async (ctx: GetServerSidePropsContext): Promise<GetServerSidePropsResultWithSession> => {
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);
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -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;
});
});