-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
_middleware.ts
43 lines (31 loc) · 929 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
39
40
41
42
43
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { decodeJwt } from '../lib/jwt'
import { utils } from 'ethers'
const PROTECTED_ROUTES = new Set(['/'])
export function middleware(req: NextRequest) {
let response = NextResponse.next()
const url = req.nextUrl.clone()
if (!PROTECTED_ROUTES.has(req.nextUrl.pathname)) {
return response
}
const publicKey = process.env.PUBLIC_KEY
if (!publicKey) {
console.error('PUBLIC_KEY environment variable is not set')
return response
}
const cookie = req.cookies['allow-list']
if (!cookie || cookie === 'deleted') {
return response
}
const data = decodeJwt(cookie)
const approvedBy = utils.verifyMessage(
`${data.address}-approved`,
data.tokenVerification
)
if (approvedBy !== publicKey) {
return response
}
url.pathname = '/protected'
return NextResponse.rewrite(url)
}