|
| 1 | +import { useState } from 'react' |
| 2 | +import { useNavigate } from 'react-router-dom' |
| 3 | +import { toast } from 'sonner' |
| 4 | + |
| 5 | +import { Button } from '@/components/ui/button' |
| 6 | +import { |
| 7 | + Card, |
| 8 | + CardContent, |
| 9 | + CardDescription, |
| 10 | + CardHeader, |
| 11 | + CardTitle, |
| 12 | +} from '@/components/ui/card' |
| 13 | +import { Input } from '@/components/ui/input' |
| 14 | +import { useSetupPassword } from '@/hooks/useAuth' |
| 15 | + |
| 16 | +export function Setup() { |
| 17 | + const [password, setPassword] = useState('') |
| 18 | + const [confirmPassword, setConfirmPassword] = useState('') |
| 19 | + const setupPassword = useSetupPassword() |
| 20 | + const navigate = useNavigate() |
| 21 | + |
| 22 | + const handleSubmit = async (e: React.FormEvent) => { |
| 23 | + e.preventDefault() |
| 24 | + |
| 25 | + if (password.length < 8) { |
| 26 | + toast.error('Password must be at least 8 characters long') |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + if (password !== confirmPassword) { |
| 31 | + toast.error('Passwords do not match') |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + try { |
| 36 | + await setupPassword.mutateAsync(password) |
| 37 | + toast.success('Password set up successfully') |
| 38 | + navigate('/') |
| 39 | + } catch (error) { |
| 40 | + toast.error( |
| 41 | + error instanceof Error ? error.message : 'Failed to set up password' |
| 42 | + ) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + return ( |
| 47 | + <div className="flex min-h-screen items-center justify-center p-4"> |
| 48 | + <Card className="w-full max-w-md"> |
| 49 | + <CardHeader> |
| 50 | + <CardTitle>Set Up Password</CardTitle> |
| 51 | + <CardDescription> |
| 52 | + Create a password to secure your portfolio. This is a one-time setup. |
| 53 | + </CardDescription> |
| 54 | + </CardHeader> |
| 55 | + <CardContent> |
| 56 | + <form onSubmit={handleSubmit} className="space-y-4"> |
| 57 | + <div className="space-y-2"> |
| 58 | + <Input |
| 59 | + type="password" |
| 60 | + placeholder="Password (min 8 characters)" |
| 61 | + value={password} |
| 62 | + onChange={(e) => setPassword(e.target.value)} |
| 63 | + autoFocus |
| 64 | + required |
| 65 | + /> |
| 66 | + </div> |
| 67 | + <div className="space-y-2"> |
| 68 | + <Input |
| 69 | + type="password" |
| 70 | + placeholder="Confirm password" |
| 71 | + value={confirmPassword} |
| 72 | + onChange={(e) => setConfirmPassword(e.target.value)} |
| 73 | + required |
| 74 | + /> |
| 75 | + </div> |
| 76 | + <Button |
| 77 | + type="submit" |
| 78 | + className="w-full" |
| 79 | + isLoading={setupPassword.isPending} |
| 80 | + > |
| 81 | + Set Up Password |
| 82 | + </Button> |
| 83 | + </form> |
| 84 | + </CardContent> |
| 85 | + </Card> |
| 86 | + </div> |
| 87 | + ) |
| 88 | +} |
0 commit comments