Spaces:
Sleeping
Sleeping
File size: 1,134 Bytes
34ed5b1 | 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 37 38 | import { auth } from "@/lib/auth"
import { NextResponse } from "next/server"
import type { NextRequest } from "next/server"
export async function middleware(request: NextRequest) {
const session = await auth()
const isAuth = !!session?.user
const isAuthPage =
request.nextUrl.pathname.startsWith("/admin/login") ||
request.nextUrl.pathname.startsWith("/admin/forgot-password") ||
request.nextUrl.pathname.startsWith("/admin/reset-password")
const isAdminPage = request.nextUrl.pathname.startsWith("/admin")
if (isAuthPage) {
if (isAuth) {
return NextResponse.redirect(new URL("/admin/dashboard", request.url))
}
return null
}
if (isAdminPage && !isAuth) {
let from = request.nextUrl.pathname
if (request.nextUrl.search) {
from += request.nextUrl.search
}
return NextResponse.redirect(
new URL(`/admin/login?from=${encodeURIComponent(from)}`, request.url)
)
}
return null
}
export const config = {
matcher: ["/admin/:path*"],
}
|