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..2a102f5b 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'; @@ -70,13 +70,6 @@ export function LoginArea({ className }: LoginAreaProps) { {t('help')} - setLoginDialogOpen(true)} - className='flex items-center gap-2 cursor-pointer p-2 rounded-md' - > - - {t('logIn')} - setSignupDialogOpen(true)} className='flex items-center gap-2 cursor-pointer p-2 rounded-md' @@ -84,20 +77,31 @@ export function LoginArea({ className }: LoginAreaProps) { {t('signUp')} + setLoginDialogOpen(true)} + className='flex items-center gap-2 cursor-pointer p-2 rounded-md' + > + + {t('logIn')} + )} - setLoginDialogOpen(false)} onLogin={handleLogin} onSignup={() => setSignupDialogOpen(true)} /> - setSignupDialogOpen(false)} + onLogin={() => { + setSignupDialogOpen(false); + setLoginDialogOpen(true); + }} /> ); diff --git a/src/components/auth/SimpleLoginDialog.tsx b/src/components/auth/SimpleLoginDialog.tsx new file mode 100644 index 00000000..b43136a7 --- /dev/null +++ b/src/components/auth/SimpleLoginDialog.tsx @@ -0,0 +1,285 @@ +import React, { useState, useEffect } from 'react'; +import { Cloud } from 'lucide-react'; +import { Button } from '@/components/ui/button'; +import { Input } from '@/components/ui/input'; +import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog"; +// Alert components removed as they're not used in the simplified design +import { useLoginActions } from '@/hooks/useLoginActions'; +import { cn } from '@/lib/utils'; + +// Only using key-add step now + +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 +}) => { + // Removed step state - we only have one step now + 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) { + 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(); + } + }; + + return ( + + + + Log in + + +
+
+

Welcome back to Shakespeare

+

Sign in to your existing account

+
+ +
+
+ 🔑 +
+
+ +
+ {/* 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 - only show if extension is detected */} + {window.nostr && ( +
+

+ +

+
+ )} + +
+ New to Shakespeare? + +
+
+
+
+
+ ); +}; + +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..57170dc5 --- /dev/null +++ b/src/components/auth/SimpleSignupDialog.tsx @@ -0,0 +1,263 @@ +import React, { useState, useEffect, useMemo } from 'react'; +import { Download, UserPlus } 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 = 'key' | 'keygen'; + +interface SimpleSignupDialogProps { + isOpen: boolean; + onClose: () => void; + onComplete?: () => void; + onLogin?: () => void; +} + +const SimpleSignupDialog: React.FC = ({ + isOpen, + onClose, + onComplete, + onLogin +}) => { + 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) { + // Always start with key generation as the default experience + setStep('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(); + if (onLogin) { + onLogin(); + } + }; + + if (step === 'key') { + return ( + + + + Sign up + + +
+
+

Welcome to Shakespeare

+

Create your account to get started

+
+ +
+
+ 🔑 +
+
+ +
+ + + +
+ + {/* Extension indicator - only show if extension is detected */} + {window.nostr && ( + <> +
+
+
+
+
+ or +
+
+ +
+

+ +

+
+ + )} +
+
+
+ ); + } + + 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..cf8112a9 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,10 +218,14 @@ export function NostrSettings() { /> {/* Signup Dialog */} - setShowSignupDialog(false)} onComplete={handleSignupComplete} + onLogin={() => { + setShowSignupDialog(false); + setShowLoginDialog(true); + }} /> );