Skip to content

Commit

Permalink
better cookies extraction logic
Browse files Browse the repository at this point in the history
  • Loading branch information
artlu99 committed Nov 3, 2024
1 parent 789a1a7 commit 1c98600
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
21 changes: 7 additions & 14 deletions functions/getSassyHashes/index.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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=(?<privyIdToken>.*)/)?.groups?.privyIdToken;
if (!privyIdToken) return null;
return privyIdToken;
};

const getFid = async (privyAuthToken: string, context): Promise<number> => {
const getFid = async (context, privyAuthToken: string): Promise<number> => {
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 {
Expand Down Expand Up @@ -81,7 +74,7 @@ export const onRequestPost: PagesFunction<Env> = 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);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 1c98600

Please sign in to comment.