-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmiddleware.ts
More file actions
62 lines (48 loc) · 1.69 KB
/
Copy pathmiddleware.ts
File metadata and controls
62 lines (48 loc) · 1.69 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
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
const ADMIN_USERNAME = 'admin';
const ADMIN_PASSWORD = 'eipsinsightv3';
const AUTH_REALM = 'EIPs Insight V3 Admin';
export function middleware(request: NextRequest) {
const authHeader = request.headers.get('authorization');
if (!isAuthorized(authHeader)) {
return new NextResponse('Authentication required.', {
status: 401,
headers: {
'WWW-Authenticate': `Basic realm="${AUTH_REALM}", charset="UTF-8"`,
},
});
}
const path = request.nextUrl.pathname;
// Check if the path is '/reviewers' in any case format
if (path.toLowerCase() === '/reviewers') {
console.log("redirected");
return NextResponse.redirect(new URL('/Reviewers', request.url));
}
if (path.toLowerCase() === '/analytics') {
console.log("redirected");
return NextResponse.redirect(new URL('/Analytics', request.url));
}
if (path.toLowerCase() === '/about') {
console.log("redirected");
return NextResponse.redirect(new URL('/About', request.url));
}
return NextResponse.next();
}
function isAuthorized(authHeader: string | null): boolean {
if (!authHeader || !authHeader.startsWith('Basic ')) return false;
try {
const encodedCredentials = authHeader.split(' ')[1] ?? '';
const decoded = atob(encodedCredentials);
const separatorIndex = decoded.indexOf(':');
if (separatorIndex < 0) return false;
const username = decoded.slice(0, separatorIndex);
const password = decoded.slice(separatorIndex + 1);
return username === ADMIN_USERNAME && password === ADMIN_PASSWORD;
} catch {
return false;
}
}
export const config = {
matcher: '/:path*',
};