Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

fixed /auth rewrite CORS issue by changing to /_auth

Steven Teye1fdd2860073
diff --git a/lib/middleware/link.ts b/lib/middleware/link.ts
index 3297cdb2a7..47b9c992d0 100644
--- a/lib/middleware/link.ts
+++ b/lib/middleware/link.ts
@@ -20,8 +20,6 @@ export default async function LinkMiddleware(
}>(`${domain}:${key}`);
const { url: target, password, proxy } = response || {};
- console.log(domain, key, target, password, proxy);
-
if (target) {
// special case for link health monitoring with planetfall.io :)
if (!req.headers.get("dub-no-track")) {
@@ -29,14 +27,14 @@ export default async function LinkMiddleware(
}
if (password && !req.cookies.get("dub_authenticated")) {
- console.log("rewriting onto to password page");
- return NextResponse.rewrite(`https://dub.sh/auth/${domain}/${key}`);
+ // rewrite to auth page (/_auth/[domain]/[key]) if the link is password protected and the user has not authenticated before
+ return NextResponse.rewrite(new URL(`/_auth/${domain}/${key}`, req.url));
}
const isBot = detectBot(req);
if (isBot && proxy) {
- // rewrite to proxy page (dub.sh/proxy/[domain]/[key]) if it's a bot
- return NextResponse.rewrite(`https://dub.sh/proxy/${domain}/${key}`);
+ // rewrite to proxy page (dub.sh/_proxy/[domain]/[key]) if it's a bot
+ return NextResponse.rewrite(`https://dub.sh/_proxy/${domain}/${key}`);
} else {
return NextResponse.redirect(target);
}
diff --git a/middleware.ts b/middleware.ts
index bc7580f5dc..7a4b9934c5 100644
--- a/middleware.ts
+++ b/middleware.ts
@@ -17,10 +17,11 @@ export const config = {
* Match all paths except for:
* 1. /api routes
* 2. /_next (Next.js internals)
- * 3. /static (inside /public)
- * 4. all root files inside /public (e.g. /favicon.ico)
+ * 3. /_proxy & /_auth (special pages for OG tag proxying and password protection)
+ * 4. /static (inside /public)
+ * 5. all root files inside /public (e.g. /favicon.ico)
*/
- "/((?!api|_next|static|va|[\\w-]+\\.\\w+).*)",
+ "/((?!api|_next|_proxy|_auth|static|va|[\\w-]+\\.\\w+).*)",
],
};
diff --git a/pages/auth/[domain]/[key].tsx b/pages/_auth/[domain]/[key].tsx
similarity index 100%
rename from pages/auth/[domain]/[key].tsx
rename to pages/_auth/[domain]/[key].tsx
diff --git a/pages/proxy/[domain]/[key].tsx b/pages/_proxy/[domain]/[key].tsx
similarity index 100%
rename from pages/proxy/[domain]/[key].tsx
rename to pages/_proxy/[domain]/[key].tsx
diff --git a/pages/api/projects/[slug]/domains/[domain]/links/[key]/revalidate.ts b/pages/api/projects/[slug]/domains/[domain]/links/[key]/revalidate.ts
index b73dfb548f..8e17e21f0e 100644
--- a/pages/api/projects/[slug]/domains/[domain]/links/[key]/revalidate.ts
+++ b/pages/api/projects/[slug]/domains/[domain]/links/[key]/revalidate.ts
@@ -11,13 +11,13 @@ export default async function handler(
const { domain, key } = req.query as { domain: string; key: string };
try {
- await res.revalidate(`/proxy/${domain}/${key}`);
+ await res.revalidate(`/_proxy/${domain}/${key}`);
res.status(200).json({
message: "OK",
});
} catch (error) {
res.status(500).json({
- message: `Failed to revalidate "${`/proxy/${domain}/${key}`}"`,
+ message: `Failed to revalidate "${`/_proxy/${domain}/${key}`}"`,
});
}
}