Skip to content

Commit

Permalink
Merge pull request #800 from auth0/feature/override-user-prop
Browse files Browse the repository at this point in the history
Allow to override the `user` prop in server-side rendered pages [SDK-3560]
  • Loading branch information
adamjmcgrath authored Sep 1, 2022
2 parents 3939ef3 + 20e1855 commit 9a479c0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/helpers/with-page-auth-required.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ export default function withPageAuthRequiredFactory(
ret = await getServerSideProps(ctx);
}
if (ret.props instanceof Promise) {
return { ...ret, props: ret.props.then((props: any) => ({ ...props, user: session.user })) };
return { ...ret, props: ret.props.then((props: any) => ({ user: session.user, ...props })) };
}
return { ...ret, props: { ...ret.props, user: session.user } };
return { ...ret, props: { user: session.user, ...ret.props } };
};
};
}
26 changes: 26 additions & 0 deletions tests/helpers/with-page-auth-required.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,32 @@ describe('with-page-auth-required ssr', () => {
expect(spy).toHaveBeenCalledWith(expect.objectContaining({ req: expect.anything(), res: expect.anything() }));
});

test('allow to override the user prop', async () => {
const baseUrl = await setup(withoutApi, {
withPageAuthRequiredOptions: {
async getServerSideProps() {
return { props: { user: { sub: 'foo' } } };
}
}
});
const cookieJar = await login(baseUrl);
const { data } = await get(baseUrl, '/protected', { cookieJar, fullResponse: true });
expect(data).toMatch(/Protected Page.*foo/);
});

test('allow to override the user prop when using aync props', async () => {
const baseUrl = await setup(withoutApi, {
withPageAuthRequiredOptions: {
async getServerSideProps() {
return { props: Promise.resolve({ user: { sub: 'foo' } }) };
}
}
});
const cookieJar = await login(baseUrl);
const { data } = await get(baseUrl, '/protected', { cookieJar, fullResponse: true });
expect(data).toMatch(/Protected Page.*foo/);
});

test('use a custom login url', async () => {
process.env.NEXT_PUBLIC_AUTH0_LOGIN = '/api/foo';
const baseUrl = await setup(withoutApi);
Expand Down

0 comments on commit 9a479c0

Please sign in to comment.