page.tsx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 { ForgotPasswordRequest } from '@/dtos/request/auth';
  7. import { fetchApi, throwError } from '@/lib/utils/client';
  8. import { VerificationType } from '@/constants/common';
  9. import Loading from '@/app/component/Loading';
  10. export default function ForgotPassword()
  11. {
  12. const router = useRouter();
  13. const [loading, setLoading] = useState<boolean>(false);
  14. const [error, setError] = useState<string>('');
  15. const [email, setEmail] = useState<string>('');
  16. const emailRef = useRef<HTMLInputElement>(null);
  17. useEffect(() => {
  18. if (error) {
  19. alert(error);
  20. setError('');
  21. }
  22. }, [error]);
  23. const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
  24. e.preventDefault();
  25. try {
  26. setLoading(true);
  27. setError('');
  28. if (!email) {
  29. emailRef.current?.focus();
  30. throw new Error('이메일을 입력해주세요.');
  31. }
  32. await new Promise(resolve => setTimeout(resolve, 500));
  33. const res = await fetchApi('/api/auth/forgot-password', {
  34. method: 'POST',
  35. body: { Email: email } as ForgotPasswordRequest
  36. });
  37. throwError(res);
  38. // 시간 제한 생성
  39. const expiration: string = (Date.now() + 10 * 60 * 1000).toString();
  40. const callbackURL: string = location.pathname;
  41. sessionStorage.setItem("type", VerificationType.ForgotPassword.toString());
  42. sessionStorage.setItem("expiration", expiration);
  43. sessionStorage.setItem("callbackURL", callbackURL);
  44. sessionStorage.setItem("email", email);
  45. router.push("/approval");
  46. } catch (err) {
  47. if (err instanceof Error) {
  48. setError(err.message);
  49. }
  50. } finally {
  51. setLoading(false);
  52. }
  53. }
  54. return (
  55. <>
  56. {loading && <Loading />}
  57. <div id="forgotPasswordForm" className="row-start-2">
  58. <fieldset>
  59. <legend>비밀번호 재설정</legend>
  60. <form method="post" acceptCharset="utf-8" autoComplete="off" className="grid pt-4 pl-4 pr-4 pb-1" onSubmit={handleSubmit}>
  61. <p>{process.env.SITE_NAME} 계정과 연결된 이메일 주소를 입력해주세요.</p>
  62. <p>해당 이메일로 인증번호가 발송되며 아래 입력란에 인증번호를 확인하면 비밀번호 재설정이 가능합니다.</p>
  63. <br />
  64. <label htmlFor="email">이메일</label>
  65. <input type="email" name="email" id="email" ref={emailRef} maxLength={30} onChange={e => setEmail(e.target.value)} autoComplete="off" />
  66. <button type="submit" className="btn btn-submit" disabled={loading}>
  67. {loading ? "조회 중..." : "다음 단계로"}
  68. </button>
  69. <hr />
  70. <Link href="/login">취소하기</Link>
  71. </form>
  72. </fieldset>
  73. </div>
  74. </>
  75. );
  76. }