forked from WebDeveloper0315/inventory-nextjs-antd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware.ts
31 lines (24 loc) · 948 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
import { NextRequest, NextResponse } from "next/server";
export async function middleware(request:NextRequest) {
try {
const isPublicPage = request.nextUrl.pathname === '/login'
// if there is no token and the page is not public , redirect to login
const token = request.cookies.get('token')?.value
// console.log(token, "isPublicPage")
if(!token && !isPublicPage){
return NextResponse.redirect(new URL('/login', request.nextUrl))
}
// if there is token and the page is public, redirect to home page
if(token && isPublicPage){
return NextResponse.redirect(new URL('/', request.nextUrl))
}
return NextResponse.next()
} catch (error) {
return NextResponse.error()
}
// console.log('middleware', request.nextUrl.pathname)
// return NextResponse.next()
}
export const config = {
matcher: ['/', '/login']
}