fix: callback for non-mobile and localstorage

This commit is contained in:
Derek Ross committed 2025-12-30 20:31:46 -05:00
1 parent 86d3d67eef
commit ac88a3a5c1
3 files changed
+121 -3

No files matched your search

+2
View File
@@ -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() {
<ScrollToTop />
<Routes>
<Route path="/" element={<Index />} />
<Route path="/remoteloginsuccess" element={<RemoteLoginSuccess />} />
{/* NIP-19 route for npub1, note1, naddr1, nevent1, nprofile1 */}
<Route path="/:nip19" element={<NIP19Page />} />
{/* ADD ALL CUSTOM ROUTES ABOVE THE CATCH-ALL "*" ROUTE */}
+10 -3
View File
@@ -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()}`;
+109
View File
@@ -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 (
<div className="min-h-screen flex items-center justify-center bg-background">
<div className="text-center p-8">
{status === 'checking' && (
<>
<Loader2 className="h-16 w-16 animate-spin text-primary mx-auto mb-4" />
<h1 className="text-2xl font-bold mb-2">Completing Login...</h1>
<p className="text-muted-foreground">Please wait while we verify your session.</p>
</>
)}
{status === 'success' && (
<>
<CheckCircle className="h-16 w-16 text-green-500 mx-auto mb-4" />
<h1 className="text-2xl font-bold mb-2">Login Successful!</h1>
<p className="text-muted-foreground">Redirecting you now...</p>
</>
)}
{status === 'timeout' && (
<>
<XCircle className="h-16 w-16 text-destructive mx-auto mb-4" />
<h1 className="text-2xl font-bold mb-2">Session Not Found</h1>
<p className="text-muted-foreground mb-4">
We couldn't detect an active login session.
</p>
<a
href="/"
className="text-primary hover:underline"
>
Return to home page
</a>
</>
)}
</div>
</div>
);
}