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 TikTok verification

Tim Wilsonc13616ecc302
diff --git a/apps/web/app/api/partners/online-presence/callback/route.ts b/apps/web/app/api/partners/online-presence/callback/route.ts
index 7d74b78009..852ecbf8fa 100644
--- a/apps/web/app/api/partners/online-presence/callback/route.ts
+++ b/apps/web/app/api/partners/online-presence/callback/route.ts
@@ -46,8 +46,15 @@ export async function GET(req: Request) {
return NextResponse.redirect(redirectUrl);
}
- const { tokenUrl, clientId, clientSecret, verify, verifiedColumn, pkce } =
- ONLINE_PRESENCE_PROVIDERS[provider];
+ const {
+ tokenUrl,
+ clientId,
+ clientSecret,
+ verify,
+ verifiedColumn,
+ pkce,
+ clientIdParam,
+ } = ONLINE_PRESENCE_PROVIDERS[provider];
// Get code verifier from cookie if this is X/Twitter
const codeVerifier = pkce
@@ -69,14 +76,22 @@ export async function GET(req: Request) {
Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString("base64")}`,
},
body: new URLSearchParams({
- client_id: clientId!,
+ [clientIdParam ?? "client_id"]: clientId!,
client_secret: clientSecret!,
code,
redirect_uri: `${APP_DOMAIN_WITH_NGROK}/api/partners/online-presence/callback`,
grant_type: "authorization_code",
...(codeVerifier && { code_verifier: codeVerifier }),
}).toString(),
- }).then((r) => r.json());
+ }).then(async (r) => {
+ const text = await r.text();
+ try {
+ return JSON.parse(text);
+ } catch (e) {
+ console.error("Failed to parse access token response", text);
+ throw e;
+ }
+ });
if (!result || !result.access_token) {
console.warn("No access token found in OAuth callback");
diff --git a/apps/web/lib/actions/partners/online-presence-providers.ts b/apps/web/lib/actions/partners/online-presence-providers.ts
index e7ff38f215..698f64c20e 100644
--- a/apps/web/lib/actions/partners/online-presence-providers.ts
+++ b/apps/web/lib/actions/partners/online-presence-providers.ts
@@ -6,8 +6,9 @@ type OnlinePresenceProvider = {
tokenUrl: string;
clientId: string | null;
clientSecret: string | null;
+ clientIdParam?: string;
pkce?: boolean;
- scopes: string[];
+ scopes: string;
verify: (props: {
partner: Partner;
accessToken: string;
@@ -22,7 +23,7 @@ export const ONLINE_PRESENCE_PROVIDERS: Record<string, OnlinePresenceProvider> =
tokenUrl: "https://oauth2.googleapis.com/token",
clientId: process.env.NEXT_PUBLIC_YOUTUBE_CLIENT_ID ?? null,
clientSecret: process.env.YOUTUBE_CLIENT_SECRET ?? null,
- scopes: ["https://www.googleapis.com/auth/youtube.readonly"],
+ scopes: "https://www.googleapis.com/auth/youtube.readonly",
verify: async ({ partner, accessToken }) => {
if (!partner.youtube) return false;
@@ -46,7 +47,7 @@ export const ONLINE_PRESENCE_PROVIDERS: Record<string, OnlinePresenceProvider> =
clientId: process.env.NEXT_PUBLIC_X_CLIENT_ID ?? null,
clientSecret: process.env.X_CLIENT_SECRET ?? null,
pkce: true,
- scopes: ["users.read", "tweet.read"],
+ scopes: "users.read tweet.read",
verify: async ({ partner, accessToken }) => {
if (!partner.twitter) return false;
@@ -60,9 +61,37 @@ export const ONLINE_PRESENCE_PROVIDERS: Record<string, OnlinePresenceProvider> =
const username = userResponse?.data?.username;
return (
- !!username &&
- partner.twitter.toLowerCase() ===
- userResponse.data.username.toLowerCase()
+ !!username && partner.twitter.toLowerCase() === username.toLowerCase()
+ );
+ },
+ },
+ tiktok: {
+ verifiedColumn: "tiktokVerifiedAt",
+ authUrl: "https://www.tiktok.com/v2/auth/authorize",
+ tokenUrl: "https://open.tiktokapis.com/v2/oauth/token/",
+ clientId: process.env.NEXT_PUBLIC_TIKTOK_CLIENT_ID ?? null,
+ clientSecret: process.env.TIKTOK_CLIENT_SECRET ?? null,
+ clientIdParam: "client_key",
+ scopes: "user.info.basic,user.info.profile",
+ verify: async ({ partner, accessToken }) => {
+ if (!partner.tiktok) return false;
+
+ // Fetch user info
+ const userResponse = await fetch(
+ "https://open.tiktokapis.com/v2/user/info/?fields=username",
+ {
+ headers: {
+ Authorization: `Bearer ${accessToken}`,
+ },
+ },
+ ).then((r) => r.json());
+
+ const username = userResponse?.data?.user?.username;
+
+ console.log({ userResponse, user: userResponse?.data?.user, username });
+
+ return (
+ !!username && partner.tiktok.toLowerCase() === username.toLowerCase()
);
},
},
diff --git a/apps/web/lib/actions/partners/update-online-presence.ts b/apps/web/lib/actions/partners/update-online-presence.ts
index 1f2782409e..90d5ce284c 100644
--- a/apps/web/lib/actions/partners/update-online-presence.ts
+++ b/apps/web/lib/actions/partners/update-online-presence.ts
@@ -104,9 +104,9 @@ export const updateOnlinePresenceAction = authPartnerActionClient
if (!data.clientId) return [key, null];
const params: Record<string, string> = {
- client_id: data.clientId,
+ [data.clientIdParam ?? "client_id"]: data.clientId,
redirect_uri: `${APP_DOMAIN_WITH_NGROK}/api/partners/online-presence/callback`,
- scope: data.scopes.join(" "),
+ scope: data.scopes,
response_type: "code",
state: Buffer.from(
JSON.stringify({