diff --git a/src/auth/index.ts b/src/auth/index.ts index 03d8d8b0..4fc36734 100644 --- a/src/auth/index.ts +++ b/src/auth/index.ts @@ -1,7 +1,6 @@ import { isRouteProtected } from '@/site/routes'; import NextAuth, { User, type DefaultSession } from 'next-auth'; import Credentials from 'next-auth/providers/credentials'; -import { NextResponse } from 'next/server'; declare module 'next-auth' { interface Session { @@ -37,18 +36,12 @@ export const { ], callbacks: { authorized({ auth, request }) { - const url = new URL(request.url); - const { pathname } = url; + const { pathname } = request.nextUrl; const isUrlProtected = isRouteProtected(pathname); const isUserLoggedIn = !!auth?.user; const isRequestAuthorized = !isUrlProtected || isUserLoggedIn; - if (pathname === '/admin') { - url.pathname = '/admin/photos'; - return NextResponse.redirect(url); - } - return isRequestAuthorized; }, }, diff --git a/src/middleware.ts b/src/middleware.ts index 4596922f..b1663f29 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,4 +1,19 @@ -export { auth as middleware } from './auth'; +import { auth } from './auth'; +import { NextRequest, NextResponse } from 'next/server'; +import { NextApiRequest, NextApiResponse } from 'next'; + +export default function middleware(req: NextRequest, res:NextResponse) { + const pathname = req.nextUrl.pathname; + + if (pathname === '/admin') { + return NextResponse.redirect(new URL('/admin/photos', req.url)); + } + + return auth( + req as unknown as NextApiRequest, + res as unknown as NextApiResponse, + ); +} export const config = { matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],