Files
homarr/src/middleware.ts
Thomas Camlong 811d940f2b 🐳 Fix issues with dockerfile (#1611)
* Add `wait $PID` to be able to send SIG_ABORT

* Move to docker `entrypoint`

* Change default NEXTAUTH_URL

* Add `VOLUME` instruction

* corrected a typo

* 🐳 Fix docker TCP not working

Fixes Lost docker connection via TCP with 0.14.0 update #1577

* 🚧 Improve dockerfile and start script and fix permission issue by adding new user with permission to read / write to /data folder

* 🐛 Cleanup changes, Local db:migrate script not working, CI failed

*  Image properties customization (#1590)

* 🌐 New Crowdin updates (#1572)

*  Add notice page for readonly db

* Misc docker changes

* 🐳 Add `homarr` as `USER`

* 🐛 Unable to use user homarr because db.sqlite file is already owned by root

---------

Co-authored-by: Lumilias <10852161+Lumilias@users.noreply.github.com>
Co-authored-by: Meier Lukas <meierschlumpf@gmail.com>
Co-authored-by: Manuel <30572287+manuel-rw@users.noreply.github.com>
Co-authored-by: Manuel <manuel.ruwe@bluewin.ch>
2023-11-13 20:04:44 +01:00

52 lines
1.2 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import { getUrl } from './tools/server/url';
import { client } from './utils/api';
const skippedUrls = [
'/onboard',
'/api/',
'/_next/',
'/favicon.ico',
'/404',
'/pages/_app',
'/imgs/',
];
let cachedUserCount = 0;
export async function middleware(req: NextRequest) {
const url = req.nextUrl.clone();
// Do not redirect if the url is in the skippedUrls array
if (skippedUrls.some((skippedUrl) => url.pathname.startsWith(skippedUrl))) {
return NextResponse.next();
}
// Do not redirect if we are on Vercel
if (process.env.VERCEL) {
return NextResponse.next();
}
// Do not redirect if there are users in the database
if (cachedUserCount > 0) {
return NextResponse.next();
}
// Do not redirect if there are users in the database
if (!(await shouldRedirectToOnboard())) {
return NextResponse.next();
}
return NextResponse.redirect(getUrl(req) + '/onboard');
}
const shouldRedirectToOnboard = async (): Promise<boolean> => {
const cacheAndGetUserCount = async () => {
cachedUserCount = await client.user.count.query();
return cachedUserCount === 0;
};
return await cacheAndGetUserCount();
};