import React, { useEffect, useState } from 'react'; import { useNavigate, useSearchParams } from 'react-router-dom'; import { Loader2, Zap } from 'lucide-react'; import { useStore } from '../store/useStore'; export function Login() { const navigate = useNavigate(); const [searchParams] = useSearchParams(); const { user, setUser } = useStore(); const [isLoading, setIsLoading] = useState(false); const [pubkeyInput, setPubkeyInput] = useState(''); const isIframe = searchParams.get('iframe') === '1'; const urlPubkey = searchParams.get('npub') || searchParams.get('pubkey'); useEffect(() => { // Handle URL-based login if (urlPubkey && !user) { setUser({ pubkey: urlPubkey, isWhitelisted: false }); navigate(isIframe ? '/dashboard?iframe=1' : '/dashboard'); return; } // Regular user redirect if (user) { navigate(isIframe ? '/dashboard?iframe=1' : '/dashboard'); } }, [user, navigate, isIframe, urlPubkey, setUser]); const handleExtensionLogin = async () => { setIsLoading(true); try { let attempts = 0; while (!window.nostr && attempts < 50) { await new Promise(resolve => setTimeout(resolve, 100)); attempts++; } if (!window.nostr) { throw new Error('Nostr provider not found after waiting'); } const pubkey = await window.nostr.getPublicKey(); if (!pubkey) { throw new Error('No public key found'); } setUser({ pubkey, isWhitelisted: false }); navigate(isIframe ? '/dashboard?iframe=1' : '/dashboard'); } catch (error: any) { if (error.message === 'Rejected by user') { return; } console.error('Login failed:', error); if (error.message.includes('Nostr provider not found')) { alert('No Nostr extension detected. Please install Alby or another Nostr extension and try again.'); } else if (error.message.includes('No public key found')) { alert('Could not access your Nostr public key. Please make sure you\'re logged into your Nostr extension.'); } else if (error.message !== 'Rejected by user') { alert('Failed to connect. Please make sure you have a Nostr extension installed and try again.'); } } finally { setIsLoading(false); } }; const handlePubkeyLogin = async () => { if (!pubkeyInput.trim()) { alert('Please enter a public key or npub'); return; } setIsLoading(true); try { setUser({ pubkey: pubkeyInput.trim(), isWhitelisted: false }); navigate(isIframe ? '/dashboard?iframe=1' : '/dashboard'); } catch (error) { console.error('Login failed:', error); alert('Failed to login with the provided public key. Please check the format and try again.'); } finally { setIsLoading(false); } }; // If we're processing URL-based login, show loading state if (urlPubkey && !user) { return (
); } return (

Connect with Nostr

To access the Noderunners relay, connect using your Nostr account. You can use a Nostr extension or enter your public key manually.

Or enter manually
setPubkeyInput(e.target.value)} placeholder="npub1... or hex public key" className="w-full px-4 py-3 bg-gray-900 border border-gray-700 rounded-lg focus:ring-2 focus:ring-orange-500 focus:border-transparent text-white placeholder-gray-500" />

Don't have a Nostr extension?{' '} Get Alby

); }