page.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 { Checkbox } from '@/components/ui/checkbox';
  7. import { Dialog, DialogTrigger } from '@/components/ui/dialog';
  8. import TermsDialog from '@/app/component/TermsDialog';
  9. import { VerificationType } from '@/constants/common';
  10. import { fetchApi } from '@/lib/utils/client';
  11. import { RegisterRequest } from '@/types/request/auth';
  12. import { RegisterResponse } from '@/types/response/auth';
  13. export default function Page()
  14. {
  15. const router = useRouter();
  16. const [error, setError] = useState<string>('');
  17. const [loading, setLoading] = useState<boolean>(false);
  18. const [email, setEmail] = useState<string>('');
  19. const [password, setPassword] = useState<string>('');
  20. const [rePassword, setRePassword] = useState<string>('');
  21. const [agree1, setAgree1] = useState<boolean>(false);
  22. const [agree2, setAgree2] = useState<boolean>(false);
  23. const emailRef = useRef<HTMLInputElement>(null);
  24. const passwordRef = useRef<HTMLInputElement>(null);
  25. const rePasswordRef = useRef<HTMLInputElement>(null);
  26. useEffect(() => {
  27. if (error) {
  28. alert(error);
  29. setError('');
  30. }
  31. }, [error]);
  32. const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
  33. e.preventDefault();
  34. try {
  35. if (email.length < 1) {
  36. emailRef.current?.focus();
  37. throw new Error('이메일을 입력해주세요.');
  38. }
  39. if (password.length < 1) {
  40. passwordRef.current?.focus();
  41. throw new Error('비밀번호를 입력해주세요.');
  42. }
  43. if (rePassword.length < 1) {
  44. rePasswordRef.current?.focus();
  45. throw new Error('비밀번호 확인을 입력해주세요.');
  46. }
  47. if (password !== rePassword) {
  48. throw new Error('비밀번호가 서로 일치하지 않습니다.');
  49. }
  50. if (!agree1 || !agree2) {
  51. throw new Error('모든 필수 약관에 동의해주세요.');
  52. }
  53. await new Promise(resolve => setTimeout(resolve, 500));
  54. const res = await fetchApi<RegisterResponse>('/api/auth/register', {
  55. method: 'POST',
  56. body: {
  57. Email: email,
  58. Password: password,
  59. IsPolicyAgree: agree1,
  60. IsPrivacyAgree: agree2
  61. } as RegisterRequest
  62. });
  63. // 시간 제한 생성
  64. const expiration: string = (Date.now() + 10 * 60 * 1000).toString();
  65. const callbackURL: string = location.pathname;
  66. sessionStorage.setItem('type', VerificationType.Registration.toString());
  67. sessionStorage.setItem('expiration', expiration);
  68. sessionStorage.setItem('callbackURL', callbackURL);
  69. sessionStorage.setItem('email', email);
  70. if (res.data!.isRegisterEmailAuth) {
  71. // 이메일 인증 필요
  72. router.push('/approval');
  73. } else {
  74. // 회원가입 완료
  75. router.push('/welcome');
  76. }
  77. } catch (err) {
  78. if (err instanceof Error) {
  79. setError(err.message);
  80. }
  81. } finally {
  82. setLoading(false);
  83. }
  84. }
  85. return (
  86. <>
  87. <div id="registForm" className="row-start-2">
  88. <fieldset>
  89. <legend>회원가입</legend>
  90. <form method="post" acceptCharset="utf-8" autoComplete="off" onSubmit={handleSubmit}>
  91. <div className="flex flex-row flex-wrap">
  92. <section className="sm:pt-4 sm:pb-4">
  93. <dl>
  94. <dt hidden>&nbsp;</dt>
  95. <dd>{ process.env.NEXT_PUBLIC_SITE_NAME } 에 오신 것을 환영합니다.</dd>
  96. <dd>빠르고 간단하게 회원가입을 진행하세요.</dd>
  97. </dl>
  98. <br/>
  99. <dl>
  100. <dt hidden>&nbsp;</dt>
  101. <dd>가입 확인을 위해 유효한 이메일을 입력해주세요.</dd>
  102. <dd>비밀번호는 최소 8자 이상 입력해주세요.</dd>
  103. </dl>
  104. </section>
  105. <section className="sm:pt-4">
  106. <article className="grid">
  107. <label htmlFor="email">이메일</label>
  108. <input type="email" name="email" id="email" ref={emailRef} maxLength={30} onChange={e => setEmail(e.target.value)} autoComplete="off" />
  109. <label htmlFor="password">비밀번호</label>
  110. <input type="password" name="password" id="password" ref={passwordRef} maxLength={20} onChange={e => setPassword(e.target.value)} />
  111. <label htmlFor="rePassword">비밀번호 확인</label>
  112. <input type="password" name="re_password" id="rePassword" ref={rePasswordRef} maxLength={20} onChange={e => setRePassword(e.target.value)} />
  113. </article>
  114. </section>
  115. </div>
  116. <hr />
  117. <div className="flex flex-row flex-wrap">
  118. <section>
  119. <dl>
  120. <dt>회원가입 약관</dt>
  121. <dd>원활한 서비스 이용을 위해 약관 동의가 필요합니다.</dd>
  122. <dd>약관 내용을 자세히 확인하신 후 동의해주세요.</dd>
  123. </dl>
  124. </section>
  125. <section className="pt-4 sm:pt-0">
  126. <p>
  127. <Checkbox name="agree_1" id="agree_1" onCheckedChange={(checked) => setAgree1(Boolean(checked))} />
  128. <label htmlFor="agree_1">
  129. <Dialog>
  130. <DialogTrigger>이용약관</DialogTrigger>
  131. <TermsDialog subject="이용약관" code="terms" />
  132. </Dialog>
  133. 에 동의합니다.
  134. </label>
  135. </p>
  136. <p>
  137. <Checkbox name="agree_2" id="agree_2" onCheckedChange={(checked) => setAgree2(Boolean(checked))} />
  138. <label htmlFor="agree_2">
  139. <Dialog>
  140. <DialogTrigger>개인정보처리방침</DialogTrigger>
  141. <TermsDialog subject="개인정보처리방침" code="privacy" />
  142. </Dialog>
  143. 에 동의합니다.
  144. </label>
  145. </p>
  146. <div className="grid grid-cols-2 gap-2 mt-3">
  147. <button type="submit" className="btn btn-submit" disabled={loading}>
  148. {loading ? "회원가입 중..." : "회원가입"}
  149. </button>
  150. <Link href="/login" className="btn btn-default">취소</Link>
  151. </div>
  152. </section>
  153. </div>
  154. </form>
  155. </fieldset>
  156. </div>
  157. </>
  158. );
  159. }