diff --git a/src/components/auth/LoginArea.test.tsx b/src/components/auth/LoginArea.test.tsx index 62df7a0e..91e3a537 100644 --- a/src/components/auth/LoginArea.test.tsx +++ b/src/components/auth/LoginArea.test.tsx @@ -35,12 +35,12 @@ vi.mock('@/components/RelaySelector', () => ({ ) })); -vi.mock('./LoginDialog', () => ({ +vi.mock('./SimpleLoginDialog', () => ({ default: ({ isOpen }: { isOpen: boolean; onClose: () => void }) => isOpen ?
Login Dialog
: null })); -vi.mock('./SignupDialog', () => ({ +vi.mock('./SimpleSignupDialog', () => ({ default: ({ isOpen }: { isOpen: boolean; onClose: () => void }) => isOpen ?
Signup Dialog
: null })); diff --git a/src/components/auth/LoginArea.tsx b/src/components/auth/LoginArea.tsx index 67a10ff0..b7aacb53 100644 --- a/src/components/auth/LoginArea.tsx +++ b/src/components/auth/LoginArea.tsx @@ -12,8 +12,8 @@ import { DropdownMenuTrigger, } from '@/components/ui/dropdown-menu.tsx'; import { Avatar, AvatarFallback } from '@/components/ui/avatar.tsx'; -import LoginDialog from './LoginDialog'; -import SignupDialog from './SignupDialog'; +import SimpleLoginDialog from './SimpleLoginDialog'; +import SimpleSignupDialog from './SimpleSignupDialog'; import { useLoggedInAccounts } from '@/hooks/useLoggedInAccounts'; import { AccountSwitcher } from './AccountSwitcher'; import { cn } from '@/lib/utils'; @@ -88,14 +88,14 @@ export function LoginArea({ className }: LoginAreaProps) { )} - setLoginDialogOpen(false)} onLogin={handleLogin} onSignup={() => setSignupDialogOpen(true)} /> - setSignupDialogOpen(false)} /> diff --git a/src/components/auth/SimpleLoginDialog.tsx b/src/components/auth/SimpleLoginDialog.tsx new file mode 100644 index 00000000..bd5bc00a --- /dev/null +++ b/src/components/auth/SimpleLoginDialog.tsx @@ -0,0 +1,339 @@ +import React, { useState, useEffect } from 'react'; +import { Shield, Key, Cloud, AlertTriangle } from 'lucide-react'; +import { Button } from '@/components/ui/button'; +import { Input } from '@/components/ui/input'; +import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog"; +import { Alert, AlertDescription } from '@/components/ui/alert'; +import { useLoginActions } from '@/hooks/useLoginActions'; +import { cn } from '@/lib/utils'; + +type Step = 'extension' | 'key-add'; + +interface SimpleLoginDialogProps { + isOpen: boolean; + onClose: () => void; + onLogin: () => void; + onSignup?: () => void; +} + +const validateNsec = (nsec: string) => { + return /^nsec1[a-zA-Z0-9]{58}$/.test(nsec); +}; + +const validateBunkerUri = (uri: string) => { + return uri.startsWith('bunker://'); +}; + +const SimpleLoginDialog: React.FC = ({ + isOpen, + onClose, + onLogin, + onSignup +}) => { + const [step, setStep] = useState('extension'); + const [isLoading, setIsLoading] = useState(false); + const [nsec, setNsec] = useState(''); + const [bunkerUri, setBunkerUri] = useState(''); + const [activeTab, setActiveTab] = useState<'nsec' | 'bunker'>('nsec'); + const [errors, setErrors] = useState<{ + nsec?: string; + bunker?: string; + extension?: string; + }>({}); + + const login = useLoginActions(); + + // Reset state when dialog opens/closes + useEffect(() => { + if (isOpen) { + setStep(window.nostr ? 'extension' : 'key-add'); + setIsLoading(false); + setNsec(''); + setBunkerUri(''); + setActiveTab('nsec'); + setErrors({}); + } + }, [isOpen]); + + const handleExtensionLogin = async () => { + setIsLoading(true); + setErrors(prev => ({ ...prev, extension: undefined })); + + try { + if (!('nostr' in window)) { + throw new Error('Nostr extension not found. Please install a NIP-07 extension.'); + } + await login.extension(); + onLogin(); + onClose(); + } catch (e: unknown) { + const error = e as Error; + setErrors(prev => ({ + ...prev, + extension: error instanceof Error ? error.message : 'Extension login failed' + })); + } finally { + setIsLoading(false); + } + }; + + const handleKeyLogin = () => { + if (!nsec.trim()) { + setErrors(prev => ({ ...prev, nsec: 'Please enter your secret key' })); + return; + } + + if (!validateNsec(nsec)) { + setErrors(prev => ({ ...prev, nsec: 'Invalid secret key format. Must be a valid nsec starting with nsec1.' })); + return; + } + + setIsLoading(true); + setErrors({}); + + setTimeout(() => { + try { + login.nsec(nsec); + onLogin(); + onClose(); + } catch { + setErrors({ nsec: "Failed to login with this key. Please check that it's correct." }); + setIsLoading(false); + } + }, 50); + }; + + 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(); + setBunkerUri(''); + } catch { + setErrors(prev => ({ + ...prev, + bunker: 'Failed to connect to bunker. Please check the URI.' + })); + } finally { + setIsLoading(false); + } + }; + + const handleSignupClick = () => { + onClose(); + if (onSignup) { + onSignup(); + } + }; + + if (step === 'extension') { + return ( + + + + Log in + + +
+
+

Welcome to Shakespeare

+
+ +
+ +
+ + {errors.extension && ( + + + {errors.extension} + + )} + +
+ + +
+
+
+
+
+ or +
+
+ + + +
+ New on Shakespeare? + +
+
+
+
+
+ ); + } + + return ( + + + + Import Key + + +
+
+ +
+ +
+ {/* Tab selector */} +
+ + +
+ + {activeTab === 'nsec' && ( +
+
+ { + setNsec(e.target.value); + if (errors.nsec) setErrors(prev => ({ ...prev, nsec: undefined })); + }} + placeholder="nsec1..." + className={errors.nsec ? 'border-red-500' : ''} + /> + {errors.nsec && ( +

{errors.nsec}

+ )} +
+ + +
+ )} + + {activeTab === 'bunker' && ( +
+
+ { + setBunkerUri(e.target.value); + if (errors.bunker) setErrors(prev => ({ ...prev, bunker: undefined })); + }} + placeholder="bunker://" + className={errors.bunker ? 'border-red-500' : ''} + /> + {errors.bunker && ( +

{errors.bunker}

+ )} +
+ + +
+ )} + +
+
+
+
+
+ or +
+
+ + {/* Extension indicator */} +
+

+ {window.nostr ? ( + + ) : ( + 'Browser extension not found.' + )} +

+
+
+
+
+
+ ); +}; + +export default SimpleLoginDialog; \ No newline at end of file diff --git a/src/components/auth/SimpleSignupDialog.tsx b/src/components/auth/SimpleSignupDialog.tsx new file mode 100644 index 00000000..78d48f60 --- /dev/null +++ b/src/components/auth/SimpleSignupDialog.tsx @@ -0,0 +1,315 @@ +import React, { useState, useEffect, useMemo } from 'react'; +import { Key, Download, UserPlus, Shield } from 'lucide-react'; +import { Button } from '@/components/ui/button'; +import { Input } from '@/components/ui/input'; +import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog"; +import { toast } from '@/hooks/useToast'; +import { useLoginActions } from '@/hooks/useLoginActions'; +import { generateSecretKey, nip19 } from 'nostr-tools'; +import { cn } from '@/lib/utils'; + +type Step = 'extension' | 'key' | 'keygen'; + +interface SimpleSignupDialogProps { + isOpen: boolean; + onClose: () => void; + onComplete?: () => void; +} + +const SimpleSignupDialog: React.FC = ({ + isOpen, + onClose, + onComplete +}) => { + const [step, setStep] = useState('key'); + const [isLoading, setIsLoading] = useState(false); + const [downloaded, setDownloaded] = useState(false); + + const login = useLoginActions(); + + // Generate key once and keep it stable + const secretKey = useMemo(() => generateSecretKey(), []); + const nsec = useMemo(() => nip19.nsecEncode(secretKey), [secretKey]); + + // Reset state when dialog opens/closes + useEffect(() => { + if (isOpen) { + setStep(window.nostr ? 'extension' : 'key'); + setIsLoading(false); + setDownloaded(false); + } + }, [isOpen]); + + const handleExtensionSignup = async () => { + setIsLoading(true); + + try { + if (!('nostr' in window)) { + throw new Error('Nostr extension not found. Please install a NIP-07 extension.'); + } + await login.extension(); + onClose(); + if (onComplete) { + setTimeout(() => { + onComplete(); + }, 600); + } + } catch (e: unknown) { + const error = e as Error; + toast({ + title: 'Extension signup failed', + description: error instanceof Error ? error.message : 'Extension signup failed', + variant: 'destructive', + }); + } finally { + setIsLoading(false); + } + }; + + const handleDownload = () => { + try { + const blob = new Blob([nsec], { type: 'text/plain; charset=utf-8' }); + const url = globalThis.URL.createObjectURL(blob); + const a = document.createElement('a'); + a.href = url; + a.download = 'secret-key.txt'; + a.style.display = 'none'; + document.body.appendChild(a); + a.click(); + globalThis.URL.revokeObjectURL(url); + document.body.removeChild(a); + setDownloaded(true); + toast({ + title: 'Secret Key Saved!', + description: 'Your key has been safely stored.', + }); + } catch { + toast({ + title: 'Download failed', + description: 'Could not download the key file. Please copy it manually.', + variant: 'destructive', + }); + } + }; + + const handleNext = async () => { + setIsLoading(true); + try { + login.nsec(nsec); + + // Mark signup completion time for fallback welcome modal + localStorage.setItem('signup_completed', Date.now().toString()); + + onClose(); + if (onComplete) { + setTimeout(() => { + onComplete(); + }, 600); + } + } catch { + toast({ + title: 'Login Failed', + description: 'Failed to login with the generated key. Please try again.', + variant: 'destructive', + }); + } finally { + setIsLoading(false); + } + }; + + const handleLoginClick = () => { + onClose(); + // This would typically open the login modal + // Since we don't have direct access to the login modal state here, + // we'll rely on the parent component to handle this + }; + + if (step === 'extension') { + return ( + + + + Sign up + + +
+
+

Welcome to Shakespeare

+
+ +
+ +
+ +
+ + +
+
+
+
+
+ or +
+
+ + + +
+ New on Shakespeare? + +
+
+
+
+
+ ); + } + + if (step === 'key') { + return ( + + + + Sign up + + +
+
+

You need a key to continue

+
+ +
+ +
+ +
+ + + +
+ +
+
+
+
+
+ or +
+
+ + {/* Extension indicator */} +
+

+ {window.nostr ? ( + + ) : ( + 'Browser extension not found.' + )} +

+
+
+
+
+ ); + } + + return ( + + + + Your new key + + +
+
+ +
+ +
+
+ + +
+ +
+

+ Back up your secret key in a secure place. If lost, your account cannot be recovered. + Never share your secret key with anyone. +

+
+ +
+ + + +
+
+
+
+
+ ); +}; + +export default SimpleSignupDialog; \ No newline at end of file diff --git a/src/pages/NostrSettings.tsx b/src/pages/NostrSettings.tsx index 26b5aaef..9996b9c5 100644 --- a/src/pages/NostrSettings.tsx +++ b/src/pages/NostrSettings.tsx @@ -6,8 +6,8 @@ import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; import { Skeleton } from '@/components/ui/skeleton'; import { RelaySelector } from '@/components/RelaySelector'; -import LoginDialog from '@/components/auth/LoginDialog'; -import SignupDialog from '@/components/auth/SignupDialog'; +import SimpleLoginDialog from '@/components/auth/SimpleLoginDialog'; +import SimpleSignupDialog from '@/components/auth/SimpleSignupDialog'; import { useIsMobile } from '@/hooks/useIsMobile'; import { useNavigate } from 'react-router-dom'; import { useLoggedInAccounts, type Account } from '@/hooks/useLoggedInAccounts'; @@ -207,7 +207,7 @@ export function NostrSettings() { {/* Login Dialog */} - setShowLoginDialog(false)} onLogin={handleLoginSuccess} @@ -218,7 +218,7 @@ export function NostrSettings() { /> {/* Signup Dialog */} - setShowSignupDialog(false)} onComplete={handleSignupComplete}