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

Fix for edge cookies delete not supporting domain or path #1341

Merged
merged 1 commit into from
Aug 3, 2023
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
2 changes: 1 addition & 1 deletion src/http/auth0-next-response-cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class Auth0NextResponseCookies extends Auth0ResponseCookies {
public clearCookie(name: string, options?: CookieSerializeOptions) {
const cookieSetter = cookies();
try {
cookieSetter.delete({ ...options, name, value: '' });
cookieSetter.set({ ...options, name, value: '', expires: new Date(0) });
} catch (_) {
warn();
}
Expand Down
2 changes: 1 addition & 1 deletion src/http/auth0-next-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class Auth0NextResponse extends Auth0Response<NextResponse> {
}

public clearCookie(name: string, options?: CookieSerializeOptions) {
this.res.cookies.delete({ ...options, name, value: '' });
this.setCookie(name, '', { ...options, expires: new Date(0) });
}

public redirect(location: string, status = 302): void {
Expand Down
18 changes: 18 additions & 0 deletions tests/http/auth0-next-response-cookies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ describe('auth0-next-response', () => {
expect(res.headers.get('set-cookie')).toEqual('foo=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT');
});

it('should delete cookies with a domain', async () => {
const cookies = new Auth0NextResponseCookies();
const res = new NextResponse();
jest.mocked(nextCookies).mockImplementation(() => res.cookies as any);
cookies.clearCookie('foo', { domain: 'example.com' });
expect(res.headers.get('set-cookie')).toEqual(
'foo=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Domain=example.com'
);
});

it('should delete cookies with a path', async () => {
const cookies = new Auth0NextResponseCookies();
const res = new NextResponse();
jest.mocked(nextCookies).mockImplementation(() => res.cookies as any);
cookies.clearCookie('foo', { path: '/foo' });
expect(res.headers.get('set-cookie')).toEqual('foo=; Path=/foo; Expires=Thu, 01 Jan 1970 00:00:00 GMT');
});

it('should not throw when deleting a cookie fails', async () => {
const cookies = new Auth0NextResponseCookies();
jest.mocked(nextCookies).mockImplementation(
Expand Down
22 changes: 20 additions & 2 deletions tests/http/auth0-next-response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,29 @@ describe('auth0-next-response', () => {
expect(auth0Res.res.headers.get('set-cookie')).toEqual(['foo=bar; Path=/', 'baz=qux; Path=/'].join(', '));
});

it('should clear cookies', async () => {
it('should delete a cookie', async () => {
const [, res] = setup();
const auth0Res = new Auth0NextResponse(res);
auth0Res.clearCookie('foo');

expect(auth0Res.res.headers.get('set-cookie')).toMatch(/foo=;.*Expires=.*1970/);
expect(auth0Res.res.headers.get('set-cookie')).toBe('foo=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT');
});

it('should delete a cookie with domain option', async () => {
const [, res] = setup();
const auth0Res = new Auth0NextResponse(res);
auth0Res.clearCookie('foo', { domain: 'example.com' });

expect(auth0Res.res.headers.get('set-cookie')).toBe(
'foo=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Domain=example.com'
);
});

it('should delete a cookie with path option', async () => {
const [, res] = setup();
const auth0Res = new Auth0NextResponse(res);
auth0Res.clearCookie('foo', { path: '/foo' });

expect(auth0Res.res.headers.get('set-cookie')).toBe('foo=; Path=/foo; Expires=Thu, 01 Jan 1970 00:00:00 GMT');
});
});