mirror of
https://github.com/neynarxyz/create-farcaster-mini-app.git
synced 2025-11-16 08:08:56 -05:00
Clean up
This commit is contained in:
parent
bbc8d81613
commit
bed12bd303
@ -50,8 +50,6 @@ export function NeynarAuthButton() {
|
|||||||
// Helper function to create a signer
|
// Helper function to create a signer
|
||||||
const createSigner = useCallback(async () => {
|
const createSigner = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
// console.log('🔧 Creating new signer...');
|
|
||||||
|
|
||||||
const response = await fetch('/api/auth/signer', {
|
const response = await fetch('/api/auth/signer', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
});
|
});
|
||||||
@ -61,12 +59,10 @@ export function NeynarAuthButton() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const signerData = await response.json();
|
const signerData = await response.json();
|
||||||
// console.log('✅ Signer created:', signerData);
|
|
||||||
|
|
||||||
return signerData;
|
return signerData;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// console.error('❌ Error creating signer:', error);
|
console.error('❌ Error creating signer:', error);
|
||||||
throw error;
|
// throw error;
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
@ -74,8 +70,6 @@ export function NeynarAuthButton() {
|
|||||||
const generateSignedKeyRequest = useCallback(
|
const generateSignedKeyRequest = useCallback(
|
||||||
async (signerUuid: string, publicKey: string) => {
|
async (signerUuid: string, publicKey: string) => {
|
||||||
try {
|
try {
|
||||||
// console.log('🔑 Generating signed key request...');
|
|
||||||
|
|
||||||
// Prepare request body
|
// Prepare request body
|
||||||
const requestBody: {
|
const requestBody: {
|
||||||
signerUuid: string;
|
signerUuid: string;
|
||||||
@ -102,12 +96,11 @@ export function NeynarAuthButton() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
// console.log('✅ Signed key request generated:', data);
|
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ Error generating signed key request:', error);
|
console.error('❌ Error generating signed key request:', error);
|
||||||
throw error;
|
// throw error;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[]
|
[]
|
||||||
@ -117,7 +110,6 @@ export function NeynarAuthButton() {
|
|||||||
const fetchAllSigners = useCallback(
|
const fetchAllSigners = useCallback(
|
||||||
async (message: string, signature: string) => {
|
async (message: string, signature: string) => {
|
||||||
try {
|
try {
|
||||||
// console.log('<27> Fetching all signers...');
|
|
||||||
setSignersLoading(true);
|
setSignersLoading(true);
|
||||||
|
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
@ -127,11 +119,8 @@ export function NeynarAuthButton() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const signerData = await response.json();
|
const signerData = await response.json();
|
||||||
// console.log('<27> Signer response:', signerData);
|
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
// console.log('✅ Signers fetched successfully:', signerData.signers);
|
|
||||||
|
|
||||||
// Store signers in localStorage, preserving existing auth data
|
// Store signers in localStorage, preserving existing auth data
|
||||||
const existingAuth = getItem<StoredAuthState>(STORAGE_KEY);
|
const existingAuth = getItem<StoredAuthState>(STORAGE_KEY);
|
||||||
const updatedState: StoredAuthState = {
|
const updatedState: StoredAuthState = {
|
||||||
@ -146,11 +135,11 @@ export function NeynarAuthButton() {
|
|||||||
return signerData.signers;
|
return signerData.signers;
|
||||||
} else {
|
} else {
|
||||||
console.error('❌ Failed to fetch signers');
|
console.error('❌ Failed to fetch signers');
|
||||||
throw new Error('Failed to fetch signers');
|
// throw new Error('Failed to fetch signers');
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ Error fetching signers:', error);
|
console.error('❌ Error fetching signers:', error);
|
||||||
throw error;
|
// throw error;
|
||||||
} finally {
|
} finally {
|
||||||
setSignersLoading(false);
|
setSignersLoading(false);
|
||||||
}
|
}
|
||||||
@ -161,8 +150,6 @@ export function NeynarAuthButton() {
|
|||||||
// Helper function to poll signer status
|
// Helper function to poll signer status
|
||||||
const startPolling = useCallback(
|
const startPolling = useCallback(
|
||||||
(signerUuid: string, message: string, signature: string) => {
|
(signerUuid: string, message: string, signature: string) => {
|
||||||
// console.log('<27> Starting polling for signer:', signerUuid);
|
|
||||||
|
|
||||||
const interval = setInterval(async () => {
|
const interval = setInterval(async () => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
@ -174,10 +161,8 @@ export function NeynarAuthButton() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const signerData = await response.json();
|
const signerData = await response.json();
|
||||||
// console.log('<27> Signer status:', signerData.status);
|
|
||||||
|
|
||||||
if (signerData.status === 'approved') {
|
if (signerData.status === 'approved') {
|
||||||
// console.log('🎉 Signer approved!');
|
|
||||||
clearInterval(interval);
|
clearInterval(interval);
|
||||||
setPollingInterval(null);
|
setPollingInterval(null);
|
||||||
setShowDialog(false);
|
setShowDialog(false);
|
||||||
@ -230,15 +215,11 @@ export function NeynarAuthButton() {
|
|||||||
const stored = getItem<StoredAuthState>(STORAGE_KEY);
|
const stored = getItem<StoredAuthState>(STORAGE_KEY);
|
||||||
if (stored && stored.isAuthenticated) {
|
if (stored && stored.isAuthenticated) {
|
||||||
setStoredAuth(stored);
|
setStoredAuth(stored);
|
||||||
if (stored.signers && stored.signers.length > 0) {
|
|
||||||
// console.log('📂 Loaded stored signers:', stored.signers);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Success callback - this is critical!
|
// Success callback - this is critical!
|
||||||
const onSuccessCallback = useCallback((res: unknown) => {
|
const onSuccessCallback = useCallback((res: unknown) => {
|
||||||
// console.log('🎉 Sign in successful!', res);
|
|
||||||
const existingAuth = getItem<StoredAuthState>(STORAGE_KEY);
|
const existingAuth = getItem<StoredAuthState>(STORAGE_KEY);
|
||||||
const authState: StoredAuthState = {
|
const authState: StoredAuthState = {
|
||||||
isAuthenticated: true,
|
isAuthenticated: true,
|
||||||
@ -279,30 +260,13 @@ export function NeynarAuthButton() {
|
|||||||
// Connect when component mounts and we have a nonce
|
// Connect when component mounts and we have a nonce
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (nonce && !channelToken) {
|
if (nonce && !channelToken) {
|
||||||
// console.log('🔌 Connecting with nonce:', nonce);
|
|
||||||
connect();
|
connect();
|
||||||
}
|
}
|
||||||
}, [nonce, channelToken, connect]);
|
}, [nonce, channelToken, connect]);
|
||||||
|
|
||||||
// Debug logging
|
|
||||||
// useEffect(() => {
|
|
||||||
// console.log('🔍 Auth state:', {
|
|
||||||
// isSuccess,
|
|
||||||
// validSignature,
|
|
||||||
// hasData: !!data,
|
|
||||||
// isPolling,
|
|
||||||
// isError,
|
|
||||||
// storedAuth: !!storedAuth?.isAuthenticated,
|
|
||||||
// });
|
|
||||||
// }, [isSuccess, validSignature, data, isPolling, isError, storedAuth]);
|
|
||||||
|
|
||||||
// Handle fetching signers after successful authentication
|
// Handle fetching signers after successful authentication
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (data?.message && data?.signature) {
|
if (data?.message && data?.signature) {
|
||||||
// console.log('📝 Got message and signature:', {
|
|
||||||
// message: data.message,
|
|
||||||
// signature: data.signature,
|
|
||||||
// });
|
|
||||||
const handleSignerFlow = async () => {
|
const handleSignerFlow = async () => {
|
||||||
try {
|
try {
|
||||||
// Ensure we have message and signature
|
// Ensure we have message and signature
|
||||||
@ -320,8 +284,6 @@ export function NeynarAuthButton() {
|
|||||||
|
|
||||||
// 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) {
|
||||||
// console.log('🔧 No signers found, creating new signer...');
|
|
||||||
|
|
||||||
// Step 1: Create a signer
|
// Step 1: Create a signer
|
||||||
const newSigner = await createSigner();
|
const newSigner = await createSigner();
|
||||||
|
|
||||||
@ -342,7 +304,6 @@ export function NeynarAuthButton() {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// If signers exist, close the dialog
|
// If signers exist, close the dialog
|
||||||
// console.log('✅ Signers already exist, closing dialog');
|
|
||||||
setSignersLoading(false);
|
setSignersLoading(false);
|
||||||
setShowDialog(false);
|
setShowDialog(false);
|
||||||
setDialogStep('signin');
|
setDialogStep('signin');
|
||||||
@ -367,9 +328,7 @@ export function NeynarAuthButton() {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
const handleSignIn = useCallback(() => {
|
const handleSignIn = useCallback(() => {
|
||||||
// console.log('🚀 Starting sign in flow...');
|
|
||||||
if (isError) {
|
if (isError) {
|
||||||
// console.log('🔄 Reconnecting due to error...');
|
|
||||||
reconnect();
|
reconnect();
|
||||||
}
|
}
|
||||||
setDialogStep('signin');
|
setDialogStep('signin');
|
||||||
@ -378,13 +337,11 @@ export function NeynarAuthButton() {
|
|||||||
|
|
||||||
// Open mobile app if on mobile and URL is available
|
// Open mobile app if on mobile and URL is available
|
||||||
if (url && isMobile()) {
|
if (url && isMobile()) {
|
||||||
// console.log('📱 Opening mobile app:', url);
|
|
||||||
window.open(url, '_blank');
|
window.open(url, '_blank');
|
||||||
}
|
}
|
||||||
}, [isError, reconnect, signIn, url]);
|
}, [isError, reconnect, signIn, url]);
|
||||||
|
|
||||||
const handleSignOut = useCallback(() => {
|
const handleSignOut = useCallback(() => {
|
||||||
// console.log('👋 Signing out...');
|
|
||||||
setShowDialog(false);
|
setShowDialog(false);
|
||||||
signOut();
|
signOut();
|
||||||
removeItem(STORAGE_KEY);
|
removeItem(STORAGE_KEY);
|
||||||
@ -397,28 +354,6 @@ export function NeynarAuthButton() {
|
|||||||
!!(storedAuth?.signers && storedAuth.signers.length > 0);
|
!!(storedAuth?.signers && storedAuth.signers.length > 0);
|
||||||
const userData = data || storedAuth?.userData;
|
const userData = data || storedAuth?.userData;
|
||||||
|
|
||||||
// Debug logging
|
|
||||||
// useEffect(() => {
|
|
||||||
// console.log('🔍 Auth state:', {
|
|
||||||
// authenticated,
|
|
||||||
// isSuccess,
|
|
||||||
// validSignature,
|
|
||||||
// hasData: !!data,
|
|
||||||
// isError,
|
|
||||||
// storedAuth: !!storedAuth?.isAuthenticated,
|
|
||||||
// storedSigners: storedAuth?.signers?.length || 0,
|
|
||||||
// hasUrl: !!url,
|
|
||||||
// });
|
|
||||||
// }, [
|
|
||||||
// authenticated,
|
|
||||||
// isSuccess,
|
|
||||||
// validSignature,
|
|
||||||
// data,
|
|
||||||
// isError,
|
|
||||||
// storedAuth,
|
|
||||||
// url,
|
|
||||||
// ]);
|
|
||||||
|
|
||||||
// Show loading state while nonce is being fetched or signers are loading
|
// Show loading state while nonce is being fetched or signers are loading
|
||||||
if (!nonce || signersLoading) {
|
if (!nonce || signersLoading) {
|
||||||
return (
|
return (
|
||||||
@ -482,30 +417,6 @@ export function NeynarAuthButton() {
|
|||||||
signerApprovalUrl={signerApprovalUrl}
|
signerApprovalUrl={signerApprovalUrl}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Debug panel (optional - can be removed in production) */}
|
|
||||||
{/* {process.env.NODE_ENV === "development" && (
|
|
||||||
<div className="mt-4 p-3 bg-gray-100 dark:bg-gray-800 rounded-lg text-xs font-mono">
|
|
||||||
<div className="font-semibold mb-2">Debug Info:</div>
|
|
||||||
<pre className="whitespace-pre-wrap text-xs">
|
|
||||||
{JSON.stringify(
|
|
||||||
{
|
|
||||||
authenticated,
|
|
||||||
isSuccess,
|
|
||||||
validSignature,
|
|
||||||
hasData: !!data,
|
|
||||||
isPolling,
|
|
||||||
isError,
|
|
||||||
hasStoredAuth: !!storedAuth?.isAuthenticated,
|
|
||||||
hasUrl: !!url,
|
|
||||||
hasChannelToken: !!channelToken,
|
|
||||||
},
|
|
||||||
null,
|
|
||||||
2
|
|
||||||
)}
|
|
||||||
</pre>
|
|
||||||
</div>
|
|
||||||
)} */}
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user