Skip to content

Commit 15af142

Browse files
committed
feat: remove strict login requirement
1 parent bbc41dc commit 15af142

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Configuration for dashboard routes that require authentication.
3+
*
4+
* To add a new protected route, simply add the path to this array.
5+
* To remove protection from a route, remove it from this array.
6+
*
7+
* Routes are matched using prefix matching, so nested routes under
8+
* a protected path will also be protected (e.g., /dashboard/projects/123
9+
* is protected if /dashboard/projects is in this array).
10+
*/
11+
export const PROTECTED_DASHBOARD_ROUTES = [
12+
'/dashboard/projects',
13+
'/dashboard/sheet',
14+
] as const;
15+

apps/web/src/middleware.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getToken } from "next-auth/jwt";
22
import { NextRequest, NextResponse } from "next/server";
3+
import { PROTECTED_DASHBOARD_ROUTES } from "@/lib/auth/protected-routes";
34

45
export async function middleware(req: NextRequest) {
56
const adaptedReq = {
@@ -10,12 +11,18 @@ export async function middleware(req: NextRequest) {
1011
req: adaptedReq as any,
1112
secret: process.env.NEXTAUTH_SECRET,
1213
});
13-
const protectedPaths = ["/dashboard"];
14-
if (protectedPaths.some((path) => req.nextUrl.pathname.startsWith(path))) {
15-
if (!token) {
16-
const signInUrl = new URL("/login", req.url);
17-
return NextResponse.redirect(signInUrl);
18-
}
14+
15+
const pathname = req.nextUrl.pathname;
16+
17+
const isProtectedRoute = PROTECTED_DASHBOARD_ROUTES.some((path) =>
18+
pathname.startsWith(path)
19+
);
20+
21+
if (isProtectedRoute && !token) {
22+
const signInUrl = new URL("/login", req.url);
23+
signInUrl.searchParams.set("callbackUrl", pathname);
24+
return NextResponse.redirect(signInUrl);
1925
}
26+
2027
return NextResponse.next();
2128
}

0 commit comments

Comments
 (0)