⬆️ v3.0.0 (#704)

This commit is contained in:
Luke Vella 2023-06-19 17:17:00 +01:00 committed by GitHub
parent 735056f25f
commit c22b3abc4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
385 changed files with 19912 additions and 5250 deletions

View file

@ -1,18 +1,34 @@
import { getSession } from "@rallly/backend/next/edge";
import languages from "@rallly/languages";
import languageParser from "accept-language-parser";
import { NextRequest, NextResponse } from "next/server";
const supportedLocales = Object.keys(languages);
const publicPaths = ["/login", "/register", "/invite", "/auth"];
export async function middleware(req: NextRequest) {
const { headers, cookies, nextUrl } = req;
const newUrl = nextUrl.clone();
const res = NextResponse.next();
const session = await getSession(req, res);
if (
process.env.AUTH_REQUIRED &&
session.user?.isGuest !== false &&
!publicPaths.some((publicPath) =>
req.nextUrl.pathname.startsWith(publicPath),
)
) {
newUrl.pathname = "/login";
return NextResponse.redirect(newUrl);
}
// Check if locale is specified in cookie
const localeCookie = cookies.get("NEXT_LOCALE");
if (localeCookie && supportedLocales.includes(localeCookie.value)) {
newUrl.pathname = `/${localeCookie.value}${newUrl.pathname}`;
const preferredLocale = localeCookie && localeCookie.value;
if (preferredLocale && supportedLocales.includes(preferredLocale)) {
newUrl.pathname = `/${preferredLocale}${newUrl.pathname}`;
return NextResponse.rewrite(newUrl);
} else {
// Check if locale is specified in header
@ -35,5 +51,5 @@ export async function middleware(req: NextRequest) {
}
export const config = {
matcher: ["/admin/:id*", "/demo", "/p/:id*", "/profile", "/new", "/login"],
matcher: ["/((?!api|_next/static|_next/image|static|.*\\.).*)"],
};