Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/getBaseUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export function getBaseUrl() {
// 1. If running inside the browser, use relative path "/"
if (typeof window !== "undefined") {
return "";
}

// 2. If running on Vercel (deployment)
if (process.env.VERCEL_URL) {
return `https://${process.env.VERCEL_URL}`;
}

// 3. If running on Appwrite Cloud Production & you set custom domain
if (process.env.NEXT_PUBLIC_APPWRITE_URL) {
try {
const url = new URL(process.env.NEXT_PUBLIC_APPWRITE_URL);
return `${url.origin}`;
} catch (error) {
console.error("Invalid APPWRITE endpoint:", error);
}
}

// 4. Fallback for Local Development
return `http://localhost:${process.env.PORT ?? 3000}`;
}
19 changes: 9 additions & 10 deletions proxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
import { getBaseUrl } from "@/lib/getBaseUrl";

const isPublicRoute = createRouteMatcher([
"/sign-in(.*)",
Expand All @@ -9,20 +10,18 @@ const isPublicRoute = createRouteMatcher([
]);

export default clerkMiddleware(async (auth, req) => {
if (!isPublicRoute(req)) {
const baseUrl =
process.env.NODE_ENV === "production"
? "https://your-domain.com"
: "http://localhost:3000";
await auth.protect({ unauthenticatedUrl: `${baseUrl}/landing` });
}
// Allow public routes
if (isPublicRoute(req)) return;

// Redirect unauthed → /landing
await auth.protect({
unauthenticatedUrl: `${getBaseUrl()}/landing`
});
});

export const config = {
matcher: [
// Skip Next.js internals and all static files, unless found in search params
"/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)",
// Always run for API routes
"/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|png|svg|woff2?|ico|csv|zip|webmanifest)).*)",
"/(api|trpc)(.*)",
],
};