-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
32 lines (26 loc) · 990 Bytes
/
middleware.ts
File metadata and controls
32 lines (26 loc) · 990 Bytes
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
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
const path = request.nextUrl.pathname;
// Define paths that are considered public (accessible without a token)
const isPublicPath = ['/verifyemail', '/logout'].includes(path);
// Get the token from the cookies
const token = request.cookies.get('token')?.value || '';
// Redirect logic based on the path and token presence
if (isPublicPath && token) {
return NextResponse.redirect(new URL('/', request.nextUrl));
}
// If trying to access a protected path without a token, redirect to the login page
if (!isPublicPath && !token) {
return NextResponse.redirect(new URL('/login', request.nextUrl));
}
}
// It specifies the paths for which this middleware should be executed.
export const config = {
matcher: [
'/',
'/profile',
'/verifyemail',
'/deleteAccount', // Add protected route here
],
};