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.

send token via Header

Kiran K32cbfd959a07
diff --git a/apps/web/app/app.dub.co/embed/referrals/activity.tsx b/apps/web/app/app.dub.co/embed/referrals/activity.tsx
index d1740b752d..170dc09ac6 100644
--- a/apps/web/app/app.dub.co/embed/referrals/activity.tsx
+++ b/apps/web/app/app.dub.co/embed/referrals/activity.tsx
@@ -1,7 +1,6 @@
import { CursorRays } from "@/ui/layout/sidebar/icons/cursor-rays";
import { InfoTooltip, MiniAreaChart } from "@dub/ui";
-import { cn, currencyFormatter, nFormatter } from "@dub/utils";
-import { fetcher } from "@dub/utils/src/functions";
+import { cn, currencyFormatter, fetcher, nFormatter } from "@dub/utils";
import { useEmbedToken } from "app/app.dub.co/embed/use-embed-token";
import { AnalyticsTimeseries } from "dub/models/components";
import { SVGProps, useId } from "react";
@@ -22,8 +21,13 @@ export function ReferralsEmbedActivity({
const isEmpty = clicks === 0 && leads === 0 && sales === 0;
const { data: analytics } = useSWR<AnalyticsTimeseries[]>(
- !isEmpty && `/api/embed/referrals/analytics?token=${token}`,
- fetcher,
+ !isEmpty && `/api/embed/referrals/analytics`,
+ (url) =>
+ fetcher(url, {
+ headers: {
+ Authorization: `Bearer ${token}`,
+ },
+ }),
{
keepPreviousData: true,
dedupingInterval: 60000,
diff --git a/apps/web/app/app.dub.co/embed/referrals/add-edit-link.tsx b/apps/web/app/app.dub.co/embed/referrals/add-edit-link.tsx
index 6694a9696b..4d4c2d03cb 100644
--- a/apps/web/app/app.dub.co/embed/referrals/add-edit-link.tsx
+++ b/apps/web/app/app.dub.co/embed/referrals/add-edit-link.tsx
@@ -64,13 +64,14 @@ export function ReferralsEmbedCreateUpdateLink({
try {
const endpoint = !link
- ? `/api/embed/referrals/links?token=${token}`
- : `/api/embed/referrals/links/${link.id}?token=${token}`;
+ ? `/api/embed/referrals/links`
+ : `/api/embed/referrals/links/${link.id}`;
const response = await fetch(endpoint, {
method: !link ? "POST" : "PATCH",
headers: {
"Content-Type": "application/json",
+ Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
...data,
diff --git a/apps/web/app/app.dub.co/embed/referrals/earnings.tsx b/apps/web/app/app.dub.co/embed/referrals/earnings.tsx
index 6df8449cd0..96c9142a69 100644
--- a/apps/web/app/app.dub.co/embed/referrals/earnings.tsx
+++ b/apps/web/app/app.dub.co/embed/referrals/earnings.tsx
@@ -20,8 +20,13 @@ export function ReferralsEmbedEarnings({ salesCount }: { salesCount: number }) {
REFERRALS_EMBED_EARNINGS_LIMIT,
);
const { data: earnings, isLoading } = useSWR<PartnerEarningsResponse[]>(
- `/api/embed/referrals/earnings?page=${pagination.pageIndex}&token=${token}`,
- fetcher,
+ `/api/embed/referrals/earnings?page=${pagination.pageIndex}`,
+ (url) =>
+ fetcher(url, {
+ headers: {
+ Authorization: `Bearer ${token}`,
+ },
+ }),
{
keepPreviousData: true,
},
diff --git a/apps/web/app/app.dub.co/embed/referrals/leaderboard.tsx b/apps/web/app/app.dub.co/embed/referrals/leaderboard.tsx
index d45226fd14..abe0fdc663 100644
--- a/apps/web/app/app.dub.co/embed/referrals/leaderboard.tsx
+++ b/apps/web/app/app.dub.co/embed/referrals/leaderboard.tsx
@@ -17,9 +17,18 @@ export function ReferralsEmbedLeaderboard() {
const { data: partners, isLoading } = useSWR<
z.infer<typeof LeaderboardPartnerSchema>[]
- >(`/api/embed/referrals/leaderboard?token=${token}`, fetcher, {
- keepPreviousData: true,
- });
+ >(
+ `/api/embed/referrals/leaderboard`,
+ (url) =>
+ fetcher(url, {
+ headers: {
+ Authorization: `Bearer ${token}`,
+ },
+ }),
+ {
+ keepPreviousData: true,
+ },
+ );
const { table, ...tableProps } = useTable({
data: partners || [],
diff --git a/apps/web/app/app.dub.co/embed/referrals/links-list.tsx b/apps/web/app/app.dub.co/embed/referrals/links-list.tsx
index abbab8a529..23f6d90eef 100644
--- a/apps/web/app/app.dub.co/embed/referrals/links-list.tsx
+++ b/apps/web/app/app.dub.co/embed/referrals/links-list.tsx
@@ -24,8 +24,13 @@ export function ReferralsEmbedLinksList({
const [partnerLinks, setPartnerLinks] = useState<ReferralsEmbedLink[]>(links);
const { data: refreshedLinks, isLoading } = useSWR<ReferralsEmbedLink[]>(
- `/api/embed/referrals/links?token=${token}`,
- fetcher,
+ `/api/embed/referrals/links`,
+ (url) =>
+ fetcher(url, {
+ headers: {
+ Authorization: `Bearer ${token}`,
+ },
+ }),
{
keepPreviousData: true,
},
diff --git a/apps/web/app/app.dub.co/embed/referrals/token.tsx b/apps/web/app/app.dub.co/embed/referrals/token.tsx
index e450f0b634..e8b1ed85d5 100644
--- a/apps/web/app/app.dub.co/embed/referrals/token.tsx
+++ b/apps/web/app/app.dub.co/embed/referrals/token.tsx
@@ -9,8 +9,13 @@ export const ReferralsReferralsEmbedToken = () => {
const token = useEmbedToken();
const { error } = useSWR<{ token: number }>(
- `/api/embed/referrals/token?token=${token}`,
- fetcher,
+ `/api/embed/referrals/token`,
+ (url) =>
+ fetcher(url, {
+ headers: {
+ Authorization: `Bearer ${token}`,
+ },
+ }),
{
revalidateOnFocus: true,
dedupingInterval: 30000,
diff --git a/apps/web/lib/embed/referrals/auth.ts b/apps/web/lib/embed/referrals/auth.ts
index 2bee32264a..04fc5916cb 100644
--- a/apps/web/lib/embed/referrals/auth.ts
+++ b/apps/web/lib/embed/referrals/auth.ts
@@ -39,7 +39,7 @@ export const withReferralsEmbedToken = (
try {
const rateLimit = 60;
const searchParams = getSearchParams(req.url);
- const embedToken = searchParams.token;
+ const embedToken = req.headers.get("Authorization")?.split(" ")[1];
if (!embedToken) {
throw new DubApiError({
diff --git a/packages/utils/src/functions/fetcher.ts b/packages/utils/src/functions/fetcher.ts
index 68dede8716..8fd3ce65f5 100644
--- a/packages/utils/src/functions/fetcher.ts
+++ b/packages/utils/src/functions/fetcher.ts
@@ -5,9 +5,12 @@ interface SWRError extends Error {
export async function fetcher<JSON = any>(
input: RequestInfo,
- init?: RequestInit,
+ init?: RequestInit & { headers?: Record<string, string> },
): Promise<JSON> {
- const res = await fetch(input, init);
+ const res = await fetch(input, {
+ ...init,
+ ...(init?.headers && { headers: init.headers }),
+ });
if (!res.ok) {
const message =