page.tsx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 '@/types/request/auth';
  7. import { fetchApi } 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. // 시간 제한 생성
  38. const expiration: string = (Date.now() + 10 * 60 * 1000).toString();
  39. const callbackURL: string = location.pathname;
  40. sessionStorage.setItem("type", VerificationType.ForgotPassword.toString());
  41. sessionStorage.setItem("expiration", expiration);
  42. sessionStorage.setItem("callbackURL", callbackURL);
  43. sessionStorage.setItem("email", email);
  44. router.push("/approval");
  45. } catch (err) {
  46. if (err instanceof Error) {
  47. setError(err.message);
  48. }
  49. } finally {
  50. setLoading(false);
  51. }
  52. }
  53. return (
  54. <>
  55. {loading && <Loading />}
  56. <div id="forgotPasswordForm" className="row-start-2">
  57. <fieldset>
  58. <legend>비밀번호 재설정</legend>
  59. <form method="post" acceptCharset="utf-8" autoComplete="off" className="grid pt-4 pl-4 pr-4 pb-1" onSubmit={handleSubmit}>
  60. <p>{process.env.SITE_NAME} 계정과 연결된 이메일 주소를 입력해주세요.</p>
  61. <p>해당 이메일로 인증번호가 발송되며 아래 입력란에 인증번호를 확인하면 비밀번호 재설정이 가능합니다.</p>
  62. <div>
  63. <br />
  64. </div>
  65. <label htmlFor="email">이메일</label>
  66. <input type="email" name="email" id="email" ref={emailRef} maxLength={30} onChange={e => setEmail(e.target.value)} autoComplete="off" />
  67. <button type="submit" className="btn btn-submit" disabled={loading}>
  68. {loading ? "조회 중..." : "다음 단계로"}
  69. </button>
  70. <hr />
  71. <Link href="/login">취소하기</Link>
  72. </form>
  73. </fieldset>
  74. </div>
  75. </>
  76. );
  77. }