🐛 Fix session empty session when logging out (#1345)

This commit is contained in:
Luke Vella 2024-09-13 17:39:25 +01:00 committed by GitHub
parent b75dfed611
commit 8f34f755b9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 36 additions and 36 deletions

View file

@ -1,16 +1,15 @@
import languages from "@rallly/languages";
import languageParser from "accept-language-parser";
import { NextResponse } from "next/server";
import withAuth from "next-auth/middleware";
import { initGuest } from "@/app/guest";
import { getLocaleFromHeader, initGuest } from "@/app/guest";
import { isSelfHosted } from "@/utils/constants";
const supportedLocales = Object.keys(languages);
export const middleware = withAuth(
async function middleware(req) {
const { headers, nextUrl } = req;
const { nextUrl } = req;
const newUrl = nextUrl.clone();
// if the user is already logged in, don't let them access the login page
@ -28,20 +27,14 @@ export const middleware = withAuth(
newUrl.pathname = `/${locale}${newUrl.pathname}`;
} else {
// Check if locale is specified in header
const acceptLanguageHeader = headers.get("accept-language");
const localeFromHeader = acceptLanguageHeader
? languageParser.pick(supportedLocales, acceptLanguageHeader)
: null;
locale = localeFromHeader ?? "en";
locale = await getLocaleFromHeader(req);
newUrl.pathname = `/${locale}${newUrl.pathname}`;
}
const res = NextResponse.rewrite(newUrl);
await initGuest(req, res, {
locale,
});
await initGuest(req, res);
return res;
},