"use client" import type React from "react" import { useState, useEffect } from "react" import { useRouter } from "next/navigation" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Alert, AlertDescription } from "@/components/ui/alert" export default function ChangePasswordPage() { const [currentPassword, setCurrentPassword] = useState("") const [newPassword, setNewPassword] = useState("") const [confirmPassword, setConfirmPassword] = useState("") const [error, setError] = useState("") const [loading, setLoading] = useState(false) const [user, setUser] = useState(null) const router = useRouter() useEffect(() => { const userData = localStorage.getItem("user") if (!userData) { router.push("/login") return } setUser(JSON.parse(userData)) }, [router]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setLoading(true) setError("") if (newPassword !== confirmPassword) { setError("הסיסמאות החדשות אינן תואמות") setLoading(false) return } if (newPassword.length < 6) { setError("הסיסמה החדשה חייבת להכיל לפחות 6 תווים") setLoading(false) return } try { const response = await fetch("/api/auth/change-password", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ nationalId: user.national_id, currentPassword, newPassword, }), }) const data = await response.json() if (response.ok) { // Update user data const updatedUser = { ...user, must_change_password: false } localStorage.setItem("user", JSON.stringify(updatedUser)) router.push("/dashboard") } else { setError(data.error || "שגיאה בשינוי סיסמה") } } catch (err) { setError("שגיאה בחיבור לשרת") } finally { setLoading(false) } } if (!user) return null return (
שינוי סיסמה {user.must_change_password ? "הסיסמה אופסה או לא שונתה, ויש לשנותה על מנת להמשיך." : "שינוי סיסמה"}
setCurrentPassword(e.target.value)} placeholder="הזן סיסמה נוכחית" required />
setNewPassword(e.target.value)} placeholder="הזן סיסמה חדשה (לפחות 6 תווים)" required minLength={6} />
setConfirmPassword(e.target.value)} placeholder="הזן שוב את הסיסמה החדשה" required minLength={6} />
{error && ( {error} )}
) }