page.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. 'use client';
  2. import './style.scss';
  3. import Link from 'next/link';
  4. import { Checkbox } from '@/components/ui/checkbox';
  5. import { useState, useEffect, useRef } from 'react';
  6. import { GoogleLogin } from '@react-oauth/google';
  7. import { fetchApi } from '@/lib/utils/client';
  8. import { LoginRequest } from '@/types/request/auth';
  9. import { LoginResponse } from '@/types/response/auth';
  10. import useAuth from '@/hooks/useAuth';
  11. export default function Page()
  12. {
  13. const { login } = useAuth();
  14. const [error, setError] = useState<string>('');
  15. const [loading, setLoading] = useState<boolean>(false);
  16. const [email, setEmail] = useState<string>('');
  17. const [password, setPassword] = useState<string>('');
  18. const [rememberMe, setRememberMe] = useState<boolean>(false);
  19. const emailRef = useRef<HTMLInputElement>(null);
  20. const passwordRef = useRef<HTMLInputElement>(null);
  21. const googleBtnRef = useRef<HTMLDivElement>(null);
  22. const [googleBtnWidth, setGoogleBtnWidth] = useState<number>(0);
  23. useEffect(() => {
  24. if (error) {
  25. alert(error);
  26. setError('');
  27. }
  28. }, [error]);
  29. useEffect(() => {
  30. if (googleBtnRef.current) {
  31. const observer = new ResizeObserver(entries => {
  32. for (const entry of entries) {
  33. setGoogleBtnWidth(Math.floor(entry.contentRect.width));
  34. }
  35. });
  36. observer.observe(googleBtnRef.current);
  37. return () => observer.disconnect();
  38. }
  39. }, []);
  40. const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
  41. e.preventDefault();
  42. try {
  43. if (email.length < 1) {
  44. emailRef.current?.focus();
  45. throw new Error('이메일을 입력하세요.');
  46. }
  47. if (password.length < 1) {
  48. passwordRef.current?.focus();
  49. throw new Error('비밀번호를 입력하세요.');
  50. }
  51. const res = await fetchApi<LoginResponse>('/api/auth/login', {
  52. method: 'POST',
  53. body: { Email: email, Password: password } as LoginRequest
  54. });
  55. login(rememberMe);
  56. } catch (err) {
  57. if (err instanceof Error) {
  58. setError(err.message);
  59. }
  60. } finally {
  61. setLoading(false);
  62. }
  63. }
  64. // 구글 로그인
  65. const handleGoogleLogin = async (credentialResponse: { credential?: string }) => {
  66. try {
  67. const res = await fetchApi<LoginResponse>('/api/auth/google-login', {
  68. method: 'POST',
  69. body: { credential: credentialResponse.credential }
  70. });
  71. login(rememberMe);
  72. } catch (err) {
  73. if (err instanceof Error) {
  74. setError(err.message);
  75. }
  76. }
  77. };
  78. const handleGoogleLoginFailed = () => {
  79. setError('Google 로그인에 실패했습니다.');
  80. };
  81. return (
  82. <>
  83. <div id="loginForm" className="row-start-2 flex flex-row flex-wrap gap-2">
  84. <fieldset className="grow">
  85. <legend>로그인</legend>
  86. <form method="post" acceptCharset="utf-8" autoComplete="off" className="grid p-4" onSubmit={handleSubmit}>
  87. <label htmlFor="email">이메일</label>
  88. <input type="email" name="email" id="email" ref={emailRef} maxLength={30} onChange={e => setEmail(e.target.value)} autoComplete="off" autoFocus />
  89. <label htmlFor="password">비밀번호</label>
  90. <input type="password" name="password" id="password" ref={passwordRef} maxLength={20} onChange={e => setPassword(e.target.value)} />
  91. <button type="submit" className="btn btn-submit" disabled={loading}>
  92. {loading ? "로그인 중..." : "로그인"}
  93. </button>
  94. <div ref={googleBtnRef} className='w-full mt-2'>
  95. {googleBtnWidth > 0 && (
  96. <GoogleLogin
  97. onSuccess={handleGoogleLogin}
  98. onError={handleGoogleLoginFailed}
  99. width={googleBtnWidth}
  100. size="large"
  101. shape="rectangular"
  102. context="signin"
  103. ux_mode="redirect"
  104. login_uri={`${window.location.origin}/login/google/callback`}
  105. logo_alignment="center"
  106. />
  107. )}
  108. </div>
  109. </form>
  110. <hr hidden/>
  111. </fieldset>
  112. <fieldset className="grow basis-1/2">
  113. <dl>
  114. <dt>아직 회원이 아니신가요?</dt>
  115. <dd>회원가입 한번으로 커뮤니티에 참여하세요!</dd>
  116. <dd>
  117. <Link href="/register">
  118. <small>></small> 회원가입
  119. </Link>
  120. </dd>
  121. </dl>
  122. <hr />
  123. <dl>
  124. <dt>비밀번호를 잊으셨나요?</dt>
  125. <dd>비밀번호를 깜박했다면 다시 설정할 수 있어요!</dd>
  126. <dd>
  127. <Link href="/forgot-password">
  128. <small>></small> 비밀번호 재설정
  129. </Link>
  130. </dd>
  131. </dl>
  132. <section className="mt-3">
  133. <Checkbox name="remember_me" id="rememberMe" checked={rememberMe} onCheckedChange={(checked) => setRememberMe(checked === true)} />
  134. <label htmlFor="rememberMe">로그인 상태 유지</label>
  135. </section>
  136. </fieldset>
  137. </div>
  138. </>
  139. );
  140. }