mirror of
https://github.com/lukevella/rallly.git
synced 2025-07-20 01:37:23 +02:00
⬆️ v3.0.0 (#704)
This commit is contained in:
parent
735056f25f
commit
c22b3abc4d
385 changed files with 19912 additions and 5250 deletions
|
@ -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|.*\\.).*)"],
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue