Skip to content

Commit

Permalink
Merge pull request #875 from auth0/next-response-status
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjmcgrath authored Oct 21, 2022
2 parents a52d23f + 7d9871e commit 29e36b7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/helpers/with-middleware-auth-required.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default function withMiddlewareAuthRequiredFactory(
if (cookies.length || authCookies.length) {
headers.set('set-cookie', [...authCookies, ...cookies].join(', '));
}
return NextResponse.next({ ...res, headers });
return NextResponse.next({ ...res, status: res.status, headers });
} else {
return authRes;
}
Expand Down
22 changes: 22 additions & 0 deletions tests/helpers/with-middleware-auth-required.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,28 @@ describe('with-middleware-auth-required', () => {
expect(middleware).toHaveBeenCalled();
});

test('should honor redirects in custom middleware for authenticated users', async () => {
const middleware = jest.fn().mockImplementation(() => {
return NextResponse.redirect('https://example.com/redirect');
});
const res = await setup({ middleware, user: { name: 'dave' } });
expect(middleware).toHaveBeenCalled();
expect(res.status).toEqual(307);
expect(res.headers.get('location')).toEqual('https://example.com/redirect');
expect(res.headers.get('set-cookie')).toMatch(/^appSession=/);
});

test('should honor rewrites in custom middleware for authenticated users', async () => {
const middleware = jest.fn().mockImplementation(() => {
return NextResponse.rewrite('https://example.com/rewrite');
});
const res = await setup({ middleware, user: { name: 'dave' } });
expect(middleware).toHaveBeenCalled();
expect(res.status).toEqual(200);
expect(res.headers.get('x-middleware-rewrite')).toEqual('https://example.com/rewrite');
expect(res.headers.get('set-cookie')).toMatch(/^appSession=/);
});

test('should set a session cookie if session is rolling', async () => {
const res = await setup({ user: { name: 'dave' } });
expect(res.status).toEqual(200);
Expand Down

0 comments on commit 29e36b7

Please sign in to comment.