2022-10-27 17:20:02 +03:00
|
|
|
import { NextFetchEvent, NextRequest, NextResponse } from 'next/server';
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line consistent-return
|
|
|
|
|
export function middleware(req: NextRequest, ev: NextFetchEvent) {
|
|
|
|
|
const isCorrectPassword = req.cookies.get('password') === process.env.PASSWORD;
|
|
|
|
|
const url = req.nextUrl.clone();
|
2022-11-12 08:12:54 +09:00
|
|
|
const skipURL =
|
|
|
|
|
url.pathname &&
|
|
|
|
|
(url.pathname.includes('login') ||
|
|
|
|
|
url.pathname === '/api/configs/tryPassword' ||
|
|
|
|
|
(url.pathname.includes('/_next/') && !url.pathname.includes('/pages/')) ||
|
2022-10-27 17:20:02 +03:00
|
|
|
url.pathname === '/favicon.ico' ||
|
|
|
|
|
url.pathname === '/404' ||
|
2022-12-24 17:18:16 +09:00
|
|
|
url.pathname === '/migrate' ||
|
2022-11-12 08:12:54 +09:00
|
|
|
url.pathname.includes('pages/_app'));
|
|
|
|
|
if (!skipURL && !isCorrectPassword && process.env.PASSWORD) {
|
2022-10-27 17:20:02 +03:00
|
|
|
url.pathname = '/login';
|
|
|
|
|
return NextResponse.rewrite(url);
|
|
|
|
|
}
|
|
|
|
|
}
|