From 1c98600a9f82b33667a1ed5b142631eba24b9506 Mon Sep 17 00:00:00 2001 From: artlu99 Date: Sun, 3 Nov 2024 11:18:30 -0500 Subject: [PATCH] better cookies extraction logic --- functions/getSassyHashes/index.ts | 21 +++++++-------------- package.json | 1 + yarn.lock | 8 ++++++++ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/functions/getSassyHashes/index.ts b/functions/getSassyHashes/index.ts index 10ed93d..8d3e27f 100644 --- a/functions/getSassyHashes/index.ts +++ b/functions/getSassyHashes/index.ts @@ -1,5 +1,6 @@ import { PrivyClient } from '@privy-io/server-auth'; import { Client, fetchExchange, gql } from '@urql/core'; +import { parse } from 'cookie'; import { Env } from '../common'; @@ -17,23 +18,15 @@ interface SassyHashRequest { castHash: string; } -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 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 cookies = request.headers.get('cookie'); - - const privyIdToken = await getPrivyIdToken(cookies); - console.log('privyIdToken:', privyIdToken); + 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: privyIdToken }); + const user = await privy.getUser({ idToken }); + console.log('privyIdToken:', idToken); console.log('user:', user); try { @@ -81,7 +74,7 @@ export const onRequestPost: PagesFunction = async (context) => { const js = (await request.json()) as SassyHashRequest; const { privyAuthToken, castHash } = js; - const fid = await getFid(privyAuthToken, context); + const fid = await getFid(context, privyAuthToken); if (!fid) return new Response(JSON.stringify({ error: 'Failed to fetch Farcaster FID' }), { status: 500 }); const sassyHashResponses = await fetchSassyHashExpensiveApi(fid, castHash, env); diff --git a/package.json b/package.json index 1733a10..669c57d 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "antd-style": "^3.6.2", "axios": "^0.24.0", "axios-mock-adapter": "^1.20.0", + "cookie": "^1.0.1", "country-list": "^2.3.0", "dayjs": "^1.11.0", "echarts": "^5.1.2", diff --git a/yarn.lock b/yarn.lock index 9713c41..b74f552 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7745,6 +7745,7 @@ __metadata: antd-style: ^3.6.2 axios: ^0.24.0 axios-mock-adapter: ^1.20.0 + cookie: ^1.0.1 country-list: ^2.3.0 dayjs: ^1.11.0 echarts: ^5.1.2 @@ -8618,6 +8619,13 @@ __metadata: languageName: node linkType: hard +"cookie@npm:^1.0.1": + version: 1.0.1 + resolution: "cookie@npm:1.0.1" + checksum: e2ff4879d816ea27dab69651c06d328b1f4214ccefa1023b6ebf85787bc23b3cad777ed51b4e54c01d161c374a05fc86e45e51ee3d9fafb48a836439c56b2458 + languageName: node + linkType: hard + "copy-anything@npm:^2.0.1": version: 2.0.6 resolution: "copy-anything@npm:2.0.6"