Add setup screen (#1682)

This commit is contained in:
Luke Vella 2025-04-23 13:10:05 +01:00 committed by GitHub
parent 42d0077045
commit 843dfc4a7d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 526 additions and 93 deletions

View file

@ -10,30 +10,23 @@ const supportedLocales = Object.keys(languages);
export const middleware = withAuth(async (req) => {
const { nextUrl } = req;
const newUrl = nextUrl.clone();
const pathname = newUrl.pathname;
const isLoggedIn = req.auth?.user?.email;
// if the user is already logged in, don't let them access the login page
if (
/^\/(login)/.test(newUrl.pathname) &&
isLoggedIn &&
!newUrl.searchParams.get("invalidSession")
) {
if (/^\/(login)/.test(pathname) && isLoggedIn) {
newUrl.pathname = "/";
return NextResponse.redirect(newUrl);
}
// Check if locale is specified in cookie
let locale = req.auth?.user?.locale;
if (locale && supportedLocales.includes(locale)) {
newUrl.pathname = `/${locale}${newUrl.pathname}`;
} else {
// Check if locale is specified in header
locale = getPreferredLocale(req);
newUrl.pathname = `/${locale}${newUrl.pathname}`;
const locale = req.auth?.user?.locale || getPreferredLocale(req);
if (supportedLocales.includes(locale)) {
newUrl.pathname = `/${locale}${pathname}`;
}
const res = NextResponse.rewrite(newUrl);
res.headers.set("x-pathname", newUrl.pathname);
res.headers.set("x-locale", locale);
res.headers.set("x-pathname", pathname);
if (req.auth?.user?.id) {
await withPostHog(res, { distinctID: req.auth.user.id });