-
Notifications
You must be signed in to change notification settings - Fork 17
/
middleware.ts
41 lines (32 loc) · 971 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
import { NextResponse } from 'next/server'
import { cookies } from 'next/headers';
import isExpiredToken from './utilities/isExpiredToken';
const restrictedEndpoints = [
'/setup/health-check',
'/setup/node-sync',
'/dashboard',
'/dashboard/logs',
'/dashboard/settings',
'/dashboard/validators',
] as any
const redirectToInitPage = (request: any, nextPath: string) => {
return NextResponse.redirect(new URL(`/?redirect=${nextPath}`, request.url))
}
export async function middleware(request) {
const { pathname } = request.nextUrl
try {
if (!restrictedEndpoints.includes(pathname)) {
return NextResponse.next()
}
const token = cookies().get('session-token').value
if(!token) {
return redirectToInitPage(request, pathname)
}
if(isExpiredToken(token)) {
return redirectToInitPage(request, pathname)
}
return NextResponse.next()
} catch (e) {
return redirectToInitPage(request, pathname)
}
}