mirror of
https://github.com/neynarxyz/create-farcaster-mini-app.git
synced 2025-11-21 02:16:12 -05:00
format
This commit is contained in:
@@ -34,25 +34,25 @@ interface UseQuickAuthReturn {
|
||||
|
||||
/**
|
||||
* Custom hook for managing QuickAuth authentication state
|
||||
*
|
||||
*
|
||||
* This hook provides a complete authentication flow using Farcaster's QuickAuth:
|
||||
* - Automatically checks for existing authentication on mount
|
||||
* - Validates tokens with the server-side API
|
||||
* - Manages authentication state in memory (no persistence)
|
||||
* - Provides sign-in/sign-out functionality
|
||||
*
|
||||
*
|
||||
* QuickAuth tokens are managed in memory only, so signing out of the Farcaster
|
||||
* client will automatically sign the user out of this mini app as well.
|
||||
*
|
||||
*
|
||||
* @returns {UseQuickAuthReturn} Object containing user state and authentication methods
|
||||
*
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* const { authenticatedUser, status, signIn, signOut } = useQuickAuth();
|
||||
*
|
||||
*
|
||||
* if (status === 'loading') return <div>Loading...</div>;
|
||||
* if (status === 'unauthenticated') return <button onClick={signIn}>Sign In</button>;
|
||||
*
|
||||
*
|
||||
* return (
|
||||
* <div>
|
||||
* <p>Welcome, FID: {authenticatedUser?.fid}</p>
|
||||
@@ -63,17 +63,20 @@ interface UseQuickAuthReturn {
|
||||
*/
|
||||
export function useQuickAuth(): UseQuickAuthReturn {
|
||||
// Current authenticated user data
|
||||
const [authenticatedUser, setAuthenticatedUser] = useState<AuthenticatedUser | null>(null);
|
||||
const [authenticatedUser, setAuthenticatedUser] =
|
||||
useState<AuthenticatedUser | null>(null);
|
||||
// Current authentication status
|
||||
const [status, setStatus] = useState<QuickAuthStatus>('loading');
|
||||
|
||||
/**
|
||||
* Validates a QuickAuth token with the server-side API
|
||||
*
|
||||
*
|
||||
* @param {string} authToken - The JWT token to validate
|
||||
* @returns {Promise<AuthenticatedUser | null>} User data if valid, null otherwise
|
||||
*/
|
||||
const validateTokenWithServer = async (authToken: string): Promise<AuthenticatedUser | null> => {
|
||||
const validateTokenWithServer = async (
|
||||
authToken: string,
|
||||
): Promise<AuthenticatedUser | null> => {
|
||||
try {
|
||||
const validationResponse = await fetch('/api/auth/validate', {
|
||||
method: 'POST',
|
||||
@@ -85,7 +88,7 @@ export function useQuickAuth(): UseQuickAuthReturn {
|
||||
const responseData = await validationResponse.json();
|
||||
return responseData.user;
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.error('Token validation failed:', error);
|
||||
@@ -102,11 +105,11 @@ export function useQuickAuth(): UseQuickAuthReturn {
|
||||
try {
|
||||
// Attempt to retrieve existing token from QuickAuth SDK
|
||||
const { token } = await sdk.quickAuth.getToken();
|
||||
|
||||
|
||||
if (token) {
|
||||
// Validate the token with our server-side API
|
||||
const validatedUserSession = await validateTokenWithServer(token);
|
||||
|
||||
|
||||
if (validatedUserSession) {
|
||||
// Token is valid, set authenticated state
|
||||
setAuthenticatedUser(validatedUserSession);
|
||||
@@ -130,24 +133,24 @@ export function useQuickAuth(): UseQuickAuthReturn {
|
||||
|
||||
/**
|
||||
* Initiates the QuickAuth sign-in process
|
||||
*
|
||||
*
|
||||
* Uses sdk.quickAuth.getToken() to get a QuickAuth session token.
|
||||
* If there is already a session token in memory that hasn't expired,
|
||||
* it will be immediately returned, otherwise a fresh one will be acquired.
|
||||
*
|
||||
*
|
||||
* @returns {Promise<boolean>} True if sign-in was successful, false otherwise
|
||||
*/
|
||||
const signIn = useCallback(async (): Promise<boolean> => {
|
||||
try {
|
||||
setStatus('loading');
|
||||
|
||||
|
||||
// Get QuickAuth session token
|
||||
const { token } = await sdk.quickAuth.getToken();
|
||||
|
||||
|
||||
if (token) {
|
||||
// Validate the token with our server-side API
|
||||
const validatedUserSession = await validateTokenWithServer(token);
|
||||
|
||||
|
||||
if (validatedUserSession) {
|
||||
// Authentication successful, update user state
|
||||
setAuthenticatedUser(validatedUserSession);
|
||||
@@ -155,7 +158,7 @@ export function useQuickAuth(): UseQuickAuthReturn {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Authentication failed, clear user state
|
||||
setStatus('unauthenticated');
|
||||
return false;
|
||||
@@ -168,7 +171,7 @@ export function useQuickAuth(): UseQuickAuthReturn {
|
||||
|
||||
/**
|
||||
* Signs out the current user and clears the authentication state
|
||||
*
|
||||
*
|
||||
* Since QuickAuth tokens are managed in memory only, this simply clears
|
||||
* the local user state. The actual token will be cleared when the
|
||||
* user signs out of their Farcaster client.
|
||||
@@ -181,7 +184,7 @@ export function useQuickAuth(): UseQuickAuthReturn {
|
||||
|
||||
/**
|
||||
* Retrieves the current authentication token from QuickAuth
|
||||
*
|
||||
*
|
||||
* @returns {Promise<string | null>} The current auth token, or null if not authenticated
|
||||
*/
|
||||
const getToken = useCallback(async (): Promise<string | null> => {
|
||||
@@ -201,4 +204,4 @@ export function useQuickAuth(): UseQuickAuthReturn {
|
||||
signOut,
|
||||
getToken,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user