mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-10 15:35:55 +01:00
🐛 Fix middleware
This commit is contained in:
@@ -2,19 +2,27 @@ import { NextFetchEvent, NextRequest, NextResponse } from 'next/server';
|
|||||||
|
|
||||||
// eslint-disable-next-line consistent-return
|
// eslint-disable-next-line consistent-return
|
||||||
export function middleware(req: NextRequest, ev: NextFetchEvent) {
|
export function middleware(req: NextRequest, ev: NextFetchEvent) {
|
||||||
const isCorrectPassword = req.cookies.get('password')?.value === process.env.PASSWORD;
|
const { cookies } = req;
|
||||||
|
const passwordCookie = cookies.get('password')?.value;
|
||||||
|
const isCorrectPassword = passwordCookie?.toString() === process.env.PASSWORD;
|
||||||
const url = req.nextUrl.clone();
|
const url = req.nextUrl.clone();
|
||||||
const skipURL =
|
// Skip the middleware if the URL is 'login', 'api/configs/tryPassword', '_next/*', 'favicon.ico', '404', 'migrate' or 'pages/_app'
|
||||||
url.pathname &&
|
const skippedUrls = [
|
||||||
(url.pathname.includes('login') ||
|
'/login',
|
||||||
url.pathname === '/api/configs/tryPassword' ||
|
'/api/configs/tryPassword',
|
||||||
(url.pathname.includes('/_next/') && !url.pathname.includes('/pages/')) ||
|
'/_next/',
|
||||||
url.pathname === '/favicon.ico' ||
|
'/favicon.ico',
|
||||||
url.pathname === '/404' ||
|
'/404',
|
||||||
url.pathname === '/migrate' ||
|
'/migrate',
|
||||||
url.pathname.includes('pages/_app'));
|
'/pages/_app',
|
||||||
if (!skipURL && !isCorrectPassword && process.env.PASSWORD) {
|
];
|
||||||
|
if (skippedUrls.some((skippedUrl) => url.pathname.startsWith(skippedUrl))) {
|
||||||
|
return NextResponse.next();
|
||||||
|
}
|
||||||
|
// If the password is not correct, redirect to the login page
|
||||||
|
if (!isCorrectPassword && process.env.PASSWORD) {
|
||||||
url.pathname = '/login';
|
url.pathname = '/login';
|
||||||
return NextResponse.rewrite(url);
|
return NextResponse.rewrite(url);
|
||||||
}
|
}
|
||||||
|
return NextResponse.next();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user