'use client'; import './style.scss'; import Link from 'next/link'; import { useRouter } from 'next/navigation'; import { useState, useEffect, useRef } from 'react'; import { ResetPasswordRequest } from '@/types/request/auth'; import { fetchApi } from '@/lib/utils/client'; import Loading from '@/app/component/Loading'; export default function ResetPassword() { const router = useRouter(); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const [password, setPassword] = useState(''); const [rePassword, setRePassword] = useState(''); const [email, setEmail] = useState(null); const passwordRef = useRef(null); const rePasswordRef = useRef(null); useEffect(() => { if (error) { alert(error); setError(''); } }, [error]); useEffect(() => { const storedEmail = sessionStorage.getItem('email'); if (!storedEmail) { setError('잘못된 접근입니다.'); router.push('/login'); return; } setEmail(storedEmail); }, [router]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); try { setLoading(true); setError(''); if (!email) { throw new Error("이메일을 입력하세요."); } if (!password) { passwordRef.current?.focus(); throw new Error("비밀번호를 입력하세요."); } if (!rePassword) { rePasswordRef.current?.focus(); throw new Error("비밀번호를 입력하세요."); } if (password !== rePassword) { throw new Error("비밀번호가 일치하지 않습니다."); } await new Promise(resolve => setTimeout(resolve, 500)); const res = await fetchApi('/api/auth/reset-password', { method: 'POST', body: { Email: email, Password: password, RePassword: rePassword } as ResetPasswordRequest }); sessionStorage.clear(); alert(res.message); router.push('/login'); } catch (err) { if (err instanceof Error) { setError(err.message); } } finally { setLoading(false); } } return ( <> {loading && }
비밀번호 변경
setPassword(e.target.value)} /> setRePassword(e.target.value)} />

취소
); }