From 7b431677dcbad9355ceb8e6ecba088ee15ec04f0 Mon Sep 17 00:00:00 2001 From: Shreyaschorge Date: Fri, 11 Jul 2025 02:35:25 +0530 Subject: [PATCH] fix 401 and cleanup --- src/app/api/auth/session-signers/route.ts | 9 ----- src/components/ui/NeynarAuthButton/index.tsx | 35 +++----------------- 2 files changed, 5 insertions(+), 39 deletions(-) diff --git a/src/app/api/auth/session-signers/route.ts b/src/app/api/auth/session-signers/route.ts index 0d23d48..9d41b0d 100644 --- a/src/app/api/auth/session-signers/route.ts +++ b/src/app/api/auth/session-signers/route.ts @@ -5,15 +5,6 @@ import { getNeynarClient } from '~/lib/neynar'; export async function GET(request: Request) { try { - const session = await getServerSession(authOptions); - - if (!session?.user?.fid) { - return NextResponse.json( - { error: 'No authenticated session found' }, - { status: 401 } - ); - } - const { searchParams } = new URL(request.url); const message = searchParams.get('message'); const signature = searchParams.get('signature'); diff --git a/src/components/ui/NeynarAuthButton/index.tsx b/src/components/ui/NeynarAuthButton/index.tsx index a4d18a3..c8f1492 100644 --- a/src/components/ui/NeynarAuthButton/index.tsx +++ b/src/components/ui/NeynarAuthButton/index.tsx @@ -113,7 +113,6 @@ export function NeynarAuthButton() { ); const [message, setMessage] = useState(null); const [signature, setSignature] = useState(null); - const [debugState, setDebugState] = useState(null); // Determine which flow to use based on context const useBackendFlow = context !== undefined; @@ -264,9 +263,7 @@ export function NeynarAuthButton() { } // Store signers in localStorage, preserving existing auth data - const existingAuth = getItem(STORAGE_KEY); const updatedState: StoredAuthState = { - ...existingAuth, isAuthenticated: !!user, signers: signerData.signers || [], user, @@ -365,13 +362,15 @@ export function NeynarAuthButton() { // Success callback - this is critical! const onSuccessCallback = useCallback( - (res: unknown) => { + async (res: unknown) => { if (!useBackendFlow) { // Only handle localStorage for frontend flow const existingAuth = getItem(STORAGE_KEY); + const user = await fetchUserData(res.fid); const authState: StoredAuthState = { + ...existingAuth, isAuthenticated: true, - user: res as StoredAuthState['user'], + user: user as StoredAuthState['user'], signers: existingAuth?.signers || [], // Preserve existing signers }; setItem(STORAGE_KEY, authState); @@ -424,35 +423,24 @@ export function NeynarAuthButton() { if (message && signature) { const handleSignerFlow = async () => { try { - // Step 1: Change to loading state - setDialogStep('loading'); - setSignersLoading(true); - // First, fetch existing signers const signers = await fetchAllSigners(message, signature); - - setDebugState('Fetched signers...'); + setSignersLoading(true); // Check if no signers exist or if we have empty signers if (!signers || signers.length === 0) { // Step 1: Create a signer const newSigner = await createSigner(); - setDebugState('Created new signer...'); - // Step 2: Generate signed key request const signedKeyData = await generateSignedKeyRequest( newSigner.signer_uuid, newSigner.public_key ); - setDebugState('Generated signed key request...'); - // Step 3: Show QR code in access dialog for signer approval if (signedKeyData.signer_approval_url) { - setDebugState('Setting signer approval URL...'); setSignerApprovalUrl(signedKeyData.signer_approval_url); - setSignersLoading(false); // Stop loading, show QR code // Check if we're in a mobile context const clientContext = context?.client as Record; const isMobileContext = @@ -460,7 +448,6 @@ export function NeynarAuthButton() { clientContext?.clientFid === FARCASTER_FID; if (isMobileContext) { - setDebugState('Opening mobile app...'); setShowDialog(false); await sdk.actions.openUrl( signedKeyData.signer_approval_url.replace( @@ -469,11 +456,6 @@ export function NeynarAuthButton() { ) ); } else { - setDebugState( - 'Opening access dialog...' + - ` ${clientContext?.platformType}` + - ` ${clientContext?.clientFid}` - ); setDialogStep('access'); setShowDialog(true); } @@ -482,14 +464,11 @@ export function NeynarAuthButton() { startPolling(newSigner.signer_uuid, message, signature); } } else { - setDebugState('Signers already exist, proceeding to signin...'); - // If signers exist, close the dialog setSignersLoading(false); setShowDialog(false); setDialogStep('signin'); } } catch (error) { - setDebugState('Error in signer flow:'); console.error('❌ Error in signer flow:', error); // On error, reset to signin step setDialogStep('signin'); @@ -578,7 +557,6 @@ export function NeynarAuthButton() { setSignerApprovalUrl(null); setMessage(null); setSignature(null); - setDebugState(null); // Reset polling interval if (pollingInterval) { @@ -657,9 +635,6 @@ export function NeynarAuthButton() { )} -

LocalStorage state

- {window && JSON.stringify(window.localStorage.getItem(STORAGE_KEY))} - {/* Unified Auth Dialog */} {