From ac88a3a5c1a9c1e8556b4a453495e4a5907b5587 Mon Sep 17 00:00:00 2001 From: Derek Ross Date: Tue, 30 Dec 2025 20:31:46 -0500 Subject: [PATCH] fix: callback for non-mobile and localstorage --- src/AppRouter.tsx | 2 + src/hooks/useLoginActions.ts | 13 +++- src/pages/RemoteLoginSuccess.tsx | 109 +++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 src/pages/RemoteLoginSuccess.tsx diff --git a/src/AppRouter.tsx b/src/AppRouter.tsx index 2db53ee..46a8a84 100644 --- a/src/AppRouter.tsx +++ b/src/AppRouter.tsx @@ -3,6 +3,7 @@ import { ScrollToTop } from "./components/ScrollToTop"; import Index from "./pages/Index"; import { NIP19Page } from "./pages/NIP19Page"; +import { RemoteLoginSuccess } from "./pages/RemoteLoginSuccess"; import NotFound from "./pages/NotFound"; export function AppRouter() { @@ -11,6 +12,7 @@ export function AppRouter() { } /> + } /> {/* NIP-19 route for npub1, note1, naddr1, nevent1, nprofile1 */} } /> {/* ADD ALL CUSTOM ROUTES ABOVE THE CATCH-ALL "*" ROUTE */} diff --git a/src/hooks/useLoginActions.ts b/src/hooks/useLoginActions.ts index 2db98d4..0318f01 100644 --- a/src/hooks/useLoginActions.ts +++ b/src/hooks/useLoginActions.ts @@ -8,6 +8,12 @@ import { useAppContext } from '@/hooks/useAppContext'; // NOTE: This file should not be edited except for adding new login methods. +/** Check if running on actual mobile device (not just small screen) */ +function isMobileDevice(): boolean { + if (typeof navigator === 'undefined') return false; + return /Android|iPhone|iPad|iPod/i.test(navigator.userAgent); +} + /** Parameters for initiating a nostrconnect:// session */ export interface NostrConnectParams { clientSecretKey: Uint8Array; @@ -29,9 +35,10 @@ export function generateNostrConnectURI(params: NostrConnectParams, appName?: st searchParams.set('name', appName); } - // Add callback URL based on current origin (works on any domain) - if (typeof window !== 'undefined') { - searchParams.set('callback', window.location.origin); + // Add callback URL only on mobile devices (not desktop QR code scanning) + // When scanning QR from desktop, the signer app is on the phone but the session is on desktop + if (typeof window !== 'undefined' && isMobileDevice()) { + searchParams.set('callback', `${window.location.origin}/remoteloginsuccess`); } return `nostrconnect://${params.clientPubkey}?${searchParams.toString()}`; diff --git a/src/pages/RemoteLoginSuccess.tsx b/src/pages/RemoteLoginSuccess.tsx new file mode 100644 index 0000000..4419a5b --- /dev/null +++ b/src/pages/RemoteLoginSuccess.tsx @@ -0,0 +1,109 @@ +import { useEffect, useState, useCallback } from 'react'; +import { useNavigate } from 'react-router-dom'; +import { useNostrLogin } from '@nostrify/react/login'; +import { Loader2, CheckCircle, XCircle } from 'lucide-react'; + +// Storage key used by @nostrify/react for logins +const LOGINS_STORAGE_KEY = 'nostrify:logins'; + +export function RemoteLoginSuccess() { + const navigate = useNavigate(); + const { logins } = useNostrLogin(); + const [checkCount, setCheckCount] = useState(0); + const [status, setStatus] = useState<'checking' | 'success' | 'timeout'>('checking'); + + // Check localStorage directly as a fallback + const checkLocalStorage = useCallback(() => { + try { + const stored = localStorage.getItem(LOGINS_STORAGE_KEY); + if (stored) { + const parsed = JSON.parse(stored); + return Array.isArray(parsed) && parsed.length > 0; + } + } catch { + // Ignore parse errors + } + return false; + }, []); + + // Check if logged in via React state or localStorage + const isLoggedIn = logins.length > 0 || checkLocalStorage(); + + useEffect(() => { + if (isLoggedIn) { + setStatus('success'); + const timer = setTimeout(() => { + navigate('/', { replace: true }); + }, 1500); + return () => clearTimeout(timer); + } + + // Check up to 20 times (10 seconds total) for the session to become active + if (checkCount < 20) { + const timer = setTimeout(() => { + setCheckCount(prev => prev + 1); + }, 500); + return () => clearTimeout(timer); + } else { + setStatus('timeout'); + } + }, [isLoggedIn, checkCount, navigate, checkLocalStorage]); + + // Listen for storage events (in case login is added from another context) + useEffect(() => { + const handleStorage = (e: StorageEvent) => { + if (e.key === LOGINS_STORAGE_KEY && e.newValue) { + try { + const parsed = JSON.parse(e.newValue); + if (Array.isArray(parsed) && parsed.length > 0) { + setStatus('success'); + setTimeout(() => navigate('/', { replace: true }), 1500); + } + } catch { + // Ignore parse errors + } + } + }; + + window.addEventListener('storage', handleStorage); + return () => window.removeEventListener('storage', handleStorage); + }, [navigate]); + + return ( +
+
+ {status === 'checking' && ( + <> + +

Completing Login...

+

Please wait while we verify your session.

+ + )} + + {status === 'success' && ( + <> + +

Login Successful!

+

Redirecting you now...

+ + )} + + {status === 'timeout' && ( + <> + +

Session Not Found

+

+ We couldn't detect an active login session. +

+ + Return to home page + + + )} +
+
+ ); +}