From 5a246a96278681b07e873c7c440764075fd93576 Mon Sep 17 00:00:00 2001 From: Sosokker Date: Wed, 12 Mar 2025 15:18:48 +0700 Subject: [PATCH] feat: add middlware to check auth route --- frontend/middleware.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 frontend/middleware.ts diff --git a/frontend/middleware.ts b/frontend/middleware.ts new file mode 100644 index 0000000..5194d9b --- /dev/null +++ b/frontend/middleware.ts @@ -0,0 +1,22 @@ +import { NextResponse } from "next/server"; +import type { NextRequest } from "next/server"; + +export function middleware(request: NextRequest) { + const token = request.cookies.get("token"); + if (!token) { + const url = request.nextUrl.clone(); + url.pathname = "/auth/signin"; + return NextResponse.redirect(url); + } + return NextResponse.next(); +} + +export const config = { + matcher: [ + // This will match all paths that: + // - have at least one character after "/" + // - do NOT start with /_next/static, /_next/image, /favicon.ico, /hub, or /auth. + // (thus "/auth/signin", "/" and any "/hub" route are not processed by this middleware) + "/((?!_next/static|_next/image|favicon.ico|hub|auth).+)", + ], +};