-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
25 lines (21 loc) · 898 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
import { NextRequest, NextResponse } from 'next/server';
import { getToken } from 'next-auth/jwt';
export { default } from 'next-auth/middleware';
// middleware will run only on these matches
export const config = {
matcher: ['/category/:path*', '/login', '/signup', '/', '/verify/:path*'],
};
export async function middleware(request: NextRequest) {
const token = await getToken({ req: request });
const url = request.nextUrl;
// if token is present and user is on auth pages then redirect to dahsboard
if (
token && (url.pathname === '/login') ){
return NextResponse.redirect(new URL('/', request.url));
}
// if token is not present and user is on dashboard page then redirect to login page
if (!token && (url.pathname === "/" || url.pathname.includes("category"))) {
return NextResponse.redirect(new URL('/login', request.url));
}
return NextResponse.next();
}