'use client'; import IconLockDots from '@/components/icon/icon-lock-dots'; import { useRouter } from 'next/navigation'; import React, { useState } from 'react'; import axios from 'axios'; interface ChangePasswordResponse { message?: string; error?: string; } const ComponentsAuthChangePasswordForm = () => { const router = useRouter(); const [currentPassword, setCurrentPassword] = useState(''); const [newPassword, setNewPassword] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const [success, setSuccess] = useState(''); const submitForm = async (e: React.FormEvent) => { e.preventDefault(); setError(''); setSuccess(''); setLoading(true); try { const token = localStorage.getItem('token'); if (!token) { setError('You are not logged in.'); setLoading(false); return; } const formData = new FormData(); formData.append('currentPassword', currentPassword); formData.append('newPassword', newPassword); const res = await axios.post( `https://api.socialbuddy.co/api/auth/change-password`, formData, { headers: { Authorization: `Bearer ${token}`, }, } ); setSuccess(res.data.message || 'Password updated successfully!'); localStorage.removeItem('token'); router.push('/login'); } catch (err: any) { const msg = err.response?.data?.error || err.message; setError(msg); } finally { setLoading(false); } }; return (
{/* Alerts */} {error && (
{error}
)} {success && (
{success}
)}
{/* Current Password */}
setCurrentPassword(e.target.value)} required className="w-full ps-10 py-3 bg-[rgba(7,13,30,0.7)] border border-white/15 rounded-lg text-white placeholder:text-gray-400 focus:outline-none focus:border-white/30" />
{/* New Password */}
setNewPassword(e.target.value)} required className="w-full ps-10 py-3 bg-[rgba(7,13,30,0.7)] border border-white/15 rounded-lg text-white placeholder:text-gray-400 focus:outline-none focus:border-white/30" />
{/* Submit */}
); }; export default ComponentsAuthChangePasswordForm;