-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmiddleware.ts
32 lines (30 loc) · 901 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
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { TOKEN_KEY } from '@constants/token'
import { SIGNUP_URL, LOGIN_URL, HOME_URL } from '@constants/pageUrl'
export function middleware(req: NextRequest) {
const { pathname } = req.nextUrl
const url = req.nextUrl.clone()
if (pathname === LOGIN_URL) {
if (req.cookies.get(TOKEN_KEY)) {
url.pathname = '/'
return NextResponse.redirect(url)
}
} else if (pathname === SIGNUP_URL && req.cookies.get(TOKEN_KEY)) {
url.pathname = HOME_URL
return NextResponse.redirect(url)
} else if (pathname !== SIGNUP_URL && !req.cookies.get(TOKEN_KEY)) {
url.pathname = LOGIN_URL
return NextResponse.redirect(url)
}
}
export const config = {
matcher: [
'/create-menu',
'/edit-menu/:path*',
'/myInfo/:path*',
'/user/:path*',
'/signup',
'/login'
]
}