-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
36 lines (27 loc) · 1.02 KB
/
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
36
import { NextResponse } from "next/server"
import authConfig from "./auth.config"
import NextAuth from "next-auth"
const { auth } = NextAuth(authConfig)
const unauthorizedRoutes = ['/login', '/register']
const publicRoutes = ['/', '/categories']
export default auth((req) => {
const isAuthorized = !!req.auth
console.log("ROUTE:", req.nextUrl.pathname)
if (isAuthorized && unauthorizedRoutes.includes(req.nextUrl.pathname)) {
return NextResponse.redirect(new URL("/", req.nextUrl))
}
if (!isAuthorized && publicRoutes.includes(req.nextUrl.pathname)) {
return NextResponse.next()
}
if (!isAuthorized && !unauthorizedRoutes.includes(req.nextUrl.pathname)) {
return NextResponse.redirect(new URL("/login", req.nextUrl))
}
if (req.nextUrl.pathname.startsWith("/api/auth")) {
return NextResponse.next()
}
return NextResponse.next()
})
// Routes Middleware should not run on
export const config = {
matcher: ['/((?!api|_next/static|_next/image|.*\\.png$).*)'],
}