-
Notifications
You must be signed in to change notification settings - Fork 7
/
middleware.ts
28 lines (24 loc) · 862 Bytes
/
middleware.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
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
export const config = {
matcher: ['/api/:path*', '/subscriptions']
}
export function middleware(request: NextRequest) {
try {
const requestHeaders = new Headers(request.headers)
const accessToken = request.cookies.getAll().filter(cookie => cookie.name.includes('accessToken'));
requestHeaders.delete("cookie");
if (accessToken.length === 1) {
requestHeaders.set('Authorization', 'Bearer ' + accessToken[0].value);
}
requestHeaders.set('X-Api-Key', process.env.PEPY_API_KEY!);
return NextResponse.next({
request: {
// New request headers
headers: requestHeaders,
},
});
} catch (error) {
console.log(error);
}
}