-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
38 lines (33 loc) · 1.29 KB
/
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
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { VerifyAuthToken } from './lib/authToken';
const APP_URL = process.env.APP_URL;
export async function middleware(request: NextRequest) {
const currentPath = request.nextUrl.pathname
const publicPaths = ['/login', '/register']
const token = request.cookies.get('token')?.value;
if (!token && !publicPaths.includes(currentPath)) {
return NextResponse.redirect(new URL('/login', request.nextUrl))
}
if (!token && publicPaths.includes(currentPath)) {
return NextResponse.next()
}
try {
const decoded = await VerifyAuthToken(token as string) as any;
const findUser = await (await fetch(`${APP_URL}/api/users/${decoded.id || ''}`)).json()
if(!findUser.user) {
const response = NextResponse.redirect(new URL('/login', request.nextUrl));
response.cookies.delete('token');
return response;
}
if (publicPaths.includes(currentPath)) {
return NextResponse.redirect(new URL('/dashboard', request.nextUrl))
}
return NextResponse.next();
} catch (error) {
return NextResponse.redirect(new URL('/login', request.nextUrl))
}
}
export const config = {
matcher: ['/login', '/register', '/dashboard/:path*'],
}