-
Notifications
You must be signed in to change notification settings - Fork 8
/
middleware.ts
38 lines (33 loc) · 936 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
29
30
31
32
33
34
35
36
37
38
// Vercel middleware to password protect the staging deployment
export default function middleware(req: Request) {
const basicAuthPasswords = process.env.BASIC_AUTH_PASSWORDS;
if (basicAuthPasswords === undefined) {
return new Response(null, {
headers: {
'x-middleware-next': '1',
},
});
}
const basicAuth = req.headers.get('authorization');
if (basicAuth) {
const pwdLines = basicAuthPasswords.split(/\s+/);
const auth = basicAuth.split(' ')[1];
const [user, pwd] = atob(auth).split(':');
for (const line of pwdLines) {
const [u, p] = line.split(':');
if (user === u && pwd === p) {
return new Response(null, {
headers: {
'x-middleware-next': '1',
},
});
}
}
}
return new Response('Auth required', {
status: 401,
headers: {
'WWW-Authenticate': 'Basic realm="Secure Area"',
},
});
}