mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-03 08:07:21 +00:00
fix: callback for non-mobile and localstorage
This commit is contained in:
1 parent
86d3d67eef
commit
ac88a3a5c1
3 files changed
+121
-3
No files matched your search
@@ -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 */}
|
||||
|
||||
@@ -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()}`;
|
||||
|
||||
@@ -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>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user