Fix SignupDialog (so it logs the user in immediately after generating the key)

This commit is contained in:
Alex Gleason 2025-05-16 13:55:12 -05:00
parent 56df17b56e
commit ebc399776d
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7

View File

@ -12,6 +12,7 @@ import {
DialogTitle, DialogTitle,
} from '@/components/ui/dialog.tsx'; } from '@/components/ui/dialog.tsx';
import { toast } from '@/hooks/useToast.ts'; import { toast } from '@/hooks/useToast.ts';
import { useLoginActions } from '@/hooks/useLoginActions';
import { generateSecretKey, nip19 } from 'nostr-tools'; import { generateSecretKey, nip19 } from 'nostr-tools';
interface SignupDialogProps { interface SignupDialogProps {
@ -22,19 +23,19 @@ interface SignupDialogProps {
const SignupDialog: React.FC<SignupDialogProps> = ({ isOpen, onClose }) => { const SignupDialog: React.FC<SignupDialogProps> = ({ isOpen, onClose }) => {
const [step, setStep] = useState<'generate' | 'download' | 'done'>('generate'); const [step, setStep] = useState<'generate' | 'download' | 'done'>('generate');
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [nsecKey, setNsecKey] = useState(''); const [nsec, setNsec] = useState('');
const login = useLoginActions();
// Generate a proper nsec key using nostr-tools // Generate a proper nsec key using nostr-tools
const generateKey = () => { const generateKey = () => {
setIsLoading(true); setIsLoading(true);
try { try {
// Generate a new private key // Generate a new secret key
const privateKey = generateSecretKey(); const sk = generateSecretKey();
// Convert to nsec format // Convert to nsec format
const nsec = nip19.nsecEncode(privateKey); setNsec(nip19.nsecEncode(sk));
setNsecKey(nsec);
setStep('download'); setStep('download');
} catch (error) { } catch (error) {
console.error('Failed to generate key:', error); console.error('Failed to generate key:', error);
@ -50,7 +51,7 @@ const SignupDialog: React.FC<SignupDialogProps> = ({ isOpen, onClose }) => {
const downloadKey = () => { const downloadKey = () => {
// Create a blob with the key text // Create a blob with the key text
const blob = new Blob([nsecKey], { type: 'text/plain' }); const blob = new Blob([nsec], { type: 'text/plain' });
const url = globalThis.URL.createObjectURL(blob); const url = globalThis.URL.createObjectURL(blob);
// Create a temporary link element and trigger download // Create a temporary link element and trigger download
@ -71,6 +72,8 @@ const SignupDialog: React.FC<SignupDialogProps> = ({ isOpen, onClose }) => {
}; };
const finishSignup = () => { const finishSignup = () => {
login.nsec(nsec);
setStep('done'); setStep('done');
onClose(); onClose();
@ -118,7 +121,7 @@ const SignupDialog: React.FC<SignupDialogProps> = ({ isOpen, onClose }) => {
{step === 'download' && ( {step === 'download' && (
<div className='space-y-6'> <div className='space-y-6'>
<div className='p-4 rounded-lg border bg-gray-50 dark:bg-gray-800 overflow-auto'> <div className='p-4 rounded-lg border bg-gray-50 dark:bg-gray-800 overflow-auto'>
<code className='text-xs break-all'>{nsecKey}</code> <code className='text-xs break-all'>{nsec}</code>
</div> </div>
<div className='text-sm text-gray-600 dark:text-gray-300 space-y-2'> <div className='text-sm text-gray-600 dark:text-gray-300 space-y-2'>