Skip to content

Commit

Permalink
Merge pull request #900 from auth0/next-12-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
redsky030428 committed Nov 8, 2022
2 parents a747101 + 6ef43cc commit 3ed7829
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/utils/middleware-cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ export default class MiddlewareCookies extends Cookies {
}

getAll(req: NextRequest): Record<string, string> {
return req.cookies.getAll().reduce((memo, { name, value }) => ({ ...memo, [name]: value }), {});
const { cookies } = req;
if (typeof cookies.getAll === 'function') {
return req.cookies.getAll().reduce((memo, { name, value }) => ({ ...memo, [name]: value }), {});
}
// Edge cookies before Next 13.0.1 have no `getAll` and extend `Map`.
const legacyCookies = cookies as unknown as Map<string, string>;
return Array.from(legacyCookies.keys()).reduce((memo: { [key: string]: string }, key) => {
memo[key] = legacyCookies.get(key) as string;
return memo;
}, {});
}
}
10 changes: 10 additions & 0 deletions tests/utils/middleware-cookies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ describe('cookie', () => {
expect(new MiddlewareCookies().getAll(req)).toMatchObject({ foo: 'bar', bar: 'baz' });
});

it('should get all cookies in Next < 13.0.1', async () => {
const req = {
cookies: new Map([
['foo', 'bar'],
['bar', 'baz']
])
} as unknown as NextRequest;
expect(new MiddlewareCookies().getAll(req)).toMatchObject({ foo: 'bar', bar: 'baz' });
});

it('should get a cookie by name', async () => {
const [req] = setup({ headers: { cookie: 'foo=bar; bar=baz;' } });
expect(new MiddlewareCookies().getAll(req)['foo']).toEqual('bar');
Expand Down

0 comments on commit 3ed7829

Please sign in to comment.