'use client'; import './style.scss'; import Link from 'next/link'; import { Checkbox } from '@/components/ui/checkbox'; import { useState, useEffect, useRef } from 'react'; import { GoogleLogin } from '@react-oauth/google'; import { fetchApi } from '@/lib/utils/client'; import { LoginRequest } from '@/types/request/auth'; import { LoginResponse } from '@/types/response/auth'; import useAuth from '@/hooks/useAuth'; export default function Page() { const { login } = useAuth(); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [rememberMe, setRememberMe] = useState(false); const emailRef = useRef(null); const passwordRef = useRef(null); const googleBtnRef = useRef(null); const [googleBtnWidth, setGoogleBtnWidth] = useState(0); useEffect(() => { if (error) { alert(error); setError(''); } }, [error]); useEffect(() => { if (googleBtnRef.current) { const observer = new ResizeObserver(entries => { for (const entry of entries) { setGoogleBtnWidth(Math.floor(entry.contentRect.width)); } }); observer.observe(googleBtnRef.current); return () => observer.disconnect(); } }, []); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); try { if (email.length < 1) { emailRef.current?.focus(); throw new Error('이메일을 입력하세요.'); } if (password.length < 1) { passwordRef.current?.focus(); throw new Error('비밀번호를 입력하세요.'); } const res = await fetchApi('/api/auth/login', { method: 'POST', body: { Email: email, Password: password } as LoginRequest }); login(rememberMe); } catch (err) { if (err instanceof Error) { setError(err.message); } } finally { setLoading(false); } } // 구글 로그인 const handleGoogleLogin = async (credentialResponse: { credential?: string }) => { try { const res = await fetchApi('/api/auth/google-login', { method: 'POST', body: { credential: credentialResponse.credential } }); login(rememberMe); } catch (err) { if (err instanceof Error) { setError(err.message); } } }; const handleGoogleLoginFailed = () => { setError('Google 로그인에 실패했습니다.'); }; return ( <>
로그인
setEmail(e.target.value)} autoComplete="off" autoFocus /> setPassword(e.target.value)} />
{googleBtnWidth > 0 && ( )}
아직 회원이 아니신가요?
회원가입 한번으로 커뮤니티에 참여하세요!
회원가입

비밀번호를 잊으셨나요?
비밀번호를 깜박했다면 다시 설정할 수 있어요!
비밀번호 재설정
setRememberMe(checked === true)} />
); }