mirror of
https://github.com/neynarxyz/create-farcaster-mini-app.git
synced 2025-12-12 04:12:34 -05:00
Compare commits
2 Commits
2254f0d9a7
...
shreyas/ne
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
20c6113b8f | ||
|
|
343d64a77c |
@@ -515,7 +515,7 @@ export async function init(
|
|||||||
'@farcaster/miniapp-wagmi-connector': '^1.0.0',
|
'@farcaster/miniapp-wagmi-connector': '^1.0.0',
|
||||||
'@farcaster/mini-app-solana': '>=0.0.17 <1.0.0',
|
'@farcaster/mini-app-solana': '>=0.0.17 <1.0.0',
|
||||||
'@farcaster/quick-auth': '>=0.0.7 <1.0.0',
|
'@farcaster/quick-auth': '>=0.0.7 <1.0.0',
|
||||||
'@neynar/react': '^1.2.14',
|
'@neynar/react': '^1.2.13',
|
||||||
'@radix-ui/react-label': '^2.1.1',
|
'@radix-ui/react-label': '^2.1.1',
|
||||||
'@solana/wallet-adapter-react': '^0.15.38',
|
'@solana/wallet-adapter-react': '^0.15.38',
|
||||||
'@tanstack/react-query': '^5.61.0',
|
'@tanstack/react-query': '^5.61.0',
|
||||||
@@ -584,7 +584,9 @@ export async function init(
|
|||||||
"supports-color": "10.2.0",
|
"supports-color": "10.2.0",
|
||||||
"strip-ansi": "7.1.0",
|
"strip-ansi": "7.1.0",
|
||||||
"chalk": "5.6.0",
|
"chalk": "5.6.0",
|
||||||
"ansi-styles": "6.2.1"
|
"ansi-styles": "6.2.1",
|
||||||
|
"axios@^1": ">=1 <2",
|
||||||
|
"axios@^0": ">=0 <1",
|
||||||
};
|
};
|
||||||
|
|
||||||
// npm v8.3+ overrides
|
// npm v8.3+ overrides
|
||||||
|
|||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@neynar/create-farcaster-mini-app",
|
"name": "@neynar/create-farcaster-mini-app",
|
||||||
"version": "1.8.13",
|
"version": "1.8.8",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@neynar/create-farcaster-mini-app",
|
"name": "@neynar/create-farcaster-mini-app",
|
||||||
"version": "1.8.13",
|
"version": "1.8.8",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"dotenv": "^16.4.7",
|
"dotenv": "^16.4.7",
|
||||||
"inquirer": "^12.4.3",
|
"inquirer": "^12.4.3",
|
||||||
|
|||||||
@@ -1,12 +1,5 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
/**
|
|
||||||
* This authentication system is designed to work both in a regular web browser and inside a miniapp.
|
|
||||||
* In other words, it supports authentication when the miniapp context is not present (web browser) as well as when the app is running inside the miniapp.
|
|
||||||
* If you only need authentication for a web application, follow the Webapp flow;
|
|
||||||
* if you only need authentication inside a miniapp, follow the Miniapp flow.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import '@farcaster/auth-kit/styles.css';
|
import '@farcaster/auth-kit/styles.css';
|
||||||
import { useSignIn, UseSignInData } from '@farcaster/auth-kit';
|
import { useSignIn, UseSignInData } from '@farcaster/auth-kit';
|
||||||
import { useCallback, useEffect, useState, useRef } from 'react';
|
import { useCallback, useEffect, useState, useRef } from 'react';
|
||||||
@@ -17,8 +10,8 @@ import { AuthDialog } from '~/components/ui/NeynarAuthButton/AuthDialog';
|
|||||||
import { getItem, removeItem, setItem } from '~/lib/localStorage';
|
import { getItem, removeItem, setItem } from '~/lib/localStorage';
|
||||||
import { useMiniApp } from '@neynar/react';
|
import { useMiniApp } from '@neynar/react';
|
||||||
import {
|
import {
|
||||||
signIn as miniappSignIn,
|
signIn as backendSignIn,
|
||||||
signOut as miniappSignOut,
|
signOut as backendSignOut,
|
||||||
useSession,
|
useSession,
|
||||||
} from 'next-auth/react';
|
} from 'next-auth/react';
|
||||||
import sdk, { SignIn as SignInCore } from '@farcaster/miniapp-sdk';
|
import sdk, { SignIn as SignInCore } from '@farcaster/miniapp-sdk';
|
||||||
@@ -123,7 +116,7 @@ export function NeynarAuthButton() {
|
|||||||
const signerFlowStartedRef = useRef(false);
|
const signerFlowStartedRef = useRef(false);
|
||||||
|
|
||||||
// Determine which flow to use based on context
|
// Determine which flow to use based on context
|
||||||
const useMiniappFlow = context !== undefined;
|
const useBackendFlow = context !== undefined;
|
||||||
|
|
||||||
// Helper function to create a signer
|
// Helper function to create a signer
|
||||||
const createSigner = useCallback(async () => {
|
const createSigner = useCallback(async () => {
|
||||||
@@ -144,16 +137,16 @@ export function NeynarAuthButton() {
|
|||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Helper function to update session with signers (miniapp flow only)
|
// Helper function to update session with signers (backend flow only)
|
||||||
const updateSessionWithSigners = useCallback(
|
const updateSessionWithSigners = useCallback(
|
||||||
async (
|
async (
|
||||||
signers: StoredAuthState['signers'],
|
signers: StoredAuthState['signers'],
|
||||||
user: StoredAuthState['user']
|
user: StoredAuthState['user']
|
||||||
) => {
|
) => {
|
||||||
if (!useMiniappFlow) return;
|
if (!useBackendFlow) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// For miniapp flow, we need to sign in again with the additional data
|
// For backend flow, we need to sign in again with the additional data
|
||||||
if (message && signature) {
|
if (message && signature) {
|
||||||
const signInData = {
|
const signInData = {
|
||||||
message,
|
message,
|
||||||
@@ -165,13 +158,13 @@ export function NeynarAuthButton() {
|
|||||||
user: JSON.stringify(user),
|
user: JSON.stringify(user),
|
||||||
};
|
};
|
||||||
|
|
||||||
await miniappSignIn('neynar', signInData);
|
await backendSignIn('neynar', signInData);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ Error updating session with signers:', error);
|
console.error('❌ Error updating session with signers:', error);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[useMiniappFlow, message, signature, nonce]
|
[useBackendFlow, message, signature, nonce]
|
||||||
);
|
);
|
||||||
|
|
||||||
// Helper function to fetch user data from Neynar API
|
// Helper function to fetch user data from Neynar API
|
||||||
@@ -238,7 +231,7 @@ export function NeynarAuthButton() {
|
|||||||
try {
|
try {
|
||||||
setSignersLoading(true);
|
setSignersLoading(true);
|
||||||
|
|
||||||
const endpoint = useMiniappFlow
|
const endpoint = useBackendFlow
|
||||||
? `/api/auth/session-signers?message=${encodeURIComponent(
|
? `/api/auth/session-signers?message=${encodeURIComponent(
|
||||||
message
|
message
|
||||||
)}&signature=${signature}`
|
)}&signature=${signature}`
|
||||||
@@ -250,8 +243,8 @@ export function NeynarAuthButton() {
|
|||||||
const signerData = await response.json();
|
const signerData = await response.json();
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
if (useMiniappFlow) {
|
if (useBackendFlow) {
|
||||||
// For miniapp flow, update session with signers
|
// For backend flow, update session with signers
|
||||||
if (signerData.signers && signerData.signers.length > 0) {
|
if (signerData.signers && signerData.signers.length > 0) {
|
||||||
const user =
|
const user =
|
||||||
signerData.user ||
|
signerData.user ||
|
||||||
@@ -260,7 +253,7 @@ export function NeynarAuthButton() {
|
|||||||
}
|
}
|
||||||
return signerData.signers;
|
return signerData.signers;
|
||||||
} else {
|
} else {
|
||||||
// For webapp flow, store in localStorage
|
// For frontend flow, store in localStorage
|
||||||
let user: StoredAuthState['user'] | null = null;
|
let user: StoredAuthState['user'] | null = null;
|
||||||
|
|
||||||
if (signerData.signers && signerData.signers.length > 0) {
|
if (signerData.signers && signerData.signers.length > 0) {
|
||||||
@@ -292,7 +285,7 @@ export function NeynarAuthButton() {
|
|||||||
setSignersLoading(false);
|
setSignersLoading(false);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[useMiniappFlow, fetchUserData, updateSessionWithSigners]
|
[useBackendFlow, fetchUserData, updateSessionWithSigners]
|
||||||
);
|
);
|
||||||
|
|
||||||
// Helper function to poll signer status
|
// Helper function to poll signer status
|
||||||
@@ -391,21 +384,21 @@ export function NeynarAuthButton() {
|
|||||||
generateNonce();
|
generateNonce();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Load stored auth state on mount (only for webapp flow)
|
// Load stored auth state on mount (only for frontend flow)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!useMiniappFlow) {
|
if (!useBackendFlow) {
|
||||||
const stored = getItem<StoredAuthState>(STORAGE_KEY);
|
const stored = getItem<StoredAuthState>(STORAGE_KEY);
|
||||||
if (stored && stored.isAuthenticated) {
|
if (stored && stored.isAuthenticated) {
|
||||||
setStoredAuth(stored);
|
setStoredAuth(stored);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [useMiniappFlow]);
|
}, [useBackendFlow]);
|
||||||
|
|
||||||
// Success callback - this is critical!
|
// Success callback - this is critical!
|
||||||
const onSuccessCallback = useCallback(
|
const onSuccessCallback = useCallback(
|
||||||
async (res: UseSignInData) => {
|
async (res: UseSignInData) => {
|
||||||
if (!useMiniappFlow) {
|
if (!useBackendFlow) {
|
||||||
// Only handle localStorage for webapp flow
|
// Only handle localStorage for frontend flow
|
||||||
const existingAuth = getItem<StoredAuthState>(STORAGE_KEY);
|
const existingAuth = getItem<StoredAuthState>(STORAGE_KEY);
|
||||||
const user = res.fid ? await fetchUserData(res.fid) : null;
|
const user = res.fid ? await fetchUserData(res.fid) : null;
|
||||||
const authState: StoredAuthState = {
|
const authState: StoredAuthState = {
|
||||||
@@ -417,9 +410,9 @@ export function NeynarAuthButton() {
|
|||||||
setItem<StoredAuthState>(STORAGE_KEY, authState);
|
setItem<StoredAuthState>(STORAGE_KEY, authState);
|
||||||
setStoredAuth(authState);
|
setStoredAuth(authState);
|
||||||
}
|
}
|
||||||
// For miniapp flow, the session will be handled by NextAuth
|
// For backend flow, the session will be handled by NextAuth
|
||||||
},
|
},
|
||||||
[useMiniappFlow, fetchUserData]
|
[useBackendFlow, fetchUserData]
|
||||||
);
|
);
|
||||||
|
|
||||||
// Error callback
|
// Error callback
|
||||||
@@ -434,8 +427,8 @@ export function NeynarAuthButton() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const {
|
const {
|
||||||
signIn: webappSignIn,
|
signIn: frontendSignIn,
|
||||||
signOut: webappSignOut,
|
signOut: frontendSignOut,
|
||||||
connect,
|
connect,
|
||||||
reconnect,
|
reconnect,
|
||||||
isSuccess,
|
isSuccess,
|
||||||
@@ -457,12 +450,12 @@ export function NeynarAuthButton() {
|
|||||||
}
|
}
|
||||||
}, [data?.message, data?.signature]);
|
}, [data?.message, data?.signature]);
|
||||||
|
|
||||||
// Connect for webapp flow when nonce is available
|
// Connect for frontend flow when nonce is available
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!useMiniappFlow && nonce && !channelToken) {
|
if (!useBackendFlow && nonce && !channelToken) {
|
||||||
connect();
|
connect();
|
||||||
}
|
}
|
||||||
}, [useMiniappFlow, nonce, channelToken, connect]);
|
}, [useBackendFlow, nonce, channelToken, connect]);
|
||||||
|
|
||||||
// Handle fetching signers after successful authentication
|
// Handle fetching signers after successful authentication
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -485,14 +478,14 @@ export function NeynarAuthButton() {
|
|||||||
// Step 1: Change to loading state
|
// Step 1: Change to loading state
|
||||||
setDialogStep('loading');
|
setDialogStep('loading');
|
||||||
|
|
||||||
// Show dialog if not using miniapp flow or in browser farcaster
|
// Show dialog if not using backend flow or in browser farcaster
|
||||||
if ((useMiniappFlow && !isMobileContext) || !useMiniappFlow)
|
if ((useBackendFlow && !isMobileContext) || !useBackendFlow)
|
||||||
setShowDialog(true);
|
setShowDialog(true);
|
||||||
|
|
||||||
// First, fetch existing signers
|
// First, fetch existing signers
|
||||||
const signers = await fetchAllSigners(message, signature);
|
const signers = await fetchAllSigners(message, signature);
|
||||||
|
|
||||||
if (useMiniappFlow && isMobileContext) setSignersLoading(true);
|
if (useBackendFlow && isMobileContext) setSignersLoading(true);
|
||||||
|
|
||||||
// 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) {
|
||||||
@@ -545,10 +538,10 @@ export function NeynarAuthButton() {
|
|||||||
}
|
}
|
||||||
}, [message, signature]); // Simplified dependencies
|
}, [message, signature]); // Simplified dependencies
|
||||||
|
|
||||||
// Miniapp flow using NextAuth
|
// Backend flow using NextAuth
|
||||||
const handleMiniappSignIn = useCallback(async () => {
|
const handleBackendSignIn = useCallback(async () => {
|
||||||
if (!nonce) {
|
if (!nonce) {
|
||||||
console.error('❌ No nonce available for miniapp sign-in');
|
console.error('❌ No nonce available for backend sign-in');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -563,7 +556,7 @@ export function NeynarAuthButton() {
|
|||||||
nonce: nonce,
|
nonce: nonce,
|
||||||
};
|
};
|
||||||
|
|
||||||
const nextAuthResult = await miniappSignIn('neynar', signInData);
|
const nextAuthResult = await backendSignIn('neynar', signInData);
|
||||||
if (nextAuthResult?.ok) {
|
if (nextAuthResult?.ok) {
|
||||||
setMessage(result.message);
|
setMessage(result.message);
|
||||||
setSignature(result.signature);
|
setSignature(result.signature);
|
||||||
@@ -574,34 +567,34 @@ export function NeynarAuthButton() {
|
|||||||
if (e instanceof SignInCore.RejectedByUser) {
|
if (e instanceof SignInCore.RejectedByUser) {
|
||||||
console.log('ℹ️ Sign-in rejected by user');
|
console.log('ℹ️ Sign-in rejected by user');
|
||||||
} else {
|
} else {
|
||||||
console.error('❌ Miniapp sign-in error:', e);
|
console.error('❌ Backend sign-in error:', e);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
setSignersLoading(false);
|
setSignersLoading(false);
|
||||||
}
|
}
|
||||||
}, [nonce]);
|
}, [nonce]);
|
||||||
|
|
||||||
const handleWebappSignIn = useCallback(() => {
|
const handleFrontEndSignIn = useCallback(() => {
|
||||||
if (isError) {
|
if (isError) {
|
||||||
reconnect();
|
reconnect();
|
||||||
}
|
}
|
||||||
setDialogStep('signin');
|
setDialogStep('signin');
|
||||||
setShowDialog(true);
|
setShowDialog(true);
|
||||||
webappSignIn();
|
frontendSignIn();
|
||||||
}, [isError, reconnect, webappSignIn]);
|
}, [isError, reconnect, frontendSignIn]);
|
||||||
|
|
||||||
const handleSignOut = useCallback(async () => {
|
const handleSignOut = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
setSignersLoading(true);
|
setSignersLoading(true);
|
||||||
|
|
||||||
if (useMiniappFlow) {
|
if (useBackendFlow) {
|
||||||
// Only sign out from NextAuth if the current session is from Neynar provider
|
// Only sign out from NextAuth if the current session is from Neynar provider
|
||||||
if (session?.provider === 'neynar') {
|
if (session?.provider === 'neynar') {
|
||||||
await miniappSignOut({ redirect: false });
|
await backendSignOut({ redirect: false });
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Webapp flow sign out
|
// Frontend flow sign out
|
||||||
webappSignOut();
|
frontendSignOut();
|
||||||
removeItem(STORAGE_KEY);
|
removeItem(STORAGE_KEY);
|
||||||
setStoredAuth(null);
|
setStoredAuth(null);
|
||||||
}
|
}
|
||||||
@@ -627,9 +620,9 @@ export function NeynarAuthButton() {
|
|||||||
} finally {
|
} finally {
|
||||||
setSignersLoading(false);
|
setSignersLoading(false);
|
||||||
}
|
}
|
||||||
}, [useMiniappFlow, webappSignOut, pollingInterval, session]);
|
}, [useBackendFlow, frontendSignOut, pollingInterval, session]);
|
||||||
|
|
||||||
const authenticated = useMiniappFlow
|
const authenticated = useBackendFlow
|
||||||
? !!(
|
? !!(
|
||||||
session?.provider === 'neynar' &&
|
session?.provider === 'neynar' &&
|
||||||
session?.user?.fid &&
|
session?.user?.fid &&
|
||||||
@@ -639,7 +632,7 @@ export function NeynarAuthButton() {
|
|||||||
: ((isSuccess && validSignature) || storedAuth?.isAuthenticated) &&
|
: ((isSuccess && validSignature) || storedAuth?.isAuthenticated) &&
|
||||||
!!(storedAuth?.signers && storedAuth.signers.length > 0);
|
!!(storedAuth?.signers && storedAuth.signers.length > 0);
|
||||||
|
|
||||||
const userData = useMiniappFlow
|
const userData = useBackendFlow
|
||||||
? {
|
? {
|
||||||
fid: session?.user?.fid,
|
fid: session?.user?.fid,
|
||||||
username: session?.user?.username || '',
|
username: session?.user?.username || '',
|
||||||
@@ -671,16 +664,16 @@ export function NeynarAuthButton() {
|
|||||||
<ProfileButton userData={userData} onSignOut={handleSignOut} />
|
<ProfileButton userData={userData} onSignOut={handleSignOut} />
|
||||||
) : (
|
) : (
|
||||||
<Button
|
<Button
|
||||||
onClick={useMiniappFlow ? handleMiniappSignIn : handleWebappSignIn}
|
onClick={useBackendFlow ? handleBackendSignIn : handleFrontEndSignIn}
|
||||||
disabled={!useMiniappFlow && !url}
|
disabled={!useBackendFlow && !url}
|
||||||
className={cn(
|
className={cn(
|
||||||
'btn btn-primary flex items-center gap-3',
|
'btn btn-primary flex items-center gap-3',
|
||||||
'disabled:opacity-50 disabled:cursor-not-allowed',
|
'disabled:opacity-50 disabled:cursor-not-allowed',
|
||||||
'transform transition-all duration-200 active:scale-[0.98]',
|
'transform transition-all duration-200 active:scale-[0.98]',
|
||||||
!url && !useMiniappFlow && 'cursor-not-allowed'
|
!url && !useBackendFlow && 'cursor-not-allowed'
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
{!useMiniappFlow && !url ? (
|
{!useBackendFlow && !url ? (
|
||||||
<>
|
<>
|
||||||
<div className="spinner-primary w-5 h-5" />
|
<div className="spinner-primary w-5 h-5" />
|
||||||
<span>Initializing...</span>
|
<span>Initializing...</span>
|
||||||
|
|||||||
Reference in New Issue
Block a user