forked from bips241/nerdshive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
35 lines (22 loc) · 892 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
30
31
32
33
34
35
import { NextRequest, NextResponse } from 'next/server';
export const config = {
matcher: ['/dashboard/:path*', '/login', '/register', '/', '/verify/:path*','/((?!api|_next/static|_next/image|favicon.ico).*)'],
};
export default async function middleware(request: NextRequest) {
const currentUser = await request.cookies.get('__Secure-authjs.session-token')?.value;
const url = request.nextUrl;
//(`currentUser`, currentUser);
if (
currentUser &&
(url.pathname.startsWith('/login') ||
url.pathname.startsWith('/register') ||
url.pathname.startsWith('/verify') ||
url.pathname === '/')
) {
return NextResponse.redirect(new URL('/dashboard', request.url));
}
if (!currentUser && url.pathname.startsWith('/dashboard')) {
return NextResponse.redirect(new URL('/login', request.url));
}
return NextResponse.next();
}