| 6 | import { useAuth } from "../context/auth-context"; |
| 7 | |
| 8 | export default function SetPassword() { |
| 9 | const { user, checkAuth } = useAuth(); |
| 10 | const navigate = useNavigate(); |
| 11 | const [searchParams] = useSearchParams(); |
| 12 | const nextPage = searchParams.get('next'); // Get redirect target |
| 13 | const [password, setPassword] = useState(''); |
| 14 | const [confirmPassword, setConfirmPassword] = useState(''); |
| 15 | const [isLoading, setIsLoading] = useState(false); |
| 16 | const [showPassword, setShowPassword] = useState(false); |
| 17 | const [showConfirmPassword, setShowConfirmPassword] = useState(false); |
| 18 | const { toast } = useToast(); |
| 19 | |
| 20 | const handleSubmit = async (e) => { |
| 21 | e.preventDefault(); |
| 22 | |
| 23 | if (password !== confirmPassword) { |
| 24 | toast({ |
| 25 | variant: "destructive", |
| 26 | title: "Passwords Don't Match", |
| 27 | description: "Please make sure both passwords are identical.", |
| 28 | }); |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | if (password.length < 8) { |
| 33 | toast({ |
| 34 | variant: "destructive", |
| 35 | title: "Password Too Short", |
| 36 | description: "Password must be at least 8 characters long.", |
| 37 | }); |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | setIsLoading(true); |
| 42 | |
| 43 | try { |
| 44 | await subdomainAPI.post('/auth/email/set-password', { password, confirmPassword }); |
| 45 | |
| 46 | toast({ |
| 47 | title: "Password Set Successfully", |
| 48 | description: nextPage === 'complete-profile' |
| 49 | ? "Redirecting to complete your profile..." |
| 50 | : "You can now login with your new password.", |
| 51 | }); |
| 52 | |
| 53 | // Refresh auth state to update 'hasPassword' flag |
| 54 | await checkAuth(); |
| 55 | |
| 56 | // Redirect based on 'next' parameter |
| 57 | if (nextPage === 'complete-profile') { |
| 58 | // Redirect to complete profile with email pre-filled |
| 59 | window.location.href = `/complete-profile?email=${encodeURIComponent(user?.email || '')}`; |
| 60 | } else { |
| 61 | // Default: redirect to dashboard |
| 62 | navigate('/dashboard'); |
| 63 | } |
| 64 | |
| 65 | } catch (err) { |