Skip to content

Commit

Permalink
Update middleware.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
patooworld authored Jul 15, 2024
1 parent 735076b commit 0c76b0d
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { NextRequest, NextResponse } from "next/server";
import { NextRequest, Next Response } from "next/server";
import { getToken } from "next-auth/jwt";

export const config = {
matcher: [
Export const config = {
Matcher: [
/*
* Match all paths except for:
* 1. /api routes
* 2. /_next (Next.js internals)
* 1. /Api routes
* 2. /_Next (Next.js internals)
* 3. /_static (inside /public)
* 4. all root files inside /public (e.g. /favicon.ico)
* 4. All root files inside /public (e.g. /favicon.ico)
*/
"/((?!api/|_next/|_static/|_vercel|[\\w-]+\\.\\w+).*)",
],
};

export default async function middleware(req: NextRequest) {
const url = req.nextUrl;
Export default async function middleware (req: NextRequest) {
Const url = req.nextUrl;

// Get hostname of request (e.g. demo.vercel.pub, demo.localhost:3000)
let hostname = req.headers
let hostname = req. Headers
.get("host")!
.replace(".localhost:3000", `.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`);

// special case for Vercel preview deployment URLs
if (
If (
hostname.includes("---") &&
hostname.endsWith(`.${process.env.NEXT_PUBLIC_VERCEL_DEPLOYMENT_SUFFIX}`)
) {
Expand All @@ -32,22 +32,22 @@ export default async function middleware(req: NextRequest) {
}`;
}

const searchParams = req.nextUrl.searchParams.toString();
Const searchParams = req.nextUrl.searchParams.toString();
// Get the pathname of the request (e.g. /, /about, /blog/first-post)
const path = `${url.pathname}${
Const path = `${url.pathname}${
searchParams.length > 0 ? `?${searchParams}` : ""
}`;

// rewrites for app pages
if (hostname == `app.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`) {
const session = await getToken({ req });
if (!session && path !== "/login") {
Const session = await getToken({ req });
If (!Session && path!== "/Login") {
return NextResponse.redirect(new URL("/login", req.url));
} else if (session && path == "/login") {
return NextResponse.redirect(new URL("/", req.url));
return NextResponse.redirect(new URL("/", req. url));
}
return NextResponse.rewrite(
new URL(`/app${path === "/" ? "" : path}`, req.url),
New URL(`/app${path === "/" ? "" : path}`, req. url),
);
}

Expand All @@ -59,12 +59,12 @@ export default async function middleware(req: NextRequest) {
}

// rewrite root application to `/home` folder
if (
If (
hostname === "localhost:3000" ||
hostname === process.env.NEXT_PUBLIC_ROOT_DOMAIN
) {
return NextResponse.rewrite(
new URL(`/home${path === "/" ? "" : path}`, req.url),
New URL(`/home${path === "/" ? "" : path}`, req. url),
);
}

Expand Down

0 comments on commit 0c76b0d

Please sign in to comment.