forked from waycaan/mazine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
30 lines (23 loc) · 843 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
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
const auth = request.cookies.get('auth')
const isAuthenticated = !!auth
const protectedPaths = ['/home', '/manage', '/likes']
if (pathname === '/') {
return NextResponse.redirect(new URL('/login', request.url))
}
if (isAuthenticated && pathname === '/login') {
return NextResponse.redirect(new URL('/home', request.url))
}
if (!isAuthenticated && protectedPaths.includes(pathname)) {
const url = new URL('/login', request.url)
url.searchParams.set('from', pathname)
return NextResponse.redirect(url)
}
return NextResponse.next()
}
export const config = {
matcher: ['/', '/login', '/home', '/manage', '/likes']
}