-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.ts
More file actions
80 lines (65 loc) · 2.77 KB
/
proxy.ts
File metadata and controls
80 lines (65 loc) · 2.77 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { NextRequest, NextResponse } from 'next/server';
import { getCookieByName } from './lib/cookies';
import { CONSENT_GIVEN_COOKIE_NAME, ADMIN_PASSWORD_CORRECT_COOKIE_NAME } from './constants/constants';
export async function proxy(request: NextRequest) {
const nextPath = request.nextUrl.pathname;
// Skip API routes
if (nextPath.startsWith('/api')) {
return NextResponse.next();
}
// consent is already given, proceed
if (nextPath === '/consent') {
const shouldAskConsent = process.env.NEXT_PUBLIC_ASK_CONSENT === 'true';
if (shouldAskConsent) {
const consentGivenDateCookie = await getCookieByName(CONSENT_GIVEN_COOKIE_NAME);
const consentGivenDate: string | undefined = consentGivenDateCookie?.value;
if (consentGivenDate !== undefined) {
return NextResponse.redirect(new URL('/', request.url));
}
return NextResponse.next();
}
return NextResponse.next();
}
if (nextPath.startsWith('/')) {
const shouldAskConsent = process.env.NEXT_PUBLIC_ASK_CONSENT === 'true';
// check if consent is needed
if (shouldAskConsent) {
const consentGivenDateCookie = await getCookieByName(CONSENT_GIVEN_COOKIE_NAME);
const consentGivenDate: string | undefined = consentGivenDateCookie?.value;
const nextPathname = request.nextUrl.pathname;
const nextPathSearchParams = request.nextUrl.search;
const nextPathFullUrl: string = `${nextPathname}${nextPathSearchParams}`;
const redirectUrlParams = new URLSearchParams();
redirectUrlParams.set('redirectUrl', nextPathFullUrl);
const nextUrl = new URL('/consent', request.url);
nextUrl.search = redirectUrlParams.toString();
if (consentGivenDate === undefined) {
return NextResponse.redirect(nextUrl);
}
}
// check if admin password is in cookies already
if (nextPath === '/login') {
const isAdminPWCorrect = await getCookieByName(ADMIN_PASSWORD_CORRECT_COOKIE_NAME);
const isCorrect: boolean = isAdminPWCorrect?.value === 'true';
if (isCorrect) {
return NextResponse.redirect(new URL('/admin', request.url));
}
return NextResponse.next();
}
// redirect to login if admin password is not in cookies
if (nextPath === '/admin') {
const isAdminPWCorrect = await getCookieByName(ADMIN_PASSWORD_CORRECT_COOKIE_NAME);
const isCorrect: boolean = isAdminPWCorrect?.value === 'true';
if (!isCorrect) {
return NextResponse.redirect(new URL('/login', request.url));
}
return NextResponse.next();
}
return NextResponse.next();
}
return NextResponse.next();
}
export const config = {
// DO not match the route: /consent, and /welcome and /welcome2
matcher: ['/((?!.*\\..*|_next|welcome|welcome2).*)', '/', '/(api|trpc)(.*)'],
};