-
Notifications
You must be signed in to change notification settings - Fork 408
/
Copy pathauth0-next-request.ts
31 lines (29 loc) · 1.05 KB
/
auth0-next-request.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { Auth0Request } from '../auth0-session/http';
import { NextRequest } from 'next/server';
export default class Auth0NextRequest extends Auth0Request<NextRequest> {
public constructor(req: NextRequest) {
/* c8 ignore next */
super(req);
}
public getUrl(): string {
return this.req.url as string;
}
public getMethod(): string {
return this.req.method as string;
}
public async getBody(): Promise<Record<string, string> | string> {
return this.req.text();
}
public getCookies(): Record<string, string> {
const { cookies } = this.req;
if (typeof cookies.getAll === 'function') {
return this.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;
}, {});
}
}