committing summative
This commit is contained in:
1 parent
73ac8e85be
commit
95b138f29e
44 files changed
+5319
-755
No files matched your search
@@ -0,0 +1,42 @@
|
||||
// export { auth as proxy } from "@/auth";
|
||||
import { auth } from "@/auth";
|
||||
import { cookies } from "next/headers"
|
||||
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export const proxy = auth(async (req) => { // req is incoming request, auth from nextauth wraps the function
|
||||
const isLoggedIn = !!req.auth // boolean based on if session info is present
|
||||
const currentPath = req.nextUrl.pathname;
|
||||
|
||||
const isAuthRoute = currentPath === "/login" || currentPath === "/register"//
|
||||
const isPublicRoute = isAuthRoute || currentPath === "/" || currentPath === "/newsletters" || currentPath.startsWith("/newsletters/") || currentPath ==="/users"; // checks individual newsletters
|
||||
|
||||
// is a public route if its an authroute, newsletter route, or root(homepage) route
|
||||
|
||||
const isAdminRoute = currentPath === "/users"
|
||||
|
||||
if (process.env.ENFORCEADMIN === "true" && isAdminRoute) {
|
||||
return NextResponse.redirect(new URL("/login", req.nextUrl.origin))
|
||||
}
|
||||
|
||||
|
||||
if (!isLoggedIn && !isPublicRoute) { // if not logged in and not on public route
|
||||
const newUrl = new URL("/login", req.nextUrl.origin);
|
||||
return NextResponse.redirect(newUrl);
|
||||
}
|
||||
|
||||
if (isLoggedIn && isAuthRoute) { // if logged in and trying to log in
|
||||
const newUrl = new URL("/", req.nextUrl.origin);
|
||||
return NextResponse.redirect(newUrl);
|
||||
}
|
||||
|
||||
return NextResponse.next();
|
||||
|
||||
}
|
||||
)
|
||||
|
||||
export const config = {
|
||||
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
|
||||
};
|
||||
|
||||
// runs proxy function on paths in matcher
|
||||
Reference in new issue
Block a user