mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-05 08:23:35 +00:00
Compare commits
5
Commits
main
...
signupDialog
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4d5df6535f | ||
|
|
4734e96fe7 | ||
|
|
e0ad32447a | ||
|
|
8aed77c49b | ||
|
|
4e780190cb |
No files matched your search
@@ -5,7 +5,7 @@ import { useState } from 'react';
|
|||||||
import { User } from 'lucide-react';
|
import { User } from 'lucide-react';
|
||||||
import { Button } from '@/components/ui/button.tsx';
|
import { Button } from '@/components/ui/button.tsx';
|
||||||
import LoginDialog from './LoginDialog';
|
import LoginDialog from './LoginDialog';
|
||||||
import SignupDialog from './SignupDialog';
|
|
||||||
import { useLoggedInAccounts } from '@/hooks/useLoggedInAccounts';
|
import { useLoggedInAccounts } from '@/hooks/useLoggedInAccounts';
|
||||||
import { AccountSwitcher } from './AccountSwitcher';
|
import { AccountSwitcher } from './AccountSwitcher';
|
||||||
import { cn } from '@/lib/utils';
|
import { cn } from '@/lib/utils';
|
||||||
@@ -17,11 +17,9 @@ export interface LoginAreaProps {
|
|||||||
export function LoginArea({ className }: LoginAreaProps) {
|
export function LoginArea({ className }: LoginAreaProps) {
|
||||||
const { currentUser } = useLoggedInAccounts();
|
const { currentUser } = useLoggedInAccounts();
|
||||||
const [loginDialogOpen, setLoginDialogOpen] = useState(false);
|
const [loginDialogOpen, setLoginDialogOpen] = useState(false);
|
||||||
const [signupDialogOpen, setSignupDialogOpen] = useState(false);
|
|
||||||
|
|
||||||
const handleLogin = () => {
|
const handleLogin = () => {
|
||||||
setLoginDialogOpen(false);
|
setLoginDialogOpen(false);
|
||||||
setSignupDialogOpen(false);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -39,16 +37,12 @@ export function LoginArea({ className }: LoginAreaProps) {
|
|||||||
)}
|
)}
|
||||||
|
|
||||||
<LoginDialog
|
<LoginDialog
|
||||||
isOpen={loginDialogOpen}
|
isOpen={loginDialogOpen}
|
||||||
onClose={() => setLoginDialogOpen(false)}
|
onClose={() => setLoginDialogOpen(false)}
|
||||||
onLogin={handleLogin}
|
onLogin={handleLogin}
|
||||||
onSignup={() => setSignupDialogOpen(true)}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<SignupDialog
|
|
||||||
isOpen={signupDialogOpen}
|
|
||||||
onClose={() => setSignupDialogOpen(false)}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
+108
-129
@@ -1,7 +1,4 @@
|
|||||||
// NOTE: This file is stable and usually should not be modified.
|
import React, { useRef, useState, useEffect } from 'react';
|
||||||
// It is important that all functionality in this file is preserved, and should only be modified if explicitly requested.
|
|
||||||
|
|
||||||
import React, { useRef, useState } from 'react';
|
|
||||||
import { Shield, Upload } from 'lucide-react';
|
import { Shield, Upload } from 'lucide-react';
|
||||||
import { Button } from '@/components/ui/button.tsx';
|
import { Button } from '@/components/ui/button.tsx';
|
||||||
import { Input } from '@/components/ui/input.tsx';
|
import { Input } from '@/components/ui/input.tsx';
|
||||||
@@ -14,20 +11,32 @@ import {
|
|||||||
} from '@/components/ui/dialog.tsx';
|
} from '@/components/ui/dialog.tsx';
|
||||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs.tsx';
|
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs.tsx';
|
||||||
import { useLoginActions } from '@/hooks/useLoginActions';
|
import { useLoginActions } from '@/hooks/useLoginActions';
|
||||||
|
import { useToast } from '@/hooks/useToast';
|
||||||
|
import SignupFlow from './SignupFlow';
|
||||||
|
|
||||||
interface LoginDialogProps {
|
interface LoginDialogProps {
|
||||||
isOpen: boolean;
|
isOpen: boolean;
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
onLogin: () => void;
|
onLogin: () => void;
|
||||||
onSignup?: () => void;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const LoginDialog: React.FC<LoginDialogProps> = ({ isOpen, onClose, onLogin, onSignup }) => {
|
const LoginDialog: React.FC<LoginDialogProps> = ({ isOpen, onClose, onLogin }) => {
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const [nsec, setNsec] = useState('');
|
const [nsec, setNsec] = useState('');
|
||||||
const [bunkerUri, setBunkerUri] = useState('');
|
const [bunkerUri, setBunkerUri] = useState('');
|
||||||
|
const [view, setView] = useState<'login' | 'signup'>('login');
|
||||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||||
const login = useLoginActions();
|
const login = useLoginActions();
|
||||||
|
const { toast } = useToast();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (isOpen) {
|
||||||
|
setView('login');
|
||||||
|
setIsLoading(false);
|
||||||
|
setNsec('');
|
||||||
|
setBunkerUri('');
|
||||||
|
}
|
||||||
|
}, [isOpen]);
|
||||||
|
|
||||||
const handleExtensionLogin = () => {
|
const handleExtensionLogin = () => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
@@ -40,6 +49,7 @@ const LoginDialog: React.FC<LoginDialogProps> = ({ isOpen, onClose, onLogin, onS
|
|||||||
onClose();
|
onClose();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Extension login failed:', error);
|
console.error('Extension login failed:', error);
|
||||||
|
toast({ title: 'Login Failed', description: (error as Error).message, variant: 'destructive' });
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
@@ -48,13 +58,13 @@ const LoginDialog: React.FC<LoginDialogProps> = ({ isOpen, onClose, onLogin, onS
|
|||||||
const handleKeyLogin = () => {
|
const handleKeyLogin = () => {
|
||||||
if (!nsec.trim()) return;
|
if (!nsec.trim()) return;
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
login.nsec(nsec);
|
login.nsec(nsec);
|
||||||
onLogin();
|
onLogin();
|
||||||
onClose();
|
onClose();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Nsec login failed:', error);
|
console.error('Nsec login failed:', error);
|
||||||
|
toast({ title: 'Login Failed', description: 'Invalid nsec key.', variant: 'destructive' });
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
@@ -63,13 +73,13 @@ const LoginDialog: React.FC<LoginDialogProps> = ({ isOpen, onClose, onLogin, onS
|
|||||||
const handleBunkerLogin = () => {
|
const handleBunkerLogin = () => {
|
||||||
if (!bunkerUri.trim() || !bunkerUri.startsWith('bunker://')) return;
|
if (!bunkerUri.trim() || !bunkerUri.startsWith('bunker://')) return;
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
login.bunker(bunkerUri);
|
login.bunker(bunkerUri);
|
||||||
onLogin();
|
onLogin();
|
||||||
onClose();
|
onClose();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Bunker login failed:', error);
|
console.error('Bunker login failed:', error);
|
||||||
|
toast({ title: 'Bunker Login Failed', description: 'Could not connect to bunker.', variant: 'destructive' });
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
@@ -78,7 +88,6 @@ const LoginDialog: React.FC<LoginDialogProps> = ({ isOpen, onClose, onLogin, onS
|
|||||||
const handleFileUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
|
const handleFileUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
const file = e.target.files?.[0];
|
const file = e.target.files?.[0];
|
||||||
if (!file) return;
|
if (!file) return;
|
||||||
|
|
||||||
const reader = new FileReader();
|
const reader = new FileReader();
|
||||||
reader.onload = (event) => {
|
reader.onload = (event) => {
|
||||||
const content = event.target?.result as string;
|
const content = event.target?.result as string;
|
||||||
@@ -87,130 +96,100 @@ const LoginDialog: React.FC<LoginDialogProps> = ({ isOpen, onClose, onLogin, onS
|
|||||||
reader.readAsText(file);
|
reader.readAsText(file);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSignupClick = () => {
|
const renderLoginView = () => (
|
||||||
onClose();
|
<>
|
||||||
if (onSignup) {
|
<DialogHeader className='px-6 pt-6 pb-0 relative'>
|
||||||
onSignup();
|
<DialogTitle className='text-xl font-semibold text-center'>Log in</DialogTitle>
|
||||||
}
|
<DialogDescription className='text-center text-muted-foreground mt-2'>
|
||||||
};
|
Access your account securely with your preferred method
|
||||||
|
</DialogDescription>
|
||||||
|
</DialogHeader>
|
||||||
|
<div className='px-6 py-8 space-y-6'>
|
||||||
|
<div className='text-center'>
|
||||||
|
<p className='text-sm text-muted-foreground'>New to Nostr?</p>
|
||||||
|
<Button variant='link' className='text-primary' onClick={() => setView('signup')}>
|
||||||
|
Create an account
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
<div className='relative'>
|
||||||
|
<div className='absolute inset-0 flex items-center' aria-hidden='true'>
|
||||||
|
<div className='w-full border-t border-border' />
|
||||||
|
</div>
|
||||||
|
<div className='relative flex justify-center text-sm'>
|
||||||
|
<span className='px-2 bg-background text-muted-foreground'>Or log in with</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<Tabs defaultValue={'nostr' in window ? 'extension' : 'key'} className='w-full'>
|
||||||
|
<TabsList className='grid grid-cols-3 mb-6'>
|
||||||
|
<TabsTrigger value='extension'>Extension</TabsTrigger>
|
||||||
|
<TabsTrigger value='key'>Nsec</TabsTrigger>
|
||||||
|
<TabsTrigger value='bunker'>Bunker</TabsTrigger>
|
||||||
|
</TabsList>
|
||||||
|
<TabsContent value='extension' className='space-y-4'>
|
||||||
|
<div className='text-center p-4 rounded-lg bg-gray-50 dark:bg-gray-800'>
|
||||||
|
<Shield className='w-12 h-12 mx-auto mb-3 text-primary' />
|
||||||
|
<p className='text-sm text-gray-600 dark:text-gray-300 mb-4'>
|
||||||
|
Login with one click using the browser extension
|
||||||
|
</p>
|
||||||
|
<Button className='w-full rounded-full py-6' onClick={handleExtensionLogin} disabled={isLoading}>
|
||||||
|
{isLoading ? 'Logging in...' : 'Login with Extension'}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</TabsContent>
|
||||||
|
<TabsContent value='key' className='space-y-4'>
|
||||||
|
<div className='space-y-4'>
|
||||||
|
<div className='space-y-2'>
|
||||||
|
<label htmlFor='nsec' className='text-sm font-medium text-gray-700 dark:text-gray-400'>
|
||||||
|
Enter your nsec
|
||||||
|
</label>
|
||||||
|
<Input id='nsec' value={nsec} onChange={(e) => setNsec(e.target.value)} className='rounded-lg border-gray-300 dark:border-gray-700 focus-visible:ring-primary' placeholder='nsec1...' />
|
||||||
|
</div>
|
||||||
|
<div className='text-center'>
|
||||||
|
<p className='text-sm mb-2 text-gray-600 dark:text-gray-400'>Or upload a key file</p>
|
||||||
|
<input type='file' accept='.txt' className='hidden' ref={fileInputRef} onChange={handleFileUpload} />
|
||||||
|
<Button variant='outline' className='w-full dark:border-gray-700 dark:bg-gray-800 dark:hover:bg-gray-700' onClick={() => fileInputRef.current?.click()}>
|
||||||
|
<Upload className='w-4 h-4 mr-2' />
|
||||||
|
Upload Nsec File
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
<Button className='w-full rounded-full py-6 mt-4' onClick={handleKeyLogin} disabled={isLoading || !nsec.trim()}>
|
||||||
|
{isLoading ? 'Verifying...' : 'Login with Nsec'}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</TabsContent>
|
||||||
|
<TabsContent value='bunker' className='space-y-4'>
|
||||||
|
<div className='space-y-2'>
|
||||||
|
<label htmlFor='bunkerUri' className='text-sm font-medium text-gray-700 dark:text-gray-400'>
|
||||||
|
Bunker URI
|
||||||
|
</label>
|
||||||
|
<Input id='bunkerUri' value={bunkerUri} onChange={(e) => setBunkerUri(e.target.value)} className='rounded-lg border-gray-300 dark:border-gray-700 focus-visible:ring-primary' placeholder='bunker://' />
|
||||||
|
{bunkerUri && !bunkerUri.startsWith('bunker://') && (<p className='text-red-500 text-xs'>URI must start with bunker://</p>)}
|
||||||
|
</div>
|
||||||
|
<Button className='w-full rounded-full py-6' onClick={handleBunkerLogin} disabled={isLoading || !bunkerUri.trim() || !bunkerUri.startsWith('bunker://')}>
|
||||||
|
{isLoading ? 'Connecting...' : 'Login with Bunker'}
|
||||||
|
</Button>
|
||||||
|
</TabsContent>
|
||||||
|
</Tabs>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
|
||||||
|
const renderSignupView = () => (
|
||||||
|
<>
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>Create Account</DialogTitle>
|
||||||
|
<DialogDescription>Create a new Nostr account.</DialogDescription>
|
||||||
|
</DialogHeader>
|
||||||
|
<div className='px-6 py-8'>
|
||||||
|
<SignupFlow onBack={() => setView('login')} onClose={onClose} />
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog open={isOpen} onOpenChange={onClose}>
|
<Dialog open={isOpen} onOpenChange={onClose}>
|
||||||
<DialogContent className='sm:max-w-md p-0 overflow-hidden rounded-2xl'>
|
<DialogContent className='sm:max-w-md p-0 overflow-hidden rounded-2xl'>
|
||||||
<DialogHeader className='px-6 pt-6 pb-0 relative'>
|
{view === 'login' ? renderLoginView() : renderSignupView()}
|
||||||
<DialogTitle className='text-xl font-semibold text-center'>Log in</DialogTitle>
|
|
||||||
<DialogDescription className='text-center text-muted-foreground mt-2'>
|
|
||||||
Access your account securely with your preferred method
|
|
||||||
</DialogDescription>
|
|
||||||
</DialogHeader>
|
|
||||||
|
|
||||||
<div className='px-6 py-8 space-y-6'>
|
|
||||||
<Tabs defaultValue={'nostr' in window ? 'extension' : 'key'} className='w-full'>
|
|
||||||
<TabsList className='grid grid-cols-3 mb-6'>
|
|
||||||
<TabsTrigger value='extension'>Extension</TabsTrigger>
|
|
||||||
<TabsTrigger value='key'>Nsec</TabsTrigger>
|
|
||||||
<TabsTrigger value='bunker'>Bunker</TabsTrigger>
|
|
||||||
</TabsList>
|
|
||||||
|
|
||||||
<TabsContent value='extension' className='space-y-4'>
|
|
||||||
<div className='text-center p-4 rounded-lg bg-gray-50 dark:bg-gray-800'>
|
|
||||||
<Shield className='w-12 h-12 mx-auto mb-3 text-primary' />
|
|
||||||
<p className='text-sm text-gray-600 dark:text-gray-300 mb-4'>
|
|
||||||
Login with one click using the browser extension
|
|
||||||
</p>
|
|
||||||
<Button
|
|
||||||
className='w-full rounded-full py-6'
|
|
||||||
onClick={handleExtensionLogin}
|
|
||||||
disabled={isLoading}
|
|
||||||
>
|
|
||||||
{isLoading ? 'Logging in...' : 'Login with Extension'}
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</TabsContent>
|
|
||||||
|
|
||||||
<TabsContent value='key' className='space-y-4'>
|
|
||||||
<div className='space-y-4'>
|
|
||||||
<div className='space-y-2'>
|
|
||||||
<label htmlFor='nsec' className='text-sm font-medium text-gray-700 dark:text-gray-400'>
|
|
||||||
Enter your nsec
|
|
||||||
</label>
|
|
||||||
<Input
|
|
||||||
id='nsec'
|
|
||||||
value={nsec}
|
|
||||||
onChange={(e) => setNsec(e.target.value)}
|
|
||||||
className='rounded-lg border-gray-300 dark:border-gray-700 focus-visible:ring-primary'
|
|
||||||
placeholder='nsec1...'
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className='text-center'>
|
|
||||||
<p className='text-sm mb-2 text-gray-600 dark:text-gray-400'>Or upload a key file</p>
|
|
||||||
<input
|
|
||||||
type='file'
|
|
||||||
accept='.txt'
|
|
||||||
className='hidden'
|
|
||||||
ref={fileInputRef}
|
|
||||||
onChange={handleFileUpload}
|
|
||||||
/>
|
|
||||||
<Button
|
|
||||||
variant='outline'
|
|
||||||
className='w-full dark:border-gray-700 dark:bg-gray-800 dark:hover:bg-gray-700'
|
|
||||||
onClick={() => fileInputRef.current?.click()}
|
|
||||||
>
|
|
||||||
<Upload className='w-4 h-4 mr-2' />
|
|
||||||
Upload Nsec File
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<Button
|
|
||||||
className='w-full rounded-full py-6 mt-4'
|
|
||||||
onClick={handleKeyLogin}
|
|
||||||
disabled={isLoading || !nsec.trim()}
|
|
||||||
>
|
|
||||||
{isLoading ? 'Verifying...' : 'Login with Nsec'}
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</TabsContent>
|
|
||||||
|
|
||||||
<TabsContent value='bunker' className='space-y-4'>
|
|
||||||
<div className='space-y-2'>
|
|
||||||
<label htmlFor='bunkerUri' className='text-sm font-medium text-gray-700 dark:text-gray-400'>
|
|
||||||
Bunker URI
|
|
||||||
</label>
|
|
||||||
<Input
|
|
||||||
id='bunkerUri'
|
|
||||||
value={bunkerUri}
|
|
||||||
onChange={(e) => setBunkerUri(e.target.value)}
|
|
||||||
className='rounded-lg border-gray-300 dark:border-gray-700 focus-visible:ring-primary'
|
|
||||||
placeholder='bunker://'
|
|
||||||
/>
|
|
||||||
{bunkerUri && !bunkerUri.startsWith('bunker://') && (
|
|
||||||
<p className='text-red-500 text-xs'>URI must start with bunker://</p>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<Button
|
|
||||||
className='w-full rounded-full py-6'
|
|
||||||
onClick={handleBunkerLogin}
|
|
||||||
disabled={isLoading || !bunkerUri.trim() || !bunkerUri.startsWith('bunker://')}
|
|
||||||
>
|
|
||||||
{isLoading ? 'Connecting...' : 'Login with Bunker'}
|
|
||||||
</Button>
|
|
||||||
</TabsContent>
|
|
||||||
</Tabs>
|
|
||||||
|
|
||||||
<div className='text-center text-sm'>
|
|
||||||
<p className='text-gray-600 dark:text-gray-400'>
|
|
||||||
Don't have an account?{' '}
|
|
||||||
<button
|
|
||||||
onClick={handleSignupClick}
|
|
||||||
className='text-primary hover:underline font-medium'
|
|
||||||
>
|
|
||||||
Sign up
|
|
||||||
</button>
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,167 +0,0 @@
|
|||||||
// 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.
|
|
||||||
|
|
||||||
import React, { useState } from 'react';
|
|
||||||
import { Download, Key } from 'lucide-react';
|
|
||||||
import { Button } from '@/components/ui/button.tsx';
|
|
||||||
import {
|
|
||||||
Dialog,
|
|
||||||
DialogContent,
|
|
||||||
DialogDescription,
|
|
||||||
DialogHeader,
|
|
||||||
DialogTitle,
|
|
||||||
} from '@/components/ui/dialog.tsx';
|
|
||||||
import { toast } from '@/hooks/useToast.ts';
|
|
||||||
import { useLoginActions } from '@/hooks/useLoginActions';
|
|
||||||
import { generateSecretKey, nip19 } from 'nostr-tools';
|
|
||||||
|
|
||||||
interface SignupDialogProps {
|
|
||||||
isOpen: boolean;
|
|
||||||
onClose: () => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
const SignupDialog: React.FC<SignupDialogProps> = ({ isOpen, onClose }) => {
|
|
||||||
const [step, setStep] = useState<'generate' | 'download' | 'done'>('generate');
|
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
|
||||||
const [nsec, setNsec] = useState('');
|
|
||||||
const login = useLoginActions();
|
|
||||||
|
|
||||||
// Generate a proper nsec key using nostr-tools
|
|
||||||
const generateKey = () => {
|
|
||||||
setIsLoading(true);
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Generate a new secret key
|
|
||||||
const sk = generateSecretKey();
|
|
||||||
|
|
||||||
// Convert to nsec format
|
|
||||||
setNsec(nip19.nsecEncode(sk));
|
|
||||||
setStep('download');
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Failed to generate key:', error);
|
|
||||||
toast({
|
|
||||||
title: 'Error',
|
|
||||||
description: 'Failed to generate key. Please try again.',
|
|
||||||
variant: 'destructive',
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
setIsLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const downloadKey = () => {
|
|
||||||
// Create a blob with the key text
|
|
||||||
const blob = new Blob([nsec], { type: 'text/plain' });
|
|
||||||
const url = globalThis.URL.createObjectURL(blob);
|
|
||||||
|
|
||||||
// Create a temporary link element and trigger download
|
|
||||||
const a = document.createElement('a');
|
|
||||||
a.href = url;
|
|
||||||
a.download = 'nsec.txt';
|
|
||||||
document.body.appendChild(a);
|
|
||||||
a.click();
|
|
||||||
|
|
||||||
// Clean up
|
|
||||||
globalThis.URL.revokeObjectURL(url);
|
|
||||||
document.body.removeChild(a);
|
|
||||||
|
|
||||||
toast({
|
|
||||||
title: 'Key downloaded',
|
|
||||||
description: 'Your key has been downloaded. Keep it safe!',
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const finishSignup = () => {
|
|
||||||
login.nsec(nsec);
|
|
||||||
|
|
||||||
setStep('done');
|
|
||||||
onClose();
|
|
||||||
|
|
||||||
toast({
|
|
||||||
title: 'Account created',
|
|
||||||
description: 'You are now logged in.',
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Dialog open={isOpen} onOpenChange={onClose}>
|
|
||||||
<DialogContent className='sm:max-w-md p-0 overflow-hidden rounded-2xl'>
|
|
||||||
<DialogHeader className='px-6 pt-6 pb-0 relative'>
|
|
||||||
<DialogTitle className='text-xl font-semibold text-center'>
|
|
||||||
{step === 'generate' && 'Create Your Account'}
|
|
||||||
{step === 'download' && 'Download Your Key'}
|
|
||||||
{step === 'done' && 'Setting Up Your Account'}
|
|
||||||
</DialogTitle>
|
|
||||||
<DialogDescription className='text-center text-muted-foreground mt-2'>
|
|
||||||
{step === 'generate' && 'Generate a secure key for your account'}
|
|
||||||
{step === 'download' && "Keep your key safe - you'll need it to log in"}
|
|
||||||
{step === 'done' && 'Finalizing your account setup'}
|
|
||||||
</DialogDescription>
|
|
||||||
</DialogHeader>
|
|
||||||
|
|
||||||
<div className='px-6 py-8 space-y-6'>
|
|
||||||
{step === 'generate' && (
|
|
||||||
<div className='text-center space-y-6'>
|
|
||||||
<div className='p-4 rounded-lg bg-gray-50 dark:bg-gray-800 flex items-center justify-center'>
|
|
||||||
<Key className='w-16 h-16 text-primary' />
|
|
||||||
</div>
|
|
||||||
<p className='text-sm text-gray-600 dark:text-gray-300'>
|
|
||||||
We'll generate a secure key for your account. You'll need this key to log in later.
|
|
||||||
</p>
|
|
||||||
<Button
|
|
||||||
className='w-full rounded-full py-6'
|
|
||||||
onClick={generateKey}
|
|
||||||
disabled={isLoading}
|
|
||||||
>
|
|
||||||
{isLoading ? 'Generating key...' : 'Generate my key'}
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{step === 'download' && (
|
|
||||||
<div className='space-y-6'>
|
|
||||||
<div className='p-4 rounded-lg border bg-gray-50 dark:bg-gray-800 overflow-auto'>
|
|
||||||
<code className='text-xs break-all'>{nsec}</code>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className='text-sm text-gray-600 dark:text-gray-300 space-y-2'>
|
|
||||||
<p className='font-medium text-red-500'>Important:</p>
|
|
||||||
<ul className='list-disc pl-5 space-y-1'>
|
|
||||||
<li>This is your only way to access your account</li>
|
|
||||||
<li>Store it somewhere safe</li>
|
|
||||||
<li>Never share this key with anyone</li>
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className='flex flex-col space-y-3'>
|
|
||||||
<Button
|
|
||||||
variant='outline'
|
|
||||||
className='w-full'
|
|
||||||
onClick={downloadKey}
|
|
||||||
>
|
|
||||||
<Download className='w-4 h-4 mr-2' />
|
|
||||||
Download Key
|
|
||||||
</Button>
|
|
||||||
|
|
||||||
<Button
|
|
||||||
className='w-full rounded-full py-6'
|
|
||||||
onClick={finishSignup}
|
|
||||||
>
|
|
||||||
I've saved my key, continue
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{step === 'done' && (
|
|
||||||
<div className='flex justify-center items-center py-8'>
|
|
||||||
<div className='animate-spin rounded-full h-12 w-12 border-b-2 border-primary'></div>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</DialogContent>
|
|
||||||
</Dialog>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default SignupDialog;
|
|
||||||
@@ -0,0 +1,212 @@
|
|||||||
|
import React, { useState, useEffect } from 'react';
|
||||||
|
import { Download, KeyRound, Lock, CheckCircle, Copy, ArrowRight } from 'lucide-react';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { Input } from '@/components/ui/input';
|
||||||
|
import { Textarea } from '@/components/ui/textarea';
|
||||||
|
import { Card, CardContent } from '@/components/ui/card';
|
||||||
|
import { useToast } from '@/hooks/useToast';
|
||||||
|
import { useLoginActions } from '@/hooks/useLoginActions';
|
||||||
|
import { useNostrPublish } from '@/hooks/useNostrPublish';
|
||||||
|
import { generateSecretKey, nip19 } from 'nostr-tools';
|
||||||
|
|
||||||
|
interface SignupFlowProps {
|
||||||
|
onBack: () => void;
|
||||||
|
onClose: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const SignupFlow: React.FC<SignupFlowProps> = ({ onBack, onClose }) => {
|
||||||
|
const [signupStep, setSignupStep] = useState<'welcome' | 'generate' | 'download' | 'profile' | 'done'>('welcome');
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [nsec, setNsec] = useState('');
|
||||||
|
const [keySecured, setKeySecured] = useState<'none' | 'copied' | 'downloaded'>('none');
|
||||||
|
const [profileData, setProfileData] = useState({ name: '', about: '' });
|
||||||
|
const { toast } = useToast();
|
||||||
|
const login = useLoginActions();
|
||||||
|
const { mutateAsync: publishEvent, isPending: isPublishing } = useNostrPublish();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setSignupStep('welcome');
|
||||||
|
setIsLoading(false);
|
||||||
|
setNsec('');
|
||||||
|
setKeySecured('none');
|
||||||
|
setProfileData({ name: '', about: '' });
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const generateKey = () => {
|
||||||
|
setIsLoading(true);
|
||||||
|
setTimeout(() => {
|
||||||
|
try {
|
||||||
|
const sk = generateSecretKey();
|
||||||
|
setNsec(nip19.nsecEncode(sk));
|
||||||
|
setSignupStep('download');
|
||||||
|
toast({ title: 'New key generated', description: 'Keep it safe.' });
|
||||||
|
} catch {
|
||||||
|
toast({ title: 'Error', description: 'Failed to generate key. Please try again.', variant: 'destructive' });
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
}, 1000);
|
||||||
|
};
|
||||||
|
|
||||||
|
const downloadKey = () => {
|
||||||
|
try {
|
||||||
|
const blob = new Blob([nsec], { type: 'text/plain; charset=utf-8' });
|
||||||
|
const url = globalThis.URL.createObjectURL(blob);
|
||||||
|
const filename = 'secret-key.txt';
|
||||||
|
const a = document.createElement('a');
|
||||||
|
a.href = url;
|
||||||
|
a.download = filename;
|
||||||
|
a.style.display = 'none';
|
||||||
|
document.body.appendChild(a);
|
||||||
|
a.click();
|
||||||
|
globalThis.URL.revokeObjectURL(url);
|
||||||
|
document.body.removeChild(a);
|
||||||
|
setKeySecured('downloaded');
|
||||||
|
toast({ title: 'Key Secured', 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 copyKey = () => {
|
||||||
|
navigator.clipboard.writeText(nsec);
|
||||||
|
setKeySecured('copied');
|
||||||
|
toast({ title: 'Copied to clipboard', description: 'Your key has been copied.' });
|
||||||
|
};
|
||||||
|
|
||||||
|
const finishKeySetup = () => {
|
||||||
|
try {
|
||||||
|
login.nsec(nsec);
|
||||||
|
setSignupStep('profile');
|
||||||
|
} catch {
|
||||||
|
toast({ title: 'Login Failed', description: 'Failed to login with the generated key. Please try again.', variant: 'destructive' });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const finishSignup = async (skipProfile = false) => {
|
||||||
|
try {
|
||||||
|
if (!skipProfile && (profileData.name || profileData.about)) {
|
||||||
|
const metadata: Record<string, string> = {};
|
||||||
|
if (profileData.name) metadata.name = profileData.name;
|
||||||
|
if (profileData.about) metadata.about = profileData.about;
|
||||||
|
await publishEvent({ kind: 0, content: JSON.stringify(metadata) });
|
||||||
|
toast({ title: 'Profile Created', description: 'Your profile has been created.' });
|
||||||
|
}
|
||||||
|
setSignupStep('done');
|
||||||
|
setTimeout(() => {
|
||||||
|
onClose();
|
||||||
|
toast({ title: 'Welcome!', description: 'You are now logged in.' });
|
||||||
|
}, 2000);
|
||||||
|
} catch {
|
||||||
|
toast({ title: 'Profile Setup Failed', description: 'Your account was created but profile setup failed. You can update it later.', variant: 'destructive' });
|
||||||
|
setSignupStep('done');
|
||||||
|
setTimeout(() => {
|
||||||
|
onClose();
|
||||||
|
toast({ title: 'Welcome!', description: 'You are now logged in.' });
|
||||||
|
}, 2000);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className='space-y-4'>
|
||||||
|
{signupStep === 'welcome' && (
|
||||||
|
<div className='text-center space-y-4'>
|
||||||
|
<div className='flex justify-center items-center h-24'>
|
||||||
|
<KeyRound className='w-16 h-16 text-primary' />
|
||||||
|
</div>
|
||||||
|
<p>Your key is your identity. It's how you log in and how others see you.</p>
|
||||||
|
<Button className='w-full' onClick={() => setSignupStep('generate')}>
|
||||||
|
<ArrowRight className='w-5 h-5 mr-2' />
|
||||||
|
Get Started
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{signupStep === 'generate' && (
|
||||||
|
<div className='text-center space-y-4'>
|
||||||
|
{isLoading ? (
|
||||||
|
<div className='space-y-3'>
|
||||||
|
<div className='relative flex justify-center items-center h-24'>
|
||||||
|
<KeyRound className='w-20 h-20 mx-auto animate-pulse text-primary' />
|
||||||
|
</div>
|
||||||
|
<p className='text-lg font-semibold'>Generating your key...</p>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className='space-y-3'>
|
||||||
|
<div className='flex justify-center items-center h-24'>
|
||||||
|
<KeyRound className='w-16 h-16 mx-auto text-primary' />
|
||||||
|
</div>
|
||||||
|
<p className='text-lg font-semibold'>Ready to generate your key?</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{!isLoading && (<Button className='w-full' onClick={generateKey} disabled={isLoading}>Generate My Key</Button>)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{signupStep === 'download' && (
|
||||||
|
<div className='space-y-4'>
|
||||||
|
<div className='relative p-3 bg-muted rounded-xl border'>
|
||||||
|
<div className='flex items-center gap-2 mb-2'><Lock className='w-4 h-4' /><span className='text-sm font-medium'>Your Secret Key</span></div>
|
||||||
|
<div className='p-2 bg-background rounded-lg border'><code className='text-xs break-all font-mono'>{nsec}</code></div>
|
||||||
|
</div>
|
||||||
|
<div className='space-y-3'>
|
||||||
|
<p className='text-sm font-medium text-center'>Choose how to secure your key:</p>
|
||||||
|
<div className='grid grid-cols-1 gap-2'>
|
||||||
|
<Card className={`cursor-pointer transition-all duration-200 ${keySecured === 'copied' ? 'ring-2 ring-primary' : 'hover:bg-muted'}`}>
|
||||||
|
<CardContent className='p-3'>
|
||||||
|
<Button variant="ghost" className='w-full h-auto p-0 justify-start' onClick={copyKey}>
|
||||||
|
<div className='flex items-center gap-3 w-full'>
|
||||||
|
<div className={`p-1.5 rounded-lg ${keySecured === 'copied' ? 'bg-primary/20' : 'bg-muted'}`}>
|
||||||
|
{keySecured === 'copied' ? (<CheckCircle className='w-4 h-4 text-primary' />) : (<Copy className='w-4 h-4' />)}
|
||||||
|
</div>
|
||||||
|
<div className='flex-1 text-left'><div className='font-medium text-sm'>Copy to Clipboard</div></div>
|
||||||
|
{keySecured === 'copied' && (<div className='text-xs font-medium text-primary'>✓ Copied</div>)}
|
||||||
|
</div>
|
||||||
|
</Button>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
<Card className={`cursor-pointer transition-all duration-200 ${keySecured === 'downloaded' ? 'ring-2 ring-primary' : 'hover:bg-muted'}`}>
|
||||||
|
<CardContent className='p-3'>
|
||||||
|
<Button variant="ghost" className='w-full h-auto p-0 justify-start' onClick={downloadKey}>
|
||||||
|
<div className='flex items-center gap-3 w-full'>
|
||||||
|
<div className={`p-1.5 rounded-lg ${keySecured === 'downloaded' ? 'bg-primary/20' : 'bg-muted'}`}>
|
||||||
|
{keySecured === 'downloaded' ? (<CheckCircle className='w-4 h-4 text-primary' />) : (<Download className='w-4 h-4' />)}
|
||||||
|
</div>
|
||||||
|
<div className='flex-1 text-left'><div className='font-medium text-sm'>Download as File</div></div>
|
||||||
|
{keySecured === 'downloaded' && (<div className='text-xs font-medium text-primary'>✓ Downloaded</div>)}
|
||||||
|
</div>
|
||||||
|
</Button>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
<Button className='w-full' onClick={finishKeySetup} disabled={keySecured === 'none'}><Lock className='w-4 h-4 mr-2' />Continue</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{signupStep === 'profile' && (
|
||||||
|
<div className='space-y-4'>
|
||||||
|
<div className={`space-y-4 text-left ${isPublishing ? 'opacity-50 pointer-events-none' : ''}`}>
|
||||||
|
<div className='space-y-2'>
|
||||||
|
<label htmlFor='profile-name' className='text-sm font-medium'>Display Name</label>
|
||||||
|
<Input id='profile-name' value={profileData.name} onChange={(e) => setProfileData(prev => ({ ...prev, name: e.target.value }))} placeholder='Your name' disabled={isPublishing} />
|
||||||
|
</div>
|
||||||
|
<div className='space-y-2'>
|
||||||
|
<label htmlFor='profile-about' className='text-sm font-medium'>Bio</label>
|
||||||
|
<Textarea id='profile-about' value={profileData.about} onChange={(e) => setProfileData(prev => ({ ...prev, about: e.target.value }))} placeholder='Tell others about yourself...' rows={3} disabled={isPublishing} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className='space-y-3'>
|
||||||
|
<Button className='w-full' onClick={() => finishSignup(false)} disabled={isPublishing}>{isPublishing ? 'Creating Profile...' : 'Create Profile & Finish'}</Button>
|
||||||
|
<Button variant='outline' className='w-full' onClick={() => finishSignup(true)} disabled={isPublishing}>Skip for now</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{signupStep === 'done' && (
|
||||||
|
<div className='flex justify-center items-center py-8'>
|
||||||
|
<div className='animate-spin rounded-full h-12 w-12 border-b-2 border-primary'></div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<Button variant='link' onClick={onBack}>Back to login</Button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default SignupFlow;
|
||||||
Reference in new issue
Block a user