mirror of
https://github.com/neynarxyz/create-farcaster-mini-app.git
synced 2025-12-08 02:12:34 -05:00
Revert "Merge pull request #15 from neynarxyz/shreyas-formatting"
This reverts commitb1fdfc19a9, reversing changes made tob9e2087bd8.
This commit is contained in:
@@ -2,7 +2,7 @@ import { useEffect } from 'react';
|
||||
|
||||
export function useDetectClickOutside<T extends HTMLElement>(
|
||||
ref: React.RefObject<T | null>,
|
||||
callback: () => void,
|
||||
callback: () => void
|
||||
) {
|
||||
useEffect(() => {
|
||||
function handleClickOutside(event: MouseEvent) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export interface NeynarUser {
|
||||
fid: number;
|
||||
@@ -19,21 +19,20 @@ export function useNeynarUser(context?: { user?: { fid?: number } }) {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
fetch(`/api/users?fids=${context.user.fid}`)
|
||||
.then(response => {
|
||||
if (!response.ok)
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
.then((response) => {
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
.then((data) => {
|
||||
if (data.users?.[0]) {
|
||||
setUser(data.users[0]);
|
||||
} else {
|
||||
setUser(null);
|
||||
}
|
||||
})
|
||||
.catch(err => setError(err.message))
|
||||
.catch((err) => setError(err.message))
|
||||
.finally(() => setLoading(false));
|
||||
}, [context?.user?.fid]);
|
||||
|
||||
return { user, loading, error };
|
||||
}
|
||||
}
|
||||
@@ -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,20 +63,17 @@ 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',
|
||||
@@ -88,7 +85,7 @@ export function useQuickAuth(): UseQuickAuthReturn {
|
||||
const responseData = await validationResponse.json();
|
||||
return responseData.user;
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.error('Token validation failed:', error);
|
||||
@@ -105,11 +102,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);
|
||||
@@ -133,24 +130,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);
|
||||
@@ -158,7 +155,7 @@ export function useQuickAuth(): UseQuickAuthReturn {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Authentication failed, clear user state
|
||||
setStatus('unauthenticated');
|
||||
return false;
|
||||
@@ -171,7 +168,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.
|
||||
@@ -184,7 +181,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> => {
|
||||
@@ -204,4 +201,4 @@ export function useQuickAuth(): UseQuickAuthReturn {
|
||||
signOut,
|
||||
getToken,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user