forked from rohansen856/gofundme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
30 lines (23 loc) · 860 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
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { verify } from 'jsonwebtoken'
export function middleware(request: NextRequest) {
const token = request.cookies.get('token')?.value
// Check if the route should be protected
const protectedPaths = ['/dashboard', '/campaign', '/api/fundraisers']
const isProtectedPath = protectedPaths.some(path => request.nextUrl.pathname.startsWith(path))
if (isProtectedPath) {
if (!token) {
return NextResponse.redirect(new URL('/login', request.url))
}
try {
verify(token, process.env.JWT_SECRET!)
} catch (error) {
return NextResponse.redirect(new URL('/login', request.url))
}
}
return NextResponse.next()
}
export const config = {
matcher: ['/dashboard/:path*', '/campaign/:path*', '/api/fundraisers/:path*'],
}