fix 401 and cleanup

This commit is contained in:
Shreyaschorge 2025-07-11 02:35:25 +05:30
parent 797c5b7154
commit 7b431677dc
No known key found for this signature in database
2 changed files with 5 additions and 39 deletions

View File

@ -5,15 +5,6 @@ import { getNeynarClient } from '~/lib/neynar';
export async function GET(request: Request) { export async function GET(request: Request) {
try { 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 { searchParams } = new URL(request.url);
const message = searchParams.get('message'); const message = searchParams.get('message');
const signature = searchParams.get('signature'); const signature = searchParams.get('signature');

View File

@ -113,7 +113,6 @@ export function NeynarAuthButton() {
); );
const [message, setMessage] = useState<string | null>(null); const [message, setMessage] = useState<string | null>(null);
const [signature, setSignature] = useState<string | null>(null); const [signature, setSignature] = useState<string | null>(null);
const [debugState, setDebugState] = useState<string | null>(null);
// Determine which flow to use based on context // Determine which flow to use based on context
const useBackendFlow = context !== undefined; const useBackendFlow = context !== undefined;
@ -264,9 +263,7 @@ export function NeynarAuthButton() {
} }
// Store signers in localStorage, preserving existing auth data // Store signers in localStorage, preserving existing auth data
const existingAuth = getItem<StoredAuthState>(STORAGE_KEY);
const updatedState: StoredAuthState = { const updatedState: StoredAuthState = {
...existingAuth,
isAuthenticated: !!user, isAuthenticated: !!user,
signers: signerData.signers || [], signers: signerData.signers || [],
user, user,
@ -365,13 +362,15 @@ export function NeynarAuthButton() {
// Success callback - this is critical! // Success callback - this is critical!
const onSuccessCallback = useCallback( const onSuccessCallback = useCallback(
(res: unknown) => { async (res: unknown) => {
if (!useBackendFlow) { if (!useBackendFlow) {
// Only handle localStorage for frontend flow // Only handle localStorage for frontend flow
const existingAuth = getItem<StoredAuthState>(STORAGE_KEY); const existingAuth = getItem<StoredAuthState>(STORAGE_KEY);
const user = await fetchUserData(res.fid);
const authState: StoredAuthState = { const authState: StoredAuthState = {
...existingAuth,
isAuthenticated: true, isAuthenticated: true,
user: res as StoredAuthState['user'], user: user as StoredAuthState['user'],
signers: existingAuth?.signers || [], // Preserve existing signers signers: existingAuth?.signers || [], // Preserve existing signers
}; };
setItem<StoredAuthState>(STORAGE_KEY, authState); setItem<StoredAuthState>(STORAGE_KEY, authState);
@ -424,35 +423,24 @@ export function NeynarAuthButton() {
if (message && signature) { if (message && signature) {
const handleSignerFlow = async () => { const handleSignerFlow = async () => {
try { try {
// Step 1: Change to loading state
setDialogStep('loading');
setSignersLoading(true);
// First, fetch existing signers // First, fetch existing signers
const signers = await fetchAllSigners(message, signature); const signers = await fetchAllSigners(message, signature);
setSignersLoading(true);
setDebugState('Fetched signers...');
// Check if no signers exist or if we have empty signers // Check if no signers exist or if we have empty signers
if (!signers || signers.length === 0) { if (!signers || signers.length === 0) {
// Step 1: Create a signer // Step 1: Create a signer
const newSigner = await createSigner(); const newSigner = await createSigner();
setDebugState('Created new signer...');
// Step 2: Generate signed key request // Step 2: Generate signed key request
const signedKeyData = await generateSignedKeyRequest( const signedKeyData = await generateSignedKeyRequest(
newSigner.signer_uuid, newSigner.signer_uuid,
newSigner.public_key newSigner.public_key
); );
setDebugState('Generated signed key request...');
// Step 3: Show QR code in access dialog for signer approval // Step 3: Show QR code in access dialog for signer approval
if (signedKeyData.signer_approval_url) { if (signedKeyData.signer_approval_url) {
setDebugState('Setting signer approval URL...');
setSignerApprovalUrl(signedKeyData.signer_approval_url); setSignerApprovalUrl(signedKeyData.signer_approval_url);
setSignersLoading(false); // Stop loading, show QR code
// Check if we're in a mobile context // Check if we're in a mobile context
const clientContext = context?.client as Record<string, unknown>; const clientContext = context?.client as Record<string, unknown>;
const isMobileContext = const isMobileContext =
@ -460,7 +448,6 @@ export function NeynarAuthButton() {
clientContext?.clientFid === FARCASTER_FID; clientContext?.clientFid === FARCASTER_FID;
if (isMobileContext) { if (isMobileContext) {
setDebugState('Opening mobile app...');
setShowDialog(false); setShowDialog(false);
await sdk.actions.openUrl( await sdk.actions.openUrl(
signedKeyData.signer_approval_url.replace( signedKeyData.signer_approval_url.replace(
@ -469,11 +456,6 @@ export function NeynarAuthButton() {
) )
); );
} else { } else {
setDebugState(
'Opening access dialog...' +
` ${clientContext?.platformType}` +
` ${clientContext?.clientFid}`
);
setDialogStep('access'); setDialogStep('access');
setShowDialog(true); setShowDialog(true);
} }
@ -482,14 +464,11 @@ export function NeynarAuthButton() {
startPolling(newSigner.signer_uuid, message, signature); startPolling(newSigner.signer_uuid, message, signature);
} }
} else { } else {
setDebugState('Signers already exist, proceeding to signin...');
// If signers exist, close the dialog
setSignersLoading(false); setSignersLoading(false);
setShowDialog(false); setShowDialog(false);
setDialogStep('signin'); setDialogStep('signin');
} }
} catch (error) { } catch (error) {
setDebugState('Error in signer flow:');
console.error('❌ Error in signer flow:', error); console.error('❌ Error in signer flow:', error);
// On error, reset to signin step // On error, reset to signin step
setDialogStep('signin'); setDialogStep('signin');
@ -578,7 +557,6 @@ export function NeynarAuthButton() {
setSignerApprovalUrl(null); setSignerApprovalUrl(null);
setMessage(null); setMessage(null);
setSignature(null); setSignature(null);
setDebugState(null);
// Reset polling interval // Reset polling interval
if (pollingInterval) { if (pollingInterval) {
@ -657,9 +635,6 @@ export function NeynarAuthButton() {
</Button> </Button>
)} )}
<p>LocalStorage state</p>
{window && JSON.stringify(window.localStorage.getItem(STORAGE_KEY))}
{/* Unified Auth Dialog */} {/* Unified Auth Dialog */}
{ {
<AuthDialog <AuthDialog