Skip to content

Commit

Permalink
withEmbedToken
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Nov 22, 2024
1 parent a6d297e commit 2a379b3
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 22 deletions.
2 changes: 1 addition & 1 deletion apps/web/app/api/analytics/client/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getAnalytics } from "@/lib/analytics/get-analytics";
import { calculateEarnings } from "@/lib/api/sales/commission";
import { withEmbedToken } from "@/lib/referrals/auth";
import { withEmbedToken } from "@/lib/auth/embed-token";
import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
import { NextResponse } from "next/server";

Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/api/events/client/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getEvents } from "@/lib/analytics/get-events";
import { calculateEarnings } from "@/lib/api/sales/commission";
import { withEmbedToken } from "@/lib/referrals/auth";
import { withEmbedToken } from "@/lib/auth/embed-token";
import { eventsQuerySchema } from "@/lib/zod/schemas/analytics";
import { NextResponse } from "next/server";

Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/api/referrals/invite/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { invitePartner } from "@/lib/api/partners/invite-partner";
import { parseRequestBody } from "@/lib/api/utils";
import { withEmbedToken } from "@/lib/auth/embed-token";
import { requestPartnerInviteSchema } from "@/lib/dots/schemas";
import { prisma } from "@/lib/prisma";
import { withEmbedToken } from "@/lib/referrals/auth";
import { NextResponse } from "next/server";

// GET /api/referrals/invite - check if a partner is already invited to a program
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/api/referrals/link/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { withEmbedToken } from "@/lib/referrals/auth";
import { withEmbedToken } from "@/lib/auth/embed-token";
import { NextResponse } from "next/server";

// GET /api/referrals/link - get the link for the given affiliate
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/api/referrals/program/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { withEmbedToken } from "@/lib/referrals/auth";
import { withEmbedToken } from "@/lib/auth/embed-token";
import { ProgramSchema } from "@/lib/zod/schemas/programs";
import { NextResponse } from "next/server";

Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/api/referrals/sales/count/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getStartEndDates } from "@/lib/analytics/utils/get-start-end-dates";
import { withEmbedToken } from "@/lib/auth/embed-token";
import { prisma } from "@/lib/prisma";
import { withEmbedToken } from "@/lib/referrals/auth";
import { getPartnerSalesCountQuerySchema } from "@/lib/zod/schemas/partners";
import { NextResponse } from "next/server";

Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/api/referrals/sales/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { withEmbedToken } from "@/lib/auth/embed-token";
import { prisma } from "@/lib/prisma";
import { withEmbedToken } from "@/lib/referrals/auth";
import z from "@/lib/zod";
import { PartnerSaleResponseSchema } from "@/lib/zod/schemas/partners";
import { NextResponse } from "next/server";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getLinkOrThrow } from "@/lib/api/links/get-link-or-throw";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { createEmbedToken } from "@/lib/referrals/create-embed-token";
Expand All @@ -7,13 +8,14 @@ import {
} from "@/lib/zod/schemas/referrals";
import { NextResponse } from "next/server";

// GET /api/referrals/tokens - create a new referral token for the given link
// POST /api/tokens/embed - create a new embed token for the given link
export const POST = withWorkspace(async ({ workspace, req }) => {
const { linkId } = createReferralTokenSchema.parse(
await parseRequestBody(req),
);
await getLinkOrThrow({ linkId, workspaceId: workspace.id });

const token = await createEmbedToken({ linkId, workspaceId: workspace.id });
const token = await createEmbedToken(linkId);

return NextResponse.json(referralTokenSchema.parse(token), {
status: 201,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const POST = withWorkspace(async ({ workspace }) => {
});
}

const token = await fetch(`${APP_DOMAIN_WITH_NGROK}/api/referrals/tokens`, {
const token = await fetch(`${APP_DOMAIN_WITH_NGROK}/api/tokens/embed`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getSearchParams } from "@dub/utils";
import { Link, Program, Project } from "@prisma/client";
import { AxiomRequest, withAxiom } from "next-axiom";
import { cookies } from "next/headers";
import { EMBED_PUBLIC_TOKEN_COOKIE_NAME } from "./constants";
import { EMBED_PUBLIC_TOKEN_COOKIE_NAME } from "../referrals/constants";

interface WithEmbedTokenHandler {
({
Expand Down
12 changes: 1 addition & 11 deletions apps/web/lib/referrals/create-embed-token.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
import { prisma } from "@/lib/prisma";
import { waitUntil } from "@vercel/functions";
import { DubApiError } from "../api/errors";
import { getLinkOrThrow } from "../api/links/get-link-or-throw";
import { createId } from "../api/utils";
import { ratelimit } from "../upstash";
import {
EMBED_PUBLIC_TOKEN_EXPIRY,
EMBED_PUBLIC_TOKEN_LENGTH,
} from "./constants";

export const createEmbedToken = async ({
linkId,
workspaceId,
}: {
linkId: string;
workspaceId: string;
}) => {
const link = await getLinkOrThrow({ linkId, workspaceId });
console.log("link", link);

export const createEmbedToken = async (linkId: string) => {
const { success } = await ratelimit(10, "1 m").limit(linkId);

if (!success) {
Expand Down

0 comments on commit 2a379b3

Please sign in to comment.