|
| 1 | +import { useState } from 'react'; |
| 2 | +import { useNavigate } from 'react-router-dom'; |
| 3 | +import { signup } from '@/api/client'; |
| 4 | +import '@/styles/App.css'; |
| 5 | + |
| 6 | +export function SignupPage() { |
| 7 | + const navigate = useNavigate(); |
| 8 | + const [formData, setFormData] = useState({ |
| 9 | + name: '', |
| 10 | + email: '', |
| 11 | + password: '', |
| 12 | + confirmPassword: '', |
| 13 | + }); |
| 14 | + const [error, setError] = useState<string>(''); |
| 15 | + |
| 16 | + const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 17 | + const { name, value } = e.target; |
| 18 | + setFormData(prev => ({ ...prev, [name]: value })); |
| 19 | + }; |
| 20 | + |
| 21 | + const handleSubmit = async (e: React.FormEvent) => { |
| 22 | + e.preventDefault(); |
| 23 | + setError(''); |
| 24 | + |
| 25 | + if (formData.password !== formData.confirmPassword) { |
| 26 | + setError('비밀번호가 일치하지 않습니다.'); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + if (formData.password.length < 8) { |
| 31 | + setError('비밀번호는 8자 이상이어야 합니다.'); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + const result = await signup({ |
| 36 | + name: formData.name, |
| 37 | + email: formData.email, |
| 38 | + password: formData.password |
| 39 | + }); |
| 40 | + |
| 41 | + if (result.success) { |
| 42 | + alert('회원가입 성공!'); |
| 43 | + navigate('/login'); |
| 44 | + } else { |
| 45 | + setError(result.error || '회원가입에 실패했습니다.'); |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + return ( |
| 50 | + <div className="container"> |
| 51 | + <header className="app-header"> |
| 52 | + <div className="logo" onClick={() => navigate('/')}>자산관리</div> |
| 53 | + <div className="auth-buttons"> |
| 54 | + <button className="text-btn active">회원가입</button> |
| 55 | + <button className="text-btn" onClick={() => navigate('/login')}>로그인</button> |
| 56 | + </div> |
| 57 | + </header> |
| 58 | + |
| 59 | + <main className="auth-container"> |
| 60 | + <h2>회원가입</h2> |
| 61 | + <form onSubmit={handleSubmit} className="auth-form"> |
| 62 | + <div className="form-group"> |
| 63 | + <label htmlFor="name">이름</label> |
| 64 | + <input |
| 65 | + type="text" |
| 66 | + id="name" |
| 67 | + name="name" |
| 68 | + value={formData.name} |
| 69 | + onChange={handleChange} |
| 70 | + required |
| 71 | + maxLength={30} |
| 72 | + placeholder="이름을 입력하세요" |
| 73 | + /> |
| 74 | + </div> |
| 75 | + |
| 76 | + <div className="form-group"> |
| 77 | + <label htmlFor="password">비밀번호</label> |
| 78 | + <input |
| 79 | + type="password" |
| 80 | + id="password" |
| 81 | + name="password" |
| 82 | + value={formData.password} |
| 83 | + onChange={handleChange} |
| 84 | + required |
| 85 | + minLength={8} |
| 86 | + placeholder="8자 이상 입력하세요" |
| 87 | + /> |
| 88 | + </div> |
| 89 | + |
| 90 | + <div className="form-group"> |
| 91 | + <label htmlFor="confirmPassword">비밀번호 확인</label> |
| 92 | + <input |
| 93 | + type="password" |
| 94 | + id="confirmPassword" |
| 95 | + name="confirmPassword" |
| 96 | + value={formData.confirmPassword} |
| 97 | + onChange={handleChange} |
| 98 | + required |
| 99 | + placeholder="다시 한번 입력하세요" |
| 100 | + /> |
| 101 | + </div> |
| 102 | + |
| 103 | + <div className="form-group"> |
| 104 | + <label htmlFor="email">이메일</label> |
| 105 | + <input |
| 106 | + type="email" |
| 107 | + id="email" |
| 108 | + name="email" |
| 109 | + value={formData.email} |
| 110 | + onChange={handleChange} |
| 111 | + required |
| 112 | + placeholder="example@email.com" |
| 113 | + /> |
| 114 | + </div> |
| 115 | + |
| 116 | + {error && <p className="error-message">{error}</p>} |
| 117 | + |
| 118 | + <button type="submit" className="submit-btn">회원가입</button> |
| 119 | + </form> |
| 120 | + </main> |
| 121 | + </div> |
| 122 | + ); |
| 123 | +} |
0 commit comments