|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { type FormEvent, useMemo, useState } from 'react'; |
| 4 | +import { useRouter, useSearchParams } from 'next/navigation'; |
| 5 | +import Image from 'next/image'; |
| 6 | + |
| 7 | +import Loader from '@/components/ui/common/Loader'; |
| 8 | + |
| 9 | +import AuthLogin from '@/components/auth/screen/AuthLogin'; |
| 10 | +import AuthFindId from '@/components/auth/screen/AuthFindId'; |
| 11 | +import AuthResetPassword from '@/components/auth/screen/AuthResetPassword'; |
| 12 | +import AuthResetRequest from '@/components/auth/screen/AuthResetRequest'; |
| 13 | + |
| 14 | +import { GoogleLogin } from '@/services/auth/signin/google/GoogleLogin'; |
| 15 | +import { login } from '@/services/auth/signin/custom/CustomAuthApi'; |
| 16 | + |
| 17 | +import { useAuth } from '@/hooks/useAuth'; |
| 18 | + |
| 19 | +import loginBg from '@public/images/bgimg.png'; |
| 20 | + |
| 21 | +const DEFAULT_FALLBACK_ROUTE = '/main'; |
| 22 | + |
| 23 | +const getSafeNextUrl = (raw: string | null): string => { |
| 24 | + if (!raw) return DEFAULT_FALLBACK_ROUTE; |
| 25 | + |
| 26 | + try { |
| 27 | + const decoded = decodeURIComponent(raw); |
| 28 | + return decoded.startsWith('/') ? decoded : DEFAULT_FALLBACK_ROUTE; |
| 29 | + } catch { |
| 30 | + return DEFAULT_FALLBACK_ROUTE; |
| 31 | + } |
| 32 | +}; |
| 33 | + |
| 34 | +export default function LoginPage() { |
| 35 | + const router = useRouter(); |
| 36 | + const searchParams = useSearchParams(); |
| 37 | + const { setAccessToken } = useAuth(); |
| 38 | + const { handleGoogleLogin } = GoogleLogin(); |
| 39 | + |
| 40 | + const nextUrl = useMemo( |
| 41 | + () => getSafeNextUrl(searchParams?.get('next') ?? null), |
| 42 | + [searchParams], |
| 43 | + ); |
| 44 | + |
| 45 | + const [password, setPassword] = useState(''); |
| 46 | + const [errors, setErrors] = useState<string[]>([]); |
| 47 | + const [isRendering, setIsRendering] = useState(0); |
| 48 | + const [loading, setLoading] = useState(false); |
| 49 | + const [verifiedEmail, setVerifiedEmail] = useState(''); |
| 50 | + |
| 51 | + const handleBackToLogin = () => setIsRendering(0); |
| 52 | + const handleFindIdClick = () => setIsRendering(1); |
| 53 | + const handleResetPasswordClick = () => setIsRendering(2); |
| 54 | + const handleBackToResetRequest = () => setIsRendering(2); |
| 55 | + const handleResetPasswordNext = (email: string) => { |
| 56 | + setVerifiedEmail(email); |
| 57 | + setIsRendering(3); |
| 58 | + }; |
| 59 | + |
| 60 | + const validatePassword = (value: string): string[] => { |
| 61 | + const newErrors: string[] = []; |
| 62 | + if (value.length <= 0) { |
| 63 | + newErrors.push('비밀번호를 입력해주세요.'); |
| 64 | + } |
| 65 | + return newErrors; |
| 66 | + }; |
| 67 | + |
| 68 | + const onSubmit = async (e: FormEvent<HTMLFormElement>) => { |
| 69 | + e.preventDefault(); |
| 70 | + |
| 71 | + const formData = new FormData(e.currentTarget); |
| 72 | + const email = formData.get('email')?.toString() ?? ''; |
| 73 | + const passwordErrors = validatePassword(password); |
| 74 | + |
| 75 | + if (passwordErrors.length > 0) { |
| 76 | + setErrors(passwordErrors); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + setErrors([]); |
| 81 | + setLoading(true); |
| 82 | + |
| 83 | + try { |
| 84 | + const res = await login(email, password); |
| 85 | + const { exists, access_token } = res.data.data; |
| 86 | + |
| 87 | + if (!exists) { |
| 88 | + alert('아이디 혹은 비밀번호가 올바르지 않습니다.'); |
| 89 | + setLoading(false); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + setAccessToken(access_token); |
| 94 | + |
| 95 | + router.push(nextUrl); |
| 96 | + } catch (error) { |
| 97 | + console.error('로그인 실패:', error); |
| 98 | + alert('로그인 중 오류가 발생했습니다.'); |
| 99 | + setLoading(false); |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + return ( |
| 104 | + <> |
| 105 | + <Loader isLoading={loading} /> |
| 106 | + <Image |
| 107 | + src={loginBg} |
| 108 | + alt='loginBg' |
| 109 | + fill |
| 110 | + className='absolute top-0 left-0 -z-10 object-cover opacity-70 blur-sm' |
| 111 | + /> |
| 112 | + <div className='flex justify-center items-center flex-1 relative'> |
| 113 | + <div |
| 114 | + key='screen1' |
| 115 | + className={`absolute w-full transition-all duration-500 ease-in-out transform ${ |
| 116 | + isRendering === 0 ? 'translate-x-0 opacity-100' : '-translate-x-full opacity-0' |
| 117 | + } flex justify-center items-center`} |
| 118 | + > |
| 119 | + <AuthLogin |
| 120 | + router={router} |
| 121 | + onSubmit={onSubmit} |
| 122 | + errors={errors} |
| 123 | + password={password} |
| 124 | + setPassword={setPassword} |
| 125 | + setErrors={setErrors} |
| 126 | + handleGoogleLogin={() => handleGoogleLogin({ next: nextUrl })} |
| 127 | + handleFindIdClick={handleFindIdClick} |
| 128 | + handleResetPasswordClick={handleResetPasswordClick} |
| 129 | + /> |
| 130 | + </div> |
| 131 | + |
| 132 | + <div |
| 133 | + key='screen2' |
| 134 | + className={`absolute w-full transition-all duration-500 ease-in-out transform ${ |
| 135 | + isRendering === 1 ? 'translate-x-0 opacity-100' : 'translate-x-full opacity-0' |
| 136 | + } flex justify-center items-center mt-[-30px]`} |
| 137 | + > |
| 138 | + <AuthFindId handleBackToLogin={handleBackToLogin} /> |
| 139 | + </div> |
| 140 | + |
| 141 | + <div |
| 142 | + key='screen3' |
| 143 | + className={`absolute w-full transition-all duration-500 ease-in-out transform ${ |
| 144 | + isRendering === 2 |
| 145 | + ? 'translate-x-0 opacity-100' |
| 146 | + : `${isRendering === 3 ? '-translate-x-full' : 'translate-x-full'} opacity-0` |
| 147 | + } flex justify-center items-center`} |
| 148 | + > |
| 149 | + <AuthResetRequest |
| 150 | + handleNextStep={handleResetPasswordNext} |
| 151 | + handleBackToLogin={handleBackToLogin} |
| 152 | + setLoading={setLoading} |
| 153 | + /> |
| 154 | + </div> |
| 155 | + |
| 156 | + <div |
| 157 | + key='screen4' |
| 158 | + className={`absolute w-full transition-all duration-500 ease-in-out transform ${ |
| 159 | + isRendering === 3 ? 'translate-x-0 opacity-100' : 'translate-x-full opacity-0' |
| 160 | + } flex justify-center items-center`} |
| 161 | + > |
| 162 | + <AuthResetPassword |
| 163 | + email={verifiedEmail} |
| 164 | + handleBackToLogin={handleBackToLogin} |
| 165 | + handleBackToResetRequest={handleBackToResetRequest} |
| 166 | + setLoading={setLoading} |
| 167 | + /> |
| 168 | + </div> |
| 169 | + </div> |
| 170 | + </> |
| 171 | + ); |
| 172 | +} |
0 commit comments