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.

Add warning if apiKey is provided but no projectSlug

Steven Teye017f7320106
diff --git a/apps/web/lib/auth/index.ts b/apps/web/lib/auth/index.ts
index d009498f61..71834d1fba 100644
--- a/apps/web/lib/auth/index.ts
+++ b/apps/web/lib/auth/index.ts
@@ -91,6 +91,22 @@ export const withAuth =
) => {
const searchParams = getSearchParams(req.url);
const { linkId } = params || {};
+
+ let apiKey: string | undefined = undefined;
+
+ const authorizationHeader = req.headers.get("Authorization");
+ if (authorizationHeader) {
+ if (!authorizationHeader.includes("Bearer ")) {
+ return new Response(
+ "Misconfigured authorization header. Did you forget to add 'Bearer '? Learn more: https://dub.sh/auth ",
+ {
+ status: 400,
+ },
+ );
+ }
+ apiKey = authorizationHeader.replace("Bearer ", "");
+ }
+
const slug = params?.slug || searchParams.projectSlug;
const domain = params?.domain || searchParams.domain;
@@ -101,7 +117,8 @@ export const withAuth =
// if there's no projectSlug defined
if (!slug) {
- if (allowAnonymous) {
+ // for /api/links (POST /api/links) – allow no session (but warn if user provides apiKey)
+ if (allowAnonymous && !apiKey) {
// @ts-expect-error
return handler({
req,
@@ -119,18 +136,7 @@ export const withAuth =
}
}
- const authorizationHeader = req.headers.get("Authorization");
- if (authorizationHeader) {
- if (!authorizationHeader.includes("Bearer ")) {
- return new Response(
- "Misconfigured authorization header. Did you forget to add 'Bearer '? Learn more: https://dub.sh/auth ",
- {
- status: 400,
- },
- );
- }
- const apiKey = authorizationHeader.replace("Bearer ", "");
-
+ if (apiKey) {
const url = new URL(req.url || "", API_DOMAIN);
if (url.pathname.includes("/analytics")) {