Skip to content

Commit

Permalink
Merge pull request #142 from ArhanAnsari/revert-141-ArhanAnsari-patch-1
Browse files Browse the repository at this point in the history
Revert "Update middleware.ts"
  • Loading branch information
ArhanAnsari authored Nov 16, 2024
2 parents 5704350 + f0b9ff1 commit 192699b
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,26 @@
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";

const publicRoutes = ["/auth/signin", "/auth/signup", "/api/public-route", "/"];
const publicRoutes = ["/auth/signin", "/auth/signup", "/api/public-route"];

export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
const token = request.cookies.get("next-auth.session-token")?.value;

// Allow public routes and assets to pass through
if (publicRoutes.includes(pathname) || pathname.startsWith("/_next/")) {
return NextResponse.next();
}

// Redirect authenticated users to /dashboard when they access /
if (pathname === "/" && token) {
return NextResponse.redirect(new URL("/dashboard", request.url));
}

// Redirect unauthenticated users to /auth/signin when accessing protected routes
if (!token) {
const signInUrl = new URL("/auth/signin", request.url);
signInUrl.searchParams.set("redirect", pathname);
return NextResponse.redirect(signInUrl);
}

if (pathname === "/") {
return NextResponse.redirect(new URL("/dashboard", request.url));
}

return NextResponse.next();
}

Expand Down

0 comments on commit 192699b

Please sign in to comment.