Skip to content

Commit 796c94a

Browse files
authored
Merge pull request #38 from willwearing/fix/middleware-trycatch
fix: middleware crash (MIDDLEWARE_INVOCATION_FAILED)
2 parents 197833a + 785e416 commit 796c94a

1 file changed

Lines changed: 49 additions & 39 deletions

File tree

apps/web/src/middleware.ts

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,33 @@ function isPublicRoute(pathname: string): boolean {
1111
}
1212

1313
export async function middleware(request: NextRequest) {
14-
const hostname = request.headers.get("host") || "localhost";
15-
const cookieHeader = request.headers.get("cookie");
14+
try {
15+
const hostname = request.headers.get("host") || "localhost";
16+
const cookieHeader = request.headers.get("cookie");
1617

17-
// 1. Resolve brand (passes cookies for dev-brand-override support)
18-
const brand = await resolveBrand(hostname, cookieHeader);
18+
// 1. Resolve brand (passes cookies for dev-brand-override support)
19+
const brand = await resolveBrand(hostname, cookieHeader);
1920

20-
// 2. Create response with brand headers
21-
let response = NextResponse.next({ request });
22-
response.headers.set("x-brand-id", brand.id);
23-
response.cookies.set("brand-id", brand.id, {
24-
httpOnly: true,
25-
secure: process.env.NODE_ENV === "production",
26-
sameSite: "lax",
27-
path: "/",
28-
});
21+
// 2. Create response with brand headers
22+
let response = NextResponse.next({ request });
23+
response.headers.set("x-brand-id", brand.id);
24+
response.cookies.set("brand-id", brand.id, {
25+
httpOnly: true,
26+
secure: process.env.NODE_ENV === "production",
27+
sameSite: "lax",
28+
path: "/",
29+
});
2930

30-
// 3. Supabase auth session refresh
31-
const supabase = createServerClient(
32-
process.env.NEXT_PUBLIC_SUPABASE_URL!,
33-
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
34-
{
31+
// 3. Supabase auth session refresh
32+
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
33+
const supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
34+
35+
if (!supabaseUrl || !supabaseKey) {
36+
// Supabase not configured — skip auth, serve public
37+
return response;
38+
}
39+
40+
const supabase = createServerClient(supabaseUrl, supabaseKey, {
3541
cookies: {
3642
getAll() {
3743
return request.cookies.getAll();
@@ -53,32 +59,36 @@ export async function middleware(request: NextRequest) {
5359
);
5460
},
5561
},
56-
}
57-
);
62+
});
5863

59-
const {
60-
data: { user },
61-
} = await supabase.auth.getUser();
64+
const {
65+
data: { user },
66+
} = await supabase.auth.getUser();
6267

63-
const pathname = request.nextUrl.pathname;
68+
const pathname = request.nextUrl.pathname;
6469

65-
// 4. Redirect unauthenticated users away from protected routes
66-
if (!user && !isPublicRoute(pathname)) {
67-
const url = request.nextUrl.clone();
68-
url.pathname = "/sign-in";
69-
url.searchParams.set("redirect", pathname);
70-
return NextResponse.redirect(url);
71-
}
70+
// 4. Redirect unauthenticated users away from protected routes
71+
if (!user && !isPublicRoute(pathname)) {
72+
const url = request.nextUrl.clone();
73+
url.pathname = "/sign-in";
74+
url.searchParams.set("redirect", pathname);
75+
return NextResponse.redirect(url);
76+
}
7277

73-
// 5. Redirect authenticated users from landing and auth pages to dashboard
74-
const AUTH_PAGES = ["/", "/sign-in", "/sign-up"];
75-
if (user && AUTH_PAGES.includes(pathname)) {
76-
const url = request.nextUrl.clone();
77-
url.pathname = "/dashboard";
78-
return NextResponse.redirect(url);
79-
}
78+
// 5. Redirect authenticated users from landing and auth pages to dashboard
79+
const AUTH_PAGES = ["/", "/sign-in", "/sign-up"];
80+
if (user && AUTH_PAGES.includes(pathname)) {
81+
const url = request.nextUrl.clone();
82+
url.pathname = "/dashboard";
83+
return NextResponse.redirect(url);
84+
}
8085

81-
return response;
86+
return response;
87+
} catch (error) {
88+
console.error("[middleware] Error:", error);
89+
// Don't crash — serve the page without auth/brand
90+
return NextResponse.next();
91+
}
8292
}
8393

8494
export const config = {

0 commit comments

Comments
 (0)