-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
40 lines (35 loc) · 1007 Bytes
/
middleware.js
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
import { getToken } from 'next-auth/jwt';
import { NextResponse } from 'next/server';
export default async function middleware(req) {
// Get the pathname of the request (e.g. /, /admin)
const path = req.nextUrl.pathname;
// A list of all protected pages
const protectedPaths = [
'/home',
'/ask-pi',
'/profile',
'/search',
'/trip-packages',
'/create-schedule',
'/maps-direction',
'/add-location',
'/add-package',
'/location-map',
'/location-detail',
'/update-profile',
];
// If it's the root path, just render it
if (path === '/') {
return NextResponse.next();
}
const session = await getToken({
req,
secret: process.env.NEXTAUTH_SECRET,
});
if (!session && protectedPaths.includes(path)) {
return NextResponse.redirect(new URL('/login', req.url));
} else if (session && (path === '/login' || path === '/register')) {
return NextResponse.redirect(new URL('/home', req.url));
}
return NextResponse.next();
}