-
Notifications
You must be signed in to change notification settings - Fork 386
/
auth0-next-response.ts
42 lines (36 loc) · 1.25 KB
/
auth0-next-response.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
32
33
34
35
36
37
38
39
40
41
42
import { NextResponse } from 'next/server';
import type { CookieSerializeOptions } from 'cookie';
import { Auth0Response } from '../auth0-session/http';
export default class Auth0NextResponse extends Auth0Response<NextResponse> {
public constructor(res: NextResponse) {
/* c8 ignore next */
super(res);
}
public setCookie(name: string, value: string, options?: CookieSerializeOptions) {
this.res.cookies.set(name, value, options);
}
public clearCookie(name: string, options?: CookieSerializeOptions) {
this.setCookie(name, '', { ...options, expires: new Date(0) });
}
public redirect(location: string, status = 302): void {
const oldRes = this.res;
this.res = new NextResponse(null, { status });
oldRes.headers.forEach((value, key) => {
this.res.headers.set(key, value);
});
this.res.headers.set('location', location);
for (const cookie of oldRes.cookies.getAll()) {
this.res.cookies.set(cookie);
}
}
public setHeader(name: string, value: string) {
this.res.headers.set(name, value);
}
public send204() {
const oldRes = this.res;
this.res = new NextResponse(null, { status: 204 });
oldRes.headers.forEach((value, key) => {
this.res.headers.set(key, value);
});
}
}