From 789a1a789baef7572e1e0e8bb4d561f89889f5b4 Mon Sep 17 00:00:00 2001 From: artlu99 Date: Sun, 3 Nov 2024 09:13:55 -0500 Subject: [PATCH] attempt to get id directly from token --- functions/getSassyHashes/index.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/functions/getSassyHashes/index.ts b/functions/getSassyHashes/index.ts index a188a93..10ed93d 100644 --- a/functions/getSassyHashes/index.ts +++ b/functions/getSassyHashes/index.ts @@ -17,9 +17,25 @@ interface SassyHashRequest { castHash: string; } -const getFid = async (privyAuthToken: string, env: Env): Promise => { +const getPrivyIdToken = async (cookies: string | undefined) => { + if (!cookies) return null; + const privyIdToken = cookies.match(/privy_id_token=(?.*)/)?.groups?.privyIdToken; + if (!privyIdToken) return null; + return privyIdToken; +}; + +const getFid = async (privyAuthToken: string, context): Promise => { + const { env, request } = context; const privy = new PrivyClient(env.REACT_APP_PRIVY_APP_ID, env.PRIVY_APP_SECRET); + const cookies = request.headers.get('cookie'); + + const privyIdToken = await getPrivyIdToken(cookies); + console.log('privyIdToken:', privyIdToken); + + const user = await privy.getUser({ idToken: privyIdToken }); + console.log('user:', user); + try { const verifiedClaims = await privy.verifyAuthToken(privyAuthToken); const user = await privy.getUser(verifiedClaims.userId); @@ -65,10 +81,7 @@ export const onRequestPost: PagesFunction = async (context) => { const js = (await request.json()) as SassyHashRequest; const { privyAuthToken, castHash } = js; - const cookies: string = request.headers.get('cookie'); - console.log('cookies:', cookies); - - const fid = await getFid(privyAuthToken, env); + const fid = await getFid(privyAuthToken, context); if (!fid) return new Response(JSON.stringify({ error: 'Failed to fetch Farcaster FID' }), { status: 500 }); const sassyHashResponses = await fetchSassyHashExpensiveApi(fid, castHash, env);