Repository: auth0/nextjs-auth0 (ts)

Issue:
Req.cookies.getAll does not exist when using withMiddlewareAuthRequired on next@12
### Checklist

- [X] The issue can be reproduced in the [sample app](https://github.com/auth0-samples/auth0-nextjs-samples/tree/main/Sample-01) (or N/A).
- [X] I have looked into the [README](https://github.com/auth0/nextjs-auth0#readme) and have not found a suitable solution or answer.
- [X] I have looked into the [examples](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md) and have not found a suitable solution or answer.
- [X] I have looked into the [API documentation](https://auth0.github.io/nextjs-auth0/) and have not found a suitable solution or answer.
- [X] I have searched the [issues](https://github.com/auth0/nextjs-auth0/issues) and have not found a suitable solution or answer.
- [X] I have searched the [Auth0 Community](https://community.auth0.com/c/sdks/5) forums and have not found a suitable solution or answer.
- [X] I agree to the terms within the [Auth0 Code of Conduct](https://github.com/auth0/open-source-template/blob/master/CODE-OF-CONDUCT.md).

### Description

I tried to use the beta `withMiddlewareAuthRequired` on a `next@12.3.2` app but it throws regarding `req.cookies.getAll` does not exist. I noticed there is a somewhat similar issue (#892). I believe `req.cookies.getAll` is only available in `next@13` which is not mentioned in their documentation. It is not mentioned in the [migration guide from v1 to v2](https://github.com/auth0/nextjs-auth0/blob/vNext/V2_MIGRATION_GUIDE.md) of this library that it breaks compatibility with `next@12` though.

IMO the solution is to either bring back retro-compatibility or mention the breaking change in the migration documentation.

### Reproduction

I created a repro using the sample app that you can find here: https://github.com/auth0-samples/auth0-nextjs-samples/compare/main...Nargonath:auth0-nextjs-samples:cookies-getall-does-not-exist.

1. Clone the project locally.
2. Checkout the `cookies-getall-does-not-exist` branch.
3. Run `npm ci`.
4. Run `npm run dev`.
5. Execute:
```curl
curl --location --request GET 'http://localhost:3000/api/protected' \
--header 'Cookie: Cookie_1=value'
```
6. Notice the error:
![image](https://user-images.githubusercontent.com/583657/200533144-39ad60e0-5790-4ee7-884a-fe4ec9258fdb.png)


### SDK version

2.0.0-beta.2

### Next.js version

12.3.2

### Node.js version

16.15.1

Pull request description:
Fix edge cookie support for Next < 13.0.1
### 📋 Changes

In order to support Next <13.0.1 we need to support the old way Edge cookies were defined (as a Map) an addition to the new way they're defined (with `getAll`)

### 📎 References

fixes #899 
https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/core/lib/cookie.ts#L144-L154
https://github.com/vercel/next.js/pull/41526



Patch:
diff --git a/src/utils/middleware-cookies.ts b/src/utils/middleware-cookies.ts
index 52859fe6..d593bfdf 100644
--- a/src/utils/middleware-cookies.ts
+++ b/src/utils/middleware-cookies.ts
@@ -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;
+    }, {});
   }
 }


Test patch:
diff --git a/tests/utils/middleware-cookies.test.ts b/tests/utils/middleware-cookies.test.ts
index dd3ce8ba..7ee1e01c 100644
--- a/tests/utils/middleware-cookies.test.ts
+++ b/tests/utils/middleware-cookies.test.ts
@@ -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');