forked from urdadx/librelinks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
33 lines (28 loc) · 875 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
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 = [
'/admin',
'/admin/customize',
'/admin/analytics',
'/admin/settings',
'/onboarding',
];
// 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('/admin', req.url));
}
return NextResponse.next();
}