page.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 'use client';
  2. import './style.scss';
  3. import Link from 'next/link';
  4. import { useRouter } from 'next/navigation';
  5. import { useState, useEffect, useRef } from 'react';
  6. import { fetchResetPassword } from '@/lib/api/auth';
  7. import { throwError } from '@/lib/utils/client';
  8. import Loading from '@/app/component/Loading';
  9. export default function ResetPassword()
  10. {
  11. const router = useRouter();
  12. const [loading, setLoading] = useState<boolean>(false);
  13. const [error, setError] = useState<string>('');
  14. const [password, setPassword] = useState<string>('');
  15. const [rePassword, setRePassword] = useState<string>('');
  16. const [email, setEmail] = useState<string|null>(null);
  17. const passwordRef = useRef<HTMLInputElement>(null);
  18. const rePasswordRef = useRef<HTMLInputElement>(null);
  19. useEffect(() => {
  20. if (error) {
  21. alert(error);
  22. setError('');
  23. }
  24. }, [error]);
  25. useEffect(() => {
  26. const storedEmail = sessionStorage.getItem('email');
  27. if (!storedEmail) {
  28. setError('잘못된 접근입니다.');
  29. router.push('/login');
  30. return;
  31. }
  32. setEmail(storedEmail);
  33. }, [router]);
  34. const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
  35. e.preventDefault();
  36. try {
  37. setLoading(true);
  38. setError('');
  39. if (!email) {
  40. throw new Error("이메일을 입력하세요.");
  41. }
  42. if (!password) {
  43. passwordRef.current?.focus();
  44. throw new Error("비밀번호를 입력하세요.");
  45. }
  46. if (!rePassword) {
  47. rePasswordRef.current?.focus();
  48. throw new Error("비밀번호를 입력하세요.");
  49. }
  50. if (password !== rePassword) {
  51. throw new Error("비밀번호가 일치하지 않습니다.");
  52. }
  53. await new Promise(resolve => setTimeout(resolve, 500));
  54. const res = await fetchResetPassword({
  55. Email: email,
  56. Password: password,
  57. RePassword: rePassword
  58. });
  59. if (!res.ok) {
  60. throwError(res);
  61. }
  62. sessionStorage.clear();
  63. alert(res.message);
  64. router.push('/login');
  65. } catch (err: any) {
  66. setError(err.message);
  67. } finally {
  68. setLoading(false);
  69. }
  70. }
  71. return (
  72. <>
  73. {loading && <Loading />}
  74. <div id="resetPasswordForm" className="row-start-2">
  75. <fieldset>
  76. <legend>비밀번호 변경</legend>
  77. <form id="fResetPassword" method="post" acceptCharset="utf-8" autoComplete="off" onSubmit={handleSubmit}>
  78. <section className="grow sm:pt-4">
  79. <article className="grid">
  80. <label htmlFor="email">이메일</label>
  81. <input type="email" name="email" id="email" value={email || ''} disabled />
  82. <label htmlFor="password">비밀번호</label>
  83. <input type="password" name="password" id="password" ref={passwordRef} maxLength={20} value={password} onChange={e => setPassword(e.target.value)} />
  84. <label htmlFor="rePassword">비밀번호 확인</label>
  85. <input type="password" name="re_password" id="rePassword" ref={rePasswordRef} maxLength={20} value={rePassword} onChange={e => setRePassword(e.target.value)} />
  86. </article>
  87. </section>
  88. <hr />
  89. <div className="grid grid-cols-2 gap-2">
  90. <button type="submit" className="btn btn-submit" disabled={loading}>
  91. {loading ? "변경 중..." : "변경하기"}
  92. </button>
  93. <Link href="/login" className="btn btn-default">취소</Link>
  94. </div>
  95. </form>
  96. </fieldset>
  97. </div>
  98. </>
  99. );
  100. }