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 onTokenExpired

Kiran Ke94fa9293e88
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page-client.tsx
index c9000988af..b0cb43eacd 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page-client.tsx
@@ -37,7 +37,10 @@ export default function ReferralsPageClient() {
}
return publicToken ? (
- <ReferralsEmbed publicToken={publicToken} />
+ <ReferralsEmbed
+ publicToken={publicToken}
+ onTokenExpired={() => createPublicToken()}
+ />
) : (
<LoadingSpinner className="my-auto h-full w-5 text-gray-400" />
);
diff --git a/apps/web/app/app.dub.co/embed/page-client.tsx b/apps/web/app/app.dub.co/embed/page-client.tsx
index 6cbfc932e5..b0e4ef2d16 100644
--- a/apps/web/app/app.dub.co/embed/page-client.tsx
+++ b/apps/web/app/app.dub.co/embed/page-client.tsx
@@ -18,6 +18,7 @@ import {
import { Copy, MoneyBill2 } from "@dub/ui/src/icons";
import { fetcher, getPrettyUrl } from "@dub/utils";
import { ProgramEnrollment, ProgramInvite } from "@prisma/client";
+import { notFound } from "next/navigation";
import { useContext } from "react";
import useSWR from "swr";
import { RequestPartnerInviteForm } from "./request-partner-invite-form";
@@ -30,16 +31,22 @@ export function ReferralsEmbedPageClient() {
const { program } = useReferralProgram();
const { searchParamsObj } = useRouterStuff();
const [copied, copyToClipboard] = useCopyToClipboard();
- const { link, isLoading: isLoadingLink } = useReferralLink();
+ const {
+ link,
+ isLoading: isLoadingLink,
+ error: linkError,
+ } = useReferralLink();
const {
start,
end,
interval = "30d",
+ token,
} = searchParamsObj as {
start?: string;
end?: string;
interval?: IntervalOptions;
+ token?: string;
};
const { data, isLoading } = useSWR<{
@@ -51,6 +58,14 @@ export function ReferralsEmbedPageClient() {
const hasInvited = data?.programInvite;
const color = program?.brandColor || "#8B5CF6";
+ if (!token) {
+ notFound();
+ }
+
+ if (linkError && linkError.status === 401) {
+ window.parent.postMessage("TOKEN_EXPIRED", "*");
+ }
+
return (
<>
<div className="relative flex flex-col rounded-lg border border-neutral-300 bg-gradient-to-r from-neutral-50 p-4 md:p-6">
diff --git a/apps/web/lib/referrals/auth.ts b/apps/web/lib/referrals/auth.ts
index 6ee1ea4cfe..c506ed2e2d 100644
--- a/apps/web/lib/referrals/auth.ts
+++ b/apps/web/lib/referrals/auth.ts
@@ -58,6 +58,8 @@ export const withAuth = (handler: WithAuthHandler) => {
});
if (!publicToken) {
+ cookieStore.delete(EMBED_PUBLIC_TOKEN_COOKIE_NAME);
+
throw new DubApiError({
code: "unauthorized",
message: "Invalid public token.",
@@ -65,6 +67,8 @@ export const withAuth = (handler: WithAuthHandler) => {
}
if (publicToken.expires < new Date()) {
+ cookieStore.delete(EMBED_PUBLIC_TOKEN_COOKIE_NAME);
+
throw new DubApiError({
code: "unauthorized",
message: "Public token expired.",
diff --git a/apps/web/lib/referrals/create-referral-token.ts b/apps/web/lib/referrals/create-referral-token.ts
index d9ca2649ac..c3c95368b8 100644
--- a/apps/web/lib/referrals/create-referral-token.ts
+++ b/apps/web/lib/referrals/create-referral-token.ts
@@ -1,5 +1,6 @@
import { prisma } from "@/lib/prisma";
import { nanoid } from "@dub/utils";
+import { waitUntil } from "@vercel/functions";
import { DubApiError } from "../api/errors";
import { ratelimit } from "../upstash";
import {
@@ -40,6 +41,9 @@ export const createReferralPublicToken = async ({
const token = await prisma.embedPublicToken.findFirst({
where: {
linkId,
+ expires: {
+ gt: new Date(),
+ },
},
});
@@ -47,6 +51,18 @@ export const createReferralPublicToken = async ({
return token;
}
+ // Remove all expired tokens for this link
+ waitUntil(
+ prisma.embedPublicToken.deleteMany({
+ where: {
+ linkId,
+ expires: {
+ lte: new Date(),
+ },
+ },
+ }),
+ );
+
return await prisma.embedPublicToken.create({
data: {
linkId,
diff --git a/apps/web/ui/embed/referrals-embed.tsx b/apps/web/ui/embed/referrals-embed.tsx
index 718c57a0d4..e35709cd26 100644
--- a/apps/web/ui/embed/referrals-embed.tsx
+++ b/apps/web/ui/embed/referrals-embed.tsx
@@ -1,6 +1,32 @@
import { APP_DOMAIN } from "@dub/utils";
+import { useEffect } from "react";
+
+interface ReferralsEmbedProps {
+ publicToken: string;
+ onTokenExpired?: () => void;
+}
+
+export const ReferralsEmbed = ({
+ publicToken,
+ onTokenExpired,
+}: ReferralsEmbedProps) => {
+ useEffect(() => {
+ const handleMessage = (event: MessageEvent) => {
+ // Verify the message is from our embed page
+ if (event.origin !== APP_DOMAIN) {
+ return;
+ }
+
+ if (event.data === "TOKEN_EXPIRED") {
+ onTokenExpired?.();
+ }
+ };
+
+ window.addEventListener("message", handleMessage);
+
+ return () => window.removeEventListener("message", handleMessage);
+ }, [onTokenExpired]);
-export const ReferralsEmbed = ({ publicToken }: { publicToken: string }) => {
return (
<iframe
src={`${APP_DOMAIN}/embed?token=${publicToken}`}