diff --git a/functions/getSassyHashes/index.ts b/functions/getSassyHashes/index.ts index 8d3e27f..4212431 100644 --- a/functions/getSassyHashes/index.ts +++ b/functions/getSassyHashes/index.ts @@ -14,33 +14,9 @@ interface SassyHashGraphQLResponse { getTextByCastHash: SassyHash; } interface SassyHashRequest { - privyAuthToken: string; castHash: string; } -const getFid = async (context, privyAuthToken: string): Promise => { - const { env, request } = context; - const privy = new PrivyClient(env.REACT_APP_PRIVY_APP_ID, env.PRIVY_APP_SECRET); - - const cookie = parse(request.headers.get('Cookie') || ''); - const idToken = cookie['privy-id-token'] != null ? cookie['privy-id-token'] : undefined; - - const user = await privy.getUser({ idToken }); - console.log('privyIdToken:', idToken); - console.log('user:', user); - - try { - const verifiedClaims = await privy.verifyAuthToken(privyAuthToken); - const user = await privy.getUser(verifiedClaims.userId); - - // const user2 = await privy.getUser({ idToken: IDTokenFromCookies(request) }); - return user?.farcaster?.fid; - } catch (error) { - console.error(`Token verification failed with error ${error}.`); - throw new Error('Failed to fetch Farcaster FID'); - } -}; - const fetchSassyHashExpensiveApi = async (viewerFid: number, castHash: string, env: Env) => { const client = new Client({ url: env.SASSYHASH_API, @@ -72,11 +48,25 @@ const fetchSassyHashExpensiveApi = async (viewerFid: number, castHash: string, e export const onRequestPost: PagesFunction = async (context) => { const { env, request } = context; const js = (await request.json()) as SassyHashRequest; - const { privyAuthToken, castHash } = js; + const { castHash } = js; + + try { + const privy = new PrivyClient(env.REACT_APP_PRIVY_APP_ID, env.PRIVY_APP_SECRET); - const fid = await getFid(context, privyAuthToken); - if (!fid) return new Response(JSON.stringify({ error: 'Failed to fetch Farcaster FID' }), { status: 500 }); + const cookie = parse(request.headers.get('Cookie') || ''); + const idToken = cookie['privy-id-token'] != null ? cookie['privy-id-token'] : undefined; - const sassyHashResponses = await fetchSassyHashExpensiveApi(fid, castHash, env); - return new Response(JSON.stringify(sassyHashResponses)); + const user = await privy.getUser({ idToken }); + + const fid = user?.farcaster?.fid; + if (fid) { + const sassyHashResponses = await fetchSassyHashExpensiveApi(fid, castHash, env); + return new Response(JSON.stringify(sassyHashResponses)); + } else { + return new Response(JSON.stringify({ error: 'Failed to fetch Farcaster FID' }), { status: 500 }); + } + } catch (error) { + console.error(`Token verification failed with error ${error}.`); + return new Response(JSON.stringify({ error: 'Unauthorized' }), { status: 401 }); + } }; diff --git a/src/api/channelFeed.api.ts b/src/api/channelFeed.api.ts index 58897be..e211c44 100644 --- a/src/api/channelFeed.api.ts +++ b/src/api/channelFeed.api.ts @@ -20,23 +20,22 @@ export interface PagedCronFeed { } interface ChannelFeedRequest { - getAccessToken: () => Promise; + authenticated: boolean; channel?: ChannelObject; pageToken?: string; following: number[]; } export const getEnhancedChannelFeed = async (channelFeedRequestPayload: ChannelFeedRequest): Promise => { - const { channel, getAccessToken, pageToken, following } = channelFeedRequestPayload; + const { channel, authenticated, pageToken, following } = channelFeedRequestPayload; if (!channel) return { casts: [] }; - const privyAuthToken = await getAccessToken(); const cronFeed = await getCronFeed({ channelId: channel.id, pageSize: CHANNEL_FEED_PAGESIZE, pageToken }); const seenFids = sift(cronFeed.casts.map((cast) => cast.author.fid).filter((fid) => fid !== null)); const seenSassyHashes = unique(sift(cronFeed.casts.map((cast) => (isSassy(cast.text) ? cast.hash : null)))); const botOrNotResponse = await getBotOrNot({ fids: seenFids ?? [] }); - const sassyHashResponses = privyAuthToken - ? await Promise.all(seenSassyHashes.map((sh) => getSassyHash({ privyAuthToken, castHash: sh }))) + const sassyHashResponses = authenticated + ? await Promise.all(seenSassyHashes.map((sh) => getSassyHash({ castHash: sh }))) : []; return { diff --git a/src/api/followingFeed.api.ts b/src/api/followingFeed.api.ts index 1f42161..5af5822 100644 --- a/src/api/followingFeed.api.ts +++ b/src/api/followingFeed.api.ts @@ -39,22 +39,21 @@ export const getFollowingFeed = (followingFeedRequestPayload: FollowingFeedReque interface EnhancedFollowingFeedRequest { fid: number; - getAccessToken: () => Promise; + authenticated: boolean; pageToken?: string; allChannels: ChannelObject[]; } export const getEnhancedFollowingFeed = async ( homeFeedRequestPayload: EnhancedFollowingFeedRequest, ): Promise => { - const { fid, getAccessToken, pageToken, allChannels } = homeFeedRequestPayload; + const { fid, authenticated, pageToken, allChannels } = homeFeedRequestPayload; - const privyAuthToken = await getAccessToken(); const cronFeed = await getFollowingFeed({ fid: fid, pageSize: FOLLOWING_FEED_PAGESIZE, pageToken }); const seenFids = sift(cronFeed.casts.map((cast) => cast.author.fid).filter((fid) => fid !== null)); const seenSassyHashes = unique(sift(cronFeed.casts.map((cast) => (isSassy(cast.text) ? cast.hash : null)))); const botOrNotResponse = await getBotOrNot({ fids: seenFids ?? [] }); - const sassyHashResponses = privyAuthToken - ? await Promise.all(seenSassyHashes.map((sh) => getSassyHash({ privyAuthToken, castHash: sh }))) + const sassyHashResponses = authenticated + ? await Promise.all(seenSassyHashes.map((sh) => getSassyHash({ castHash: sh }))) : []; return { diff --git a/src/api/forYouFeed.api.ts b/src/api/forYouFeed.api.ts index 8235466..ca1f257 100644 --- a/src/api/forYouFeed.api.ts +++ b/src/api/forYouFeed.api.ts @@ -20,7 +20,7 @@ export const getNeynarOpenrankForYouFeed = (forYouFeedRequestPayload: ForYouFeed interface EnhancedForYouFeedRequest { fid: number; - getAccessToken: () => Promise; + authenticated: boolean; cursor?: string; following: number[]; allChannels: ChannelObject[]; @@ -28,15 +28,14 @@ interface EnhancedForYouFeedRequest { export const getEnhancedForYouFeed = async ( homeFeedRequestPayload: EnhancedForYouFeedRequest, ): Promise => { - const { fid, getAccessToken, cursor, following, allChannels } = homeFeedRequestPayload; + const { fid, authenticated, cursor, following, allChannels } = homeFeedRequestPayload; - const privyAuthToken = await getAccessToken(); const forYouFeed = await getNeynarOpenrankForYouFeed({ fid: fid, limit: FORYOU_FEED_PAGESIZE, cursor }); const seenFids = sift(forYouFeed.casts.map((cast) => cast.author.fid).filter((fid) => fid !== null)); const seenSassyHashes = unique(sift(forYouFeed.casts.map((cast) => (isSassy(cast.text) ? cast.hash : null)))); const botOrNotResponse = await getBotOrNot({ fids: seenFids ?? [] }); - const sassyHashResponses = privyAuthToken - ? await Promise.all(seenSassyHashes.map((sh) => getSassyHash({ privyAuthToken, castHash: sh }))) + const sassyHashResponses = authenticated + ? await Promise.all(seenSassyHashes.map((sh) => getSassyHash({ castHash: sh }))) : []; return { diff --git a/src/api/sassyHash.api.ts b/src/api/sassyHash.api.ts index d7b9eb5..7eb85ff 100644 --- a/src/api/sassyHash.api.ts +++ b/src/api/sassyHash.api.ts @@ -11,7 +11,6 @@ export interface SassyHashResponse { data: SassyHash; } interface SassyHashRequest { - privyAuthToken: string; castHash: string; } diff --git a/src/components/apps/channelFeed/ChannelFeed.tsx b/src/components/apps/channelFeed/ChannelFeed.tsx index be63149..629abb1 100644 --- a/src/components/apps/channelFeed/ChannelFeed.tsx +++ b/src/components/apps/channelFeed/ChannelFeed.tsx @@ -59,12 +59,12 @@ export const ChannelFeed: React.FC = () => { return (ffQuery.data?.result?.users ?? [])?.map((u) => Number(u.fid)); }, [ffQuery.isLoading, ffQuery.error, ffQuery.data]); - const { getAccessToken } = usePrivy(); + const { authenticated } = usePrivy(); useEffect(() => { setChannelModerators(unique(sift([activeChannel?.leadFid, activeChannel?.moderatorFid]))); getEnhancedChannelFeed({ - getAccessToken, + authenticated, channel: activeChannel, following: memodFfData ?? [], }) @@ -76,7 +76,7 @@ export const ChannelFeed: React.FC = () => { .finally(() => { setLoaded(true); }); - }, [getAccessToken, memodChData, memodFfData, activeChannel]); + }, [authenticated, memodChData, memodFfData, activeChannel]); useEffect(() => { setNumCasts(casts.length); @@ -97,7 +97,7 @@ export const ChannelFeed: React.FC = () => { const next = () => getEnhancedChannelFeed({ - getAccessToken, + authenticated, channel: activeChannel, pageToken: nextPageToken, following: memodFfData ?? [], diff --git a/src/components/apps/followingFeed/FollowingFeed.tsx b/src/components/apps/followingFeed/FollowingFeed.tsx index cb3c3d3..135699c 100644 --- a/src/components/apps/followingFeed/FollowingFeed.tsx +++ b/src/components/apps/followingFeed/FollowingFeed.tsx @@ -45,14 +45,14 @@ export const FollowingFeed: React.FC = ({ fid }) => { return (ffQuery.data?.result?.users ?? []).map((u) => Number(u.fid)); }, [ffQuery.isLoading, ffQuery.error, ffQuery.data]); - const { getAccessToken } = usePrivy(); + const { authenticated } = usePrivy(); useEffect(() => { setCasts([]); getEnhancedFollowingFeed({ fid: fid, - getAccessToken, + authenticated, allChannels: memodChannelData ?? [], }) .then((res) => { @@ -63,7 +63,7 @@ export const FollowingFeed: React.FC = ({ fid }) => { .finally(() => { setLoaded(true); }); - }, [getAccessToken, fid, memodChannelData, memodFfData]); + }, [authenticated, fid, memodChannelData, memodFfData]); useEffect(() => { setNumCasts(casts.length); @@ -75,7 +75,7 @@ export const FollowingFeed: React.FC = ({ fid }) => { const next = () => getEnhancedFollowingFeed({ fid, - getAccessToken, + authenticated, pageToken: nextPageToken, allChannels: memodChannelData ?? [], }).then((newCasts) => { diff --git a/src/components/apps/forYouFeed/ForYouFeed.tsx b/src/components/apps/forYouFeed/ForYouFeed.tsx index f82f647..269dc56 100644 --- a/src/components/apps/forYouFeed/ForYouFeed.tsx +++ b/src/components/apps/forYouFeed/ForYouFeed.tsx @@ -47,14 +47,14 @@ export const ForYouFeed: React.FC = ({ fid }) => { return (ffQuery.data?.result?.users ?? []).map((u) => Number(u.fid)); }, [ffQuery.isLoading, ffQuery.error, ffQuery.data]); - const { getAccessToken } = usePrivy(); + const { authenticated } = usePrivy(); useEffect(() => { setCasts([]); getEnhancedForYouFeed({ fid: fid, - getAccessToken, + authenticated, following: memodFfData ?? [], allChannels: memodChannelData ?? [], }) @@ -66,7 +66,7 @@ export const ForYouFeed: React.FC = ({ fid }) => { .finally(() => { setLoaded(true); }); - }, [fid, getAccessToken, memodChannelData, memodFfData]); + }, [fid, authenticated, memodChannelData, memodFfData]); useEffect(() => { setNumCasts(casts.length); @@ -79,7 +79,7 @@ export const ForYouFeed: React.FC = ({ fid }) => { const next = () => getEnhancedForYouFeed({ fid: fid, - getAccessToken, + authenticated, cursor: nextCursor, following: memodFfData ?? [], allChannels: memodChannelData ?? [],