diff --git a/lib/components/farcaster-data-handler.react.js b/lib/components/farcaster-data-handler.react.js --- a/lib/components/farcaster-data-handler.react.js +++ b/lib/components/farcaster-data-handler.react.js @@ -15,7 +15,7 @@ import { relationshipActions } from '../types/relationship-types.js'; import { syncedMetadataNames } from '../types/synced-metadata-types.js'; import { authoritativeKeyserverID } from '../utils/authoritative-keyserver.js'; -import { useCurrentUserFID } from '../utils/farcaster-utils.js'; +import { useCurrentUserFID, useUnlinkFID } from '../utils/farcaster-utils.js'; import { useDispatchActionPromise } from '../utils/redux-promise-utils.js'; import { useSelector, useDispatch } from '../utils/redux-utils.js'; import { usingCommServicesAccessToken } from '../utils/services-utils.js'; @@ -54,6 +54,8 @@ const fid = useCurrentUserFID(); + const unlinkFID = useUnlinkFID(); + const prevCanQueryRef = React.useRef(); const handleFarcasterMutuals = React.useCallback(async () => { @@ -136,17 +138,32 @@ }); }, [loggedIn, isActive, findUserIdentities, userInfos, dispatch]); + const shouldCheckCurrentFIDRef = React.useRef(true); + const handleCurrentUserFID = React.useCallback(async () => { if ( !loggedIn || !isActive || !findUserIdentities || !currentUserID || - fid + !neynarClient ) { return; } + if (fid && fid !== 'NONE' && shouldCheckCurrentFIDRef.current) { + const isCurrentUserFIDValid = + await neynarClient.checkIfCurrentUserFIDIsValid(fid); + + if (isCurrentUserFIDValid) { + shouldCheckCurrentFIDRef.current = false; + return; + } + + await unlinkFID(); + return; + } + const currentUserIdentityObj = await findUserIdentities([currentUserID]); const identityFID = currentUserIdentityObj[currentUserID].farcasterID; @@ -159,7 +176,16 @@ }, }); } - }, [loggedIn, isActive, findUserIdentities, currentUserID, fid, dispatch]); + }, [ + loggedIn, + isActive, + findUserIdentities, + currentUserID, + neynarClient, + fid, + unlinkFID, + dispatch, + ]); React.useEffect(() => { if (!usingCommServicesAccessToken) { diff --git a/lib/utils/neynar-client.js b/lib/utils/neynar-client.js --- a/lib/utils/neynar-client.js +++ b/lib/utils/neynar-client.js @@ -308,6 +308,28 @@ throw error; } } + + async checkIfCurrentUserFIDIsValid(fid: string): Promise { + const url = getNeynarURL('2', 'user/bulk', { fids: fid }); + + try { + const response = await fetch(url, { + method: 'GET', + headers: { + Accept: 'application/json', + api_key: this.apiKey, + }, + }); + + return response.ok; + } catch (error) { + console.log( + 'Failed to check if current user FID is valid:', + getMessageForException(error) ?? 'unknown', + ); + throw error; + } + } } export { NeynarClient };