mirror of
https://github.com/lukevella/rallly.git
synced 2025-06-06 04:31:50 +02:00
🐛 Include locale when creating guest user
This commit is contained in:
parent
dc8de27682
commit
d1194400b2
2 changed files with 17 additions and 6 deletions
|
@ -42,7 +42,15 @@ export async function resetUser(res: NextResponse) {
|
||||||
await setCookie(res, jwt);
|
await setCookie(res, jwt);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function initGuest(req: NextRequest, res: NextResponse) {
|
export async function initGuest(
|
||||||
|
req: NextRequest,
|
||||||
|
res: NextResponse,
|
||||||
|
{
|
||||||
|
locale,
|
||||||
|
}: {
|
||||||
|
locale: string;
|
||||||
|
},
|
||||||
|
) {
|
||||||
const { name } = getCookieSettings();
|
const { name } = getCookieSettings();
|
||||||
|
|
||||||
if (req.cookies.has(name)) {
|
if (req.cookies.has(name)) {
|
||||||
|
@ -53,6 +61,7 @@ export async function initGuest(req: NextRequest, res: NextResponse) {
|
||||||
const jwt: JWT = {
|
const jwt: JWT = {
|
||||||
sub: `user-${randomid()}`,
|
sub: `user-${randomid()}`,
|
||||||
email: null,
|
email: null,
|
||||||
|
locale,
|
||||||
};
|
};
|
||||||
|
|
||||||
await setCookie(res, jwt);
|
await setCookie(res, jwt);
|
||||||
|
|
|
@ -23,23 +23,25 @@ export const middleware = withAuth(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if locale is specified in cookie
|
// Check if locale is specified in cookie
|
||||||
const preferredLocale = req.nextauth.token?.locale;
|
let locale = req.nextauth.token?.locale;
|
||||||
if (preferredLocale && supportedLocales.includes(preferredLocale)) {
|
if (locale && supportedLocales.includes(locale)) {
|
||||||
newUrl.pathname = `/${preferredLocale}${newUrl.pathname}`;
|
newUrl.pathname = `/${locale}${newUrl.pathname}`;
|
||||||
} else {
|
} else {
|
||||||
// Check if locale is specified in header
|
// Check if locale is specified in header
|
||||||
const acceptLanguageHeader = headers.get("accept-language");
|
const acceptLanguageHeader = headers.get("accept-language");
|
||||||
const localeFromHeader = acceptLanguageHeader
|
const localeFromHeader = acceptLanguageHeader
|
||||||
? languageParser.pick(supportedLocales, acceptLanguageHeader)
|
? languageParser.pick(supportedLocales, acceptLanguageHeader)
|
||||||
: null;
|
: null;
|
||||||
const locale = localeFromHeader ?? "en";
|
locale = localeFromHeader ?? "en";
|
||||||
|
|
||||||
newUrl.pathname = `/${locale}${newUrl.pathname}`;
|
newUrl.pathname = `/${locale}${newUrl.pathname}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = NextResponse.rewrite(newUrl);
|
const res = NextResponse.rewrite(newUrl);
|
||||||
|
|
||||||
await initGuest(req, res);
|
await initGuest(req, res, {
|
||||||
|
locale,
|
||||||
|
});
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue