fix: rewrite login to use NIP-46 nostrconnect flow (Ditto pattern)

Completely replaces the broken NIP-55 intent approach with NIP-46:

- Amber tab: generates nostrconnect:// URI, opens Amber, waits for
  Amber to connect back via relay using NLogin.fromNostrConnect()
  (same pattern as Ditto/soapbox-pub)
- Bunker tab: paste bunker:// URI from Amber or nsec.app
- Extension tab: NIP-07 browser extension (desktop)
- useLoginActions: updated to match Ditto's implementation with
  nostrconnect(), generateNostrConnectParams, generateNostrConnectURI
- Clean mobile-first UI — no clipboard hacks, no intent tricks

Co-authored-by: shakespeare.diy <assistant@shakespeare.diy>
This commit is contained in:
shakespeare.diy 2026-06-18 23:49:55 +07:00
parent e8bc072787
commit 916f067238
2 changed files with 263 additions and 420 deletions

View file

@ -1,18 +1,16 @@
// NOTE: This file is stable and usually should not be modified. // NOTE: This file is stable and usually should not be modified.
// It is important that all functionality in this file is preserved, and should only be modified if explicitly requested. // It is important that all functionality in this file is preserved, and should only be modified if explicitly requested.
import React, { useState, useEffect, useRef } from 'react'; import React, { useState, useEffect, useRef, useCallback } from 'react';
import { Shield, AlertTriangle, UserPlus, Sparkles, Cloud, Smartphone, Check, Loader2 } from 'lucide-react'; import { Shield, UserPlus, Sparkles, Cloud, Smartphone, Check, Loader2, Copy, RefreshCw } from 'lucide-react';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input'; // used for bunker URI input import { Input } from '@/components/ui/input';
import { Dialog, DialogContent, DialogHeader, DialogDescription } from "@/components/ui/dialog"; import { Dialog, DialogContent, DialogHeader, DialogDescription } from "@/components/ui/dialog";
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { Alert, AlertDescription } from '@/components/ui/alert'; import { useLoginActions, generateNostrConnectParams, generateNostrConnectURI, type NostrConnectParams, type NostrConnectStatus } from '@/hooks/useLoginActions';
import { useLoginActions } from '@/hooks/useLoginActions';
import { useToast } from '@/hooks/useToast'; import { useToast } from '@/hooks/useToast';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
interface LoginDialogProps { interface LoginDialogProps {
isOpen: boolean; isOpen: boolean;
onClose: () => void; onClose: () => void;
@ -20,472 +18,326 @@ interface LoginDialogProps {
onSignup?: () => void; onSignup?: () => void;
} }
const validateBunkerUri = (uri: string) => { const RELAYS = [
return uri.startsWith('bunker://'); 'wss://relay.ditto.pub',
}; 'wss://relay.nostr.band',
];
const LoginDialog: React.FC<LoginDialogProps> = ({ isOpen, onClose, onLogin, onSignup }) => { export const LoginDialog: React.FC<LoginDialogProps> = ({ isOpen, onClose, onLogin, onSignup }) => {
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [bunkerUri, setBunkerUri] = useState(''); const [bunkerUri, setBunkerUri] = useState('');
const [errors, setErrors] = useState<{ const [bunkerError, setBunkerError] = useState('');
bunker?: string;
extension?: string; // Amber nostrconnect state
amber?: string; const [amberStep, setAmberStep] = useState<'idle' | 'connecting' | 'success'>('idle');
}>({}); const [amberStatus, setAmberStatus] = useState('');
const [amberStep, setAmberStep] = useState<'initial' | 'waiting' | 'success'>('initial'); const [nostrConnectUri, setNostrConnectUri] = useState('');
const unsubAmberRef = useRef<(() => void) | null>(null); const [nostrConnectParams, setNostrConnectParams] = useState<NostrConnectParams | null>(null);
const pollRef = useRef<ReturnType<typeof setInterval> | null>(null); const abortRef = useRef<AbortController | null>(null);
const login = useLoginActions(); const login = useLoginActions();
const { toast } = useToast(); const { toast } = useToast();
// Reset all state when dialog opens/closes const isAndroid = /Android/i.test(navigator.userAgent);
// Reset state when dialog opens/closes
useEffect(() => { useEffect(() => {
if (isOpen) {
setIsLoading(false);
setBunkerUri('');
setErrors({});
setAmberStep('initial');
}
if (!isOpen) { if (!isOpen) {
// Clean up listeners when dialog closes abortRef.current?.abort();
if (unsubAmberRef.current) { unsubAmberRef.current(); unsubAmberRef.current = null; } abortRef.current = null;
if (pollRef.current) { clearInterval(pollRef.current); pollRef.current = null; } setAmberStep('idle');
setAmberStatus('');
setNostrConnectUri('');
setNostrConnectParams(null);
setBunkerUri('');
setBunkerError('');
setIsLoading(false);
} }
}, [isOpen]); }, [isOpen]);
// Clean up on unmount // Generate a fresh nostrconnect:// URI and start waiting
useEffect(() => { const startAmberConnect = useCallback(async () => {
return () => { abortRef.current?.abort();
if (unsubAmberRef.current) unsubAmberRef.current(); const controller = new AbortController();
if (pollRef.current) clearInterval(pollRef.current); abortRef.current = controller;
};
}, []);
const handleExtensionLogin = async () => { const params = generateNostrConnectParams({ relays: RELAYS });
setIsLoading(true); const uri = generateNostrConnectURI(params, {
setErrors(prev => ({ ...prev, extension: undefined })); name: 'SkateSpots',
url: 'https://skate-spots.org',
perms: 'sign_event,get_public_key,nip44_encrypt,nip44_decrypt',
});
setNostrConnectParams(params);
setNostrConnectUri(uri);
setAmberStep('connecting');
setAmberStatus('Waiting for Amber to connect...');
// Open Amber with the nostrconnect:// URI
// This works in browser, PWA, and Capacitor — it's a standard URI scheme
// that Amber registers as an intent filter
window.location.href = uri;
try { try {
if (!('nostr' in window)) { await login.nostrconnect(
throw new Error('Nostr extension not found. Please install a NIP-07 extension.'); params,
} controller.signal,
(status: NostrConnectStatus) => {
if (status === 'pending') setAmberStatus('Waiting for Amber...');
else if (status === 'connected') setAmberStatus('Connected! Getting your pubkey...');
else if (status === 'authenticated') setAmberStatus('Authenticated!');
},
);
setAmberStep('success');
toast({ title: '🛹 Logged in!', description: 'Connected via Amber!' });
setTimeout(() => { onLogin(); onClose(); }, 800);
} catch (e: unknown) {
if ((e as Error)?.name === 'AbortError') return;
setAmberStep('idle');
setAmberStatus('');
toast({ title: 'Connection failed', description: 'Amber did not connect. Try again.', variant: 'destructive' });
}
}, [login, onLogin, onClose, toast]);
const handleBunkerLogin = async () => {
const uri = bunkerUri.trim();
if (!uri.startsWith('bunker://')) {
setBunkerError('Must start with bunker://');
return;
}
setIsLoading(true);
setBunkerError('');
try {
await login.bunker(uri);
toast({ title: '🛹 Logged in!', description: 'Connected via bunker!' });
onLogin();
onClose();
} catch {
setBunkerError('Connection failed. Check your bunker URI and try again.');
} finally {
setIsLoading(false);
}
};
const handleExtensionLogin = async () => {
try {
await login.extension(); await login.extension();
onLogin(); onLogin();
onClose(); onClose();
} catch (e: unknown) {
const error = e as Error;
console.error('Bunker login failed:', error);
console.error('Nsec login failed:', error);
console.error('Extension login failed:', error);
setErrors(prev => ({
...prev,
extension: error instanceof Error ? error.message : 'Extension login failed'
}));
} finally {
setIsLoading(false);
}
};
const handleAmberLogin = () => {
// Show the bunker URI input — the user copies their bunker:// URI from Amber
setAmberStep('waiting');
setErrors(prev => ({ ...prev, amber: undefined }));
};
const handleBunkerLogin = async () => {
if (!bunkerUri.trim()) {
setErrors(prev => ({ ...prev, bunker: 'Please enter a bunker URI' }));
return;
}
if (!validateBunkerUri(bunkerUri)) {
setErrors(prev => ({ ...prev, bunker: 'Invalid bunker URI format. Must start with bunker://' }));
return;
}
setIsLoading(true);
setErrors(prev => ({ ...prev, bunker: undefined }));
try {
await login.bunker(bunkerUri);
onLogin();
onClose();
// Clear the URI from memory
setBunkerUri('');
} catch { } catch {
setErrors(prev => ({ toast({ title: 'No extension found', description: 'Install a Nostr browser extension like Alby.', variant: 'destructive' });
...prev,
bunker: 'Failed to connect to bunker. Please check the URI.'
}));
} finally {
setIsLoading(false);
} }
}; };
const handleSignupClick = () => { const copyUri = () => {
onClose(); navigator.clipboard.writeText(nostrConnectUri).catch(() => {});
if (onSignup) { toast({ title: 'Copied!', description: 'Paste in Amber → Connect apps → Add app' });
onSignup();
}
}; };
// Detect device type for better UX
const isAndroid = /Android/i.test(navigator.userAgent);
const isIOS = /iPhone|iPad|iPod/i.test(navigator.userAgent);
const isMobile = isAndroid || isIOS;
const hasExtension = typeof window !== 'undefined' && 'nostr' in window;
// Detect best default tab based on device and browser capabilities
const getDefaultTab = () => {
// Android - Amber is the best option
if (isAndroid) return 'amber';
// iOS - Bunker is recommended (no Amber support)
if (isIOS) return 'bunker';
// Desktop with extension - use extension
if (hasExtension) return 'extension';
// Desktop without extension - show extension tab with install prompt
return 'extension';
};
const defaultTab = getDefaultTab();
return ( return (
<Dialog open={isOpen} onOpenChange={onClose}> <Dialog open={isOpen} onOpenChange={onClose}>
<DialogContent <DialogContent className={cn("w-[calc(100%-1rem)] sm:max-w-md max-h-[90vh] max-h-[90dvh] p-0 overflow-hidden rounded-2xl bg-black border-2 border-[#ff0040]/50")}>
className={cn("w-[calc(100%-1rem)] sm:max-w-md max-h-[90vh] max-h-[90dvh] p-0 overflow-hidden rounded-2xl bg-black border-2 border-[#ff0040]/50")} <DialogHeader className={cn('px-4 sm:px-6 pt-4 sm:pt-6 pb-1')}>
> <DialogDescription className="text-center text-sm text-gray-400">
<DialogHeader className={cn('px-4 sm:px-6 pt-4 sm:pt-6 pb-1 relative')}> Sign up or log in to continue
</DialogDescription>
<DialogDescription className="text-center text-sm">
Sign up or log in to continue
</DialogDescription>
</DialogHeader> </DialogHeader>
<div className='px-4 sm:px-6 pt-2 pb-4 space-y-4 overflow-y-auto flex-1'>
{/* Prominent Sign Up Section */}
<div className='relative p-6 rounded-2xl bg-black border-2 border-[#ccff00]/50 overflow-hidden'>
{/* Neon glow effect */}
<div className="absolute inset-0 bg-[#ccff00]/5 blur-xl"></div>
<div className='px-4 sm:px-6 pt-2 pb-6 space-y-4 overflow-y-auto'>
{/* Sign Up */}
<div className='relative p-5 rounded-2xl bg-black border-2 border-[#ccff00]/50 overflow-hidden'>
<div className="absolute inset-0 bg-[#ccff00]/5 blur-xl" />
<div className='relative z-10 text-center space-y-3'> <div className='relative z-10 text-center space-y-3'>
<div className='flex justify-center items-center gap-2 mb-2'> <div className='flex justify-center items-center gap-2'>
<Sparkles className='w-5 h-5 text-[#ccff00]' /> <Sparkles className='w-5 h-5 text-[#ccff00]' />
<span className='font-bold text-[#ccff00] uppercase tracking-wide' style={{ <span className='font-bold text-[#ccff00] uppercase tracking-wide'>New here?</span>
textShadow: '0 0 10px #ccff00'
}}>
New here?
</span>
</div> </div>
<p className='text-sm text-gray-300'> <p className='text-xs text-gray-300'>Create a free account. No email required.</p>
Create a free account in seconds. No email required.
</p>
<Button <Button
onClick={handleSignupClick} onClick={() => { onClose(); onSignup?.(); }}
className='w-full rounded-full py-4 text-base font-bold bg-[#ccff00] text-black hover:bg-[#ccff00]/90 transform transition-all duration-200 hover:scale-105 shadow-[0_0_20px_rgba(204,255,0,0.5)] border-0 uppercase tracking-wide' className='w-full rounded-full py-3 font-bold bg-[#ccff00] text-black hover:bg-[#ccff00]/90 uppercase'
> >
<UserPlus className='w-4 h-4 mr-2' /> <UserPlus className='w-4 h-4 mr-2' />
<span>Create Account</span> Create Account
</Button> </Button>
</div> </div>
</div> </div>
{/* Divider */} {/* Divider */}
<div className='relative'> <div className='relative flex items-center'>
<div className='absolute inset-0 flex items-center'> <div className='flex-1 border-t border-gray-800' />
<div className='w-full border-t border-gray-300 dark:border-gray-600'></div> <span className='px-3 text-xs text-gray-500'>Or log in</span>
</div> <div className='flex-1 border-t border-gray-800' />
<div className='relative flex justify-center text-sm'>
<span className='px-3 bg-background text-muted-foreground'>
<span>Or log in</span>
</span>
</div>
</div> </div>
{/* Login Methods */} {/* Login tabs — mobile: Amber + Bunker only */}
<Tabs defaultValue={defaultTab} className="w-full"> <Tabs defaultValue={isAndroid ? 'amber' : 'bunker'} className="w-full">
<TabsList className="grid w-full grid-cols-3 bg-black/50 border border-[#ff0040]/30 rounded-lg mb-4 h-auto p-1"> <TabsList className="grid w-full grid-cols-3 bg-black/50 border border-[#ff0040]/30 rounded-lg mb-4 h-auto p-1">
<TabsTrigger value="extension" className="flex flex-col sm:flex-row items-center gap-0.5 sm:gap-1 text-[10px] sm:text-sm py-1.5 sm:py-2 px-1 sm:px-3"> <TabsTrigger value="amber" className="flex items-center gap-1 text-xs py-2 px-2">
<Shield className="w-4 h-4" />
<span className="hidden sm:inline">Extension</span>
<span className="sm:hidden">Ext</span>
</TabsTrigger>
<TabsTrigger value="amber" className="flex flex-col sm:flex-row items-center gap-0.5 sm:gap-1 text-[10px] sm:text-sm py-1.5 sm:py-2 px-1 sm:px-3">
<Smartphone className="w-4 h-4" /> <Smartphone className="w-4 h-4" />
<span>Amber</span> <span>Amber</span>
</TabsTrigger> </TabsTrigger>
<TabsTrigger value="bunker" className="flex flex-col sm:flex-row items-center gap-0.5 sm:gap-1 text-[10px] sm:text-sm py-1.5 sm:py-2 px-1 sm:px-3"> <TabsTrigger value="bunker" className="flex items-center gap-1 text-xs py-2 px-2">
<Cloud className="w-4 h-4" /> <Cloud className="w-4 h-4" />
<span className="hidden sm:inline">Bunker</span> <span>Bunker</span>
<span className="sm:hidden">Bnk</span> </TabsTrigger>
<TabsTrigger value="extension" className="flex items-center gap-1 text-xs py-2 px-2">
<Shield className="w-4 h-4" />
<span>Ext</span>
</TabsTrigger> </TabsTrigger>
</TabsList> </TabsList>
<TabsContent value='extension' className='space-y-3'>
{errors.extension && (
<Alert variant="destructive" className="bg-red-950/50 border-red-500/50">
<AlertTriangle className="h-4 w-4" />
<AlertDescription className="text-sm">
<strong>No extension detected.</strong> Try using the <strong>Create Account</strong> button above{isAndroid ? ', or use Amber on Android' : isIOS ? ', or use Bunker for iOS' : ''}.
</AlertDescription>
</Alert>
)}
<div className='text-center p-6 rounded-lg bg-black/50 border-2 border-[#ff0040]/30'>
<Shield className='w-12 h-12 mx-auto mb-3 text-[#ff0040]' />
<p className='text-sm text-gray-300 mb-2 font-bold'>
{isMobile ? 'Browser Extension' : 'One-Click Login (Desktop)'}
</p>
<p className='text-xs text-gray-400 mb-4'>
{isMobile
? 'Works with mobile browsers that have Nostr extensions (e.g., Kiwi Browser + Alby).'
: 'Best for desktop browsers with Alby, nos2x, or similar extensions installed.'
}
</p>
<div className="flex justify-center">
<Button
className='w-full rounded-full py-4 bg-[#ff0040] hover:bg-[#ff0040]/90 border-2 border-[#ff0040] shadow-[0_0_20px_rgba(255,0,64,0.5)] font-bold uppercase'
onClick={handleExtensionLogin}
disabled={isLoading}
>
{isLoading ? 'Logging in...' : 'Login with Extension'}
</Button>
</div>
</div>
</TabsContent>
{/* ── AMBER TAB ── */}
<TabsContent value='amber' className='space-y-3'> <TabsContent value='amber' className='space-y-3'>
{isIOS && ( {amberStep === 'idle' && (
<Alert className="bg-yellow-950/50 border-yellow-500/50"> <div className='text-center p-5 rounded-xl bg-black/50 border-2 border-[#ccff00]/30 space-y-4'>
<AlertTriangle className="h-4 w-4 text-yellow-500" /> <div className="w-14 h-14 rounded-full bg-[#ccff00]/10 border-2 border-[#ccff00]/40 flex items-center justify-center mx-auto">
<AlertDescription className="text-sm text-yellow-200"> <Smartphone className='w-7 h-7 text-[#ccff00]' />
Amber is Android-only. For iOS, use <strong>Bunker</strong> (nsec.app or Nostrum).
</AlertDescription>
</Alert>
)}
{/* Initial state — single tap */}
{amberStep === 'initial' && (
<div className='text-center p-6 rounded-lg bg-black/50 border-2 border-[#ccff00]/30 space-y-4'>
<div className="w-16 h-16 rounded-full bg-[#ccff00]/10 border-2 border-[#ccff00]/40 flex items-center justify-center mx-auto">
<Smartphone className='w-8 h-8 text-[#ccff00]' />
</div> </div>
<div> <div>
<p className='text-sm text-white font-bold mb-1'> <p className='text-sm text-white font-bold'>Login with Amber</p>
Login with Amber <p className='text-xs text-gray-400 mt-1'>
</p> Tap below — Amber opens, approve once, you're in.
<p className='text-xs text-gray-400'>
Uses NIP-46 — your keys stay in Amber, we connect via relay. Tap below to get started.
</p> </p>
</div> </div>
<Button <Button
className='w-full rounded-full py-4 bg-[#ccff00] text-black hover:bg-[#ccff00]/90 border-2 border-[#ccff00] shadow-[0_0_20px_rgba(204,255,0,0.5)] font-bold uppercase text-base h-auto' className='w-full rounded-full py-3 bg-[#ccff00] text-black hover:bg-[#ccff00]/90 font-bold uppercase'
onClick={handleAmberLogin} onClick={startAmberConnect}
disabled={isLoading || isIOS}
> >
<Smartphone className="w-4 h-4 mr-2" /> <Smartphone className="w-4 h-4 mr-2" />
{isIOS ? 'Android only' : 'Connect Amber'} Open Amber
</Button> </Button>
<p className='text-xs text-gray-500'> <p className='text-xs text-gray-500'>
Don't have Amber?{' '} Need Amber?{' '}
<a <a href="https://zapstore.dev/apps/naddr1qvzqqqr7pvpzqateqake4lc2fn77lflzq30jfpk8uhvtccalc66989er8cdmljceqqdkxmmd9enhyet9deshyaphvvejumn0wd68yumfvahx2usx8zmj2" target="_blank" rel="noopener noreferrer" className="text-[#ccff00] hover:underline">Get it on Zapstore</a>
href="https://github.com/greenart7c3/Amber/releases"
target="_blank"
rel="noopener noreferrer"
className="text-[#ccff00] hover:underline"
>
Download on GitHub
</a>
{' '}or{' '}
<a
href="https://f-droid.org/packages/com.greenart7c3.nostrsigner/"
target="_blank"
rel="noopener noreferrer"
className="text-[#ccff00] hover:underline"
>
F-Droid
</a>
</p> </p>
</div> </div>
)} )}
{/* Waiting — show bunker URI instructions */} {amberStep === 'connecting' && (
{amberStep === 'waiting' && ( <div className='p-5 rounded-xl bg-black/50 border-2 border-[#ccff00]/40 space-y-4'>
<div className='p-4 rounded-lg bg-black/50 border-2 border-[#ccff00]/40 space-y-4'> <div className='text-center space-y-2'>
<div className='text-center'> <Loader2 className='w-8 h-8 text-[#ccff00] animate-spin mx-auto' />
<p className='text-sm text-white font-bold mb-1'>Connect with Amber (NIP-46)</p> <p className='text-sm text-white font-bold'>Waiting for Amber...</p>
<p className='text-xs text-gray-400'> <p className='text-xs text-gray-400'>{amberStatus}</p>
Get your Bunker URI from Amber and paste it below.
</p>
</div> </div>
{/* Step by step */} <div className='text-xs text-gray-400 bg-black/40 rounded-lg p-3 space-y-1'>
<div className='space-y-2 text-xs text-gray-300'> <p className='font-bold text-white'>If Amber didn't open automatically:</p>
<div className='flex gap-2'> <p>1. Open <strong className='text-white'>Amber</strong></p>
<span className='text-[#ccff00] font-bold min-w-[18px]'>1.</span> <p>2. Tap <strong className='text-white'>Connect apps → Paste link</strong></p>
<span>Open <strong className='text-white'>Amber</strong> app</span> <p>3. Paste the connection string below</p>
</div>
<div className='flex gap-2'>
<span className='text-[#ccff00] font-bold min-w-[18px]'>2.</span>
<span>Tap the key icon → <strong className='text-white'>Connect apps</strong> → <strong className='text-white'>Add app</strong></span>
</div>
<div className='flex gap-2'>
<span className='text-[#ccff00] font-bold min-w-[18px]'>3.</span>
<span>Copy the <strong className='text-white'>bunker://</strong> connection string</span>
</div>
<div className='flex gap-2'>
<span className='text-[#ccff00] font-bold min-w-[18px]'>4.</span>
<span>Paste it below and tap <strong className='text-white'>Connect</strong></span>
</div>
</div> </div>
<Input {nostrConnectUri && (
placeholder='bunker://...' <Button
value={bunkerUri} variant="outline"
onChange={e => { className='w-full rounded-full border-[#ccff00]/30 text-[#ccff00] hover:bg-[#ccff00]/10 text-xs h-auto py-2'
setBunkerUri(e.target.value); onClick={copyUri}
setErrors(prev => ({ ...prev, amber: undefined })); >
}} <Copy className='w-3 h-3 mr-2' />
className='bg-black/60 border-[#ccff00]/30 text-white placeholder:text-gray-600 text-xs' Copy connection string
autoComplete='off' </Button>
autoCorrect='off'
spellCheck={false}
/>
{errors.amber && (
<p className="text-xs text-red-400">{errors.amber}</p>
)} )}
<Button <div className='flex gap-2'>
className='w-full rounded-full py-3 bg-[#ccff00] text-black hover:bg-[#ccff00]/90 font-bold uppercase text-sm h-auto' <Button
disabled={isLoading || !bunkerUri.trim().startsWith('bunker://')} variant="ghost"
onClick={async () => { className='flex-1 text-xs text-gray-500 hover:text-gray-300 h-auto py-2'
if (!bunkerUri.trim().startsWith('bunker://')) { onClick={() => { abortRef.current?.abort(); setAmberStep('idle'); setAmberStatus(''); }}
setErrors(prev => ({ ...prev, amber: 'Must start with bunker://' })); >
return; Cancel
} </Button>
setIsLoading(true); <Button
setErrors(prev => ({ ...prev, amber: undefined })); variant="ghost"
try { className='flex-1 text-xs text-[#ccff00] hover:text-[#ccff00]/80 h-auto py-2'
await login.bunker(bunkerUri.trim()); onClick={startAmberConnect}
setAmberStep('success'); >
toast({ title: '🛹 Logged in!', description: 'Connected via Amber!' }); <RefreshCw className='w-3 h-3 mr-1' />
setTimeout(() => { onLogin(); onClose(); }, 600); Retry
} catch { </Button>
setErrors(prev => ({ ...prev, amber: 'Connection failed. Check your bunker URI and try again.' })); </div>
} finally {
setIsLoading(false);
}
}}
>
{isLoading ? <Loader2 className='w-4 h-4 animate-spin mr-2' /> : null}
Connect
</Button>
<Button
variant="ghost"
className="w-full text-xs text-gray-600 hover:text-gray-400 h-auto py-1"
onClick={() => {
setAmberStep('initial');
setBunkerUri('');
setErrors(prev => ({ ...prev, amber: undefined }));
}}
>
← Back
</Button>
</div> </div>
)} )}
{/* Success */}
{amberStep === 'success' && ( {amberStep === 'success' && (
<div className='text-center p-6 rounded-lg bg-black/50 border-2 border-green-500/30 space-y-3'> <div className='text-center p-5 rounded-xl bg-black/50 border-2 border-green-500/30 space-y-3'>
<Check className='w-12 h-12 mx-auto text-green-500' /> <Check className='w-10 h-10 mx-auto text-green-500' />
<p className='text-sm text-green-400 font-bold'>Logged in!</p> <p className='text-sm text-green-400 font-bold'>Logged in!</p>
<Loader2 className="w-5 h-5 mx-auto animate-spin text-green-500" /> <Loader2 className="w-4 h-4 mx-auto animate-spin text-green-500" />
</div> </div>
)} )}
</TabsContent> </TabsContent>
<TabsContent value='bunker' className='space-y-4'> {/* ── BUNKER TAB ── */}
{isIOS && ( <TabsContent value='bunker' className='space-y-3'>
<Alert className="bg-blue-950/50 border-blue-500/50"> <div className='p-5 rounded-xl bg-black/50 border-2 border-blue-500/30 space-y-4'>
<Cloud className="h-4 w-4 text-blue-400" /> <div className='text-center'>
<AlertDescription className="text-sm text-blue-200"> <Cloud className='w-10 h-10 mx-auto mb-2 text-blue-400' />
<strong>Recommended for iOS!</strong> Use nsec.app for the best experience on iPhone/iPad. <p className='text-sm text-white font-bold'>Login with Bunker (NIP-46)</p>
</AlertDescription> <p className='text-xs text-gray-400 mt-1'>
</Alert> Paste your <code className='text-blue-300'>bunker://</code> connection string from Amber or nsec.app.
)} </p>
<div className='text-center p-6 rounded-lg bg-black/50 border-2 border-blue-500/30'> </div>
<Cloud className='w-12 h-12 mx-auto mb-3 text-blue-400' />
<p className='text-sm text-gray-300 mb-2 font-bold'> <div className='text-xs text-gray-400 bg-black/40 rounded-lg p-3 space-y-1'>
Login with Nostr Connect {isIOS && '(Best for iOS)'} <p className='font-bold text-white'>From Amber:</p>
</p> <p>Settings → NIP-46 Bunker → Create connection → Copy URI</p>
<p className='text-xs text-gray-400 mb-4'> </div>
{isIOS
? 'Works on any device! Get a bunker:// connection string from nsec.app and paste it below.'
: 'Access your account from any device using a remote signer (bunker). Your keys stay safe on the server.'
}
</p>
<div className='space-y-2'> <div className='space-y-2'>
<label htmlFor='bunkerUri' className='text-sm font-medium block text-left'>
Bunker Connection String
</label>
<Input <Input
id='bunkerUri'
value={bunkerUri}
onChange={(e) => {
setBunkerUri(e.target.value);
if (errors.bunker) setErrors(prev => ({ ...prev, bunker: undefined }));
}}
className={`rounded-lg ${
errors.bunker ? 'border-red-500' : ''
}`}
placeholder='bunker://...' placeholder='bunker://...'
autoComplete="off" value={bunkerUri}
onChange={e => { setBunkerUri(e.target.value); setBunkerError(''); }}
className='bg-black/60 border-blue-500/30 text-white placeholder:text-gray-600 text-xs'
autoComplete='off'
autoCorrect='off'
spellCheck={false}
/> />
{errors.bunker && ( {bunkerError && <p className="text-xs text-red-400">{bunkerError}</p>}
<p className="text-sm text-red-500">{errors.bunker}</p>
)}
</div> </div>
<div className="flex justify-center mt-4"> <Button
<Button className='w-full rounded-full py-3 bg-blue-500 hover:bg-blue-600 text-white font-bold uppercase'
className='w-full rounded-full py-4 bg-blue-500 hover:bg-blue-600 border-2 border-blue-500 shadow-[0_0_20px_rgba(59,130,246,0.5)] font-bold uppercase' disabled={isLoading || !bunkerUri.trim()}
onClick={handleBunkerLogin} onClick={handleBunkerLogin}
disabled={isLoading || !bunkerUri.trim()} >
> {isLoading ? <Loader2 className='w-4 h-4 animate-spin mr-2' /> : <Cloud className='w-4 h-4 mr-2' />}
{isLoading ? 'Connecting...' : 'Connect to Bunker'} Connect
</Button> </Button>
</div>
<div className='mt-4 pt-4 border-t border-gray-700'> <div className='text-center pt-1'>
<p className='text-xs text-gray-500 mb-2'>Get a bunker connection from:</p> <p className='text-xs text-gray-500 mb-2'>Or get a bunker from:</p>
<div className="flex flex-wrap gap-2 justify-center"> <div className='flex gap-2 justify-center flex-wrap'>
<a <a href="https://nsec.app" target="_blank" rel="noopener noreferrer" className="text-xs px-3 py-1 bg-blue-500/20 text-blue-400 rounded-full hover:bg-blue-500/30">nsec.app →</a>
href="https://nsec.app" <a href="https://nostrum.social" target="_blank" rel="noopener noreferrer" className="text-xs px-3 py-1 bg-blue-500/20 text-blue-400 rounded-full hover:bg-blue-500/30">Nostrum →</a>
target="_blank"
rel="noopener noreferrer"
className="text-xs px-3 py-1 bg-blue-500/20 text-blue-400 rounded-full hover:bg-blue-500/30 transition-colors"
>
nsec.app →
</a>
<a
href="https://nostrum.social"
target="_blank"
rel="noopener noreferrer"
className="text-xs px-3 py-1 bg-blue-500/20 text-blue-400 rounded-full hover:bg-blue-500/30 transition-colors"
>
Nostrum →
</a>
</div> </div>
</div> </div>
</div> </div>
</TabsContent> </TabsContent>
{/* ── EXTENSION TAB ── */}
<TabsContent value='extension' className='space-y-3'>
<div className='text-center p-5 rounded-xl bg-black/50 border-2 border-[#ff0040]/30 space-y-4'>
<Shield className='w-10 h-10 mx-auto text-[#ff0040]' />
<div>
<p className='text-sm text-white font-bold'>Browser Extension</p>
<p className='text-xs text-gray-400 mt-1'>Best for desktop. Works with Alby, nos2x, and similar NIP-07 extensions.</p>
</div>
<Button
className='w-full rounded-full py-3 bg-[#ff0040] hover:bg-[#ff0040]/90 text-white font-bold uppercase'
onClick={handleExtensionLogin}
>
Login with Extension
</Button>
</div>
</TabsContent>
</Tabs> </Tabs>
</div> </div>
</DialogContent> </DialogContent>
</Dialog> </Dialog>
); );
}; };
export default LoginDialog; export default LoginDialog;

View file

@ -1,69 +1,60 @@
import { useNostr } from '@nostrify/react'; import { useNostr } from '@nostrify/react';
import { NLogin, useNostrLogin } from '@nostrify/react/login'; import {
import { setAmberLogin, clearAmberLogin, openAmberForLogin } from '@/lib/amber'; NLogin,
type NLoginType,
type NostrConnectParams,
type NostrConnectStatus,
useNostrLogin,
generateNostrConnectParams,
generateNostrConnectURI,
} from '@nostrify/react/login';
// NOTE: This file should not be edited except for adding new login methods. // NOTE: This file should not be edited except for adding new login methods.
/** Custom login type for Amber (NIP-55) */ export type { NostrConnectParams, NostrConnectStatus };
export interface AmberLoginType { export { generateNostrConnectParams, generateNostrConnectURI };
id: string;
type: 'amber';
pubkey: string;
createdAt: number;
}
export function useLoginActions() { export function useLoginActions() {
const { nostr } = useNostr(); const { nostr } = useNostr();
const { logins, addLogin, removeLogin } = useNostrLogin(); const { logins, addLogin, setLogin, removeLogin } = useNostrLogin();
const addAndActivate = (login: NLoginType) => {
addLogin(login);
setLogin(login.id);
};
return { return {
// Login with a Nostr secret key // Login with a Nostr secret key
nsec(nsec: string): void { nsec(nsec: string): void {
const login = NLogin.fromNsec(nsec); const login = NLogin.fromNsec(nsec);
addLogin(login); addAndActivate(login);
}, },
// Login with a NIP-46 "bunker://" URI // Login with a NIP-46 "bunker://" URI
async bunker(uri: string): Promise<void> { async bunker(uri: string): Promise<void> {
const login = await NLogin.fromBunker(uri, nostr); const login = await NLogin.fromBunker(uri, nostr);
addLogin(login); addAndActivate(login);
}, },
// Login with a NIP-07 browser extension // Login with a NIP-07 browser extension
async extension(): Promise<void> { async extension(): Promise<void> {
const login = await NLogin.fromExtension(); const login = await NLogin.fromExtension();
addLogin(login); addAndActivate(login);
}, },
// Login with Amber (NIP-55) - opens Amber app // Login via nostrconnect:// (client-initiated NIP-46)
openAmber(): void { // Generates a URI, opens Amber, waits for Amber to connect back via relay.
openAmberForLogin(); async nostrconnect(
}, params: NostrConnectParams,
// Complete Amber login after callback signal?: AbortSignal,
amber(pubkey: string): void { onStatus?: (status: NostrConnectStatus) => void,
// Store in localStorage for persistence ): Promise<void> {
setAmberLogin(pubkey); const login = await NLogin.fromNostrConnect(params, nostr, { signal, onStatus });
addAndActivate(login);
// Create a custom login entry
const login: AmberLoginType = {
id: `amber_${pubkey}`,
type: 'amber',
pubkey,
createdAt: Date.now(),
};
// Add to the login system (cast to any since it's a custom type)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
addLogin(login as any);
}, },
// Log out the current user // Log out the current user
async logout(): Promise<void> { async logout(): Promise<void> {
const login = logins[0]; const login = logins[0];
if (login) { if (login) {
// Clear Amber login from localStorage if it was an Amber login
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((login as any).type === 'amber') {
clearAmberLogin();
}
removeLogin(login.id); removeLogin(login.id);
} }
} },
}; };
} }