|
1 | | -// src/components/SignUp.jsx |
2 | | -import React from 'react'; |
3 | | -import '../login/Login.css'; // 로그인과 같은 스타일 사용 |
| 1 | +import React, { useState } from 'react'; |
| 2 | +import axios from 'axios'; |
| 3 | +import '../login/Login.css'; // 로그인 스타일 재사용 |
4 | 4 |
|
5 | 5 | function SignUp() { |
| 6 | + const [formData, setFormData] = useState({ |
| 7 | + username: '', |
| 8 | + email: '', |
| 9 | + password: '', |
| 10 | + confirmPassword: '', |
| 11 | + name: '', |
| 12 | + }); |
| 13 | + |
| 14 | + const handleChange = (e) => { |
| 15 | + setFormData({ |
| 16 | + ...formData, |
| 17 | + [e.target.id]: e.target.value |
| 18 | + }); |
| 19 | + }; |
| 20 | + |
| 21 | + const handleSubmit = async (e) => { |
| 22 | + e.preventDefault(); |
| 23 | + |
| 24 | + if (formData.password !== formData.confirmPassword) { |
| 25 | + alert('비밀번호가 일치하지 않습니다.'); |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + try { |
| 30 | + const response = await axios.post('http://localhost:5000/api/users/signup', { |
| 31 | + userId: formData.username, |
| 32 | + email: formData.email, |
| 33 | + password: formData.password, |
| 34 | + name: formData.name, |
| 35 | + }, { |
| 36 | + headers: { |
| 37 | + 'Content-Type': 'application/json' |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + |
| 42 | + console.log('회원가입 성공:', response.data); |
| 43 | + alert('회원가입이 완료되었습니다!'); |
| 44 | + } catch (error) { |
| 45 | + console.error('회원가입 오류:', error.response?.data || error.message); |
| 46 | + alert('회원가입에 실패했습니다.'); |
| 47 | + } |
| 48 | + }; |
| 49 | + |
6 | 50 | return ( |
7 | 51 | <div className="login-container"> |
8 | 52 | <div className="login-box"> |
9 | 53 | <h1 className="login-title">회원가입</h1> |
10 | 54 | <p className="login-subtitle">CodeViz 계정을 만들고 코드 시각화를 시작하세요</p> |
11 | 55 |
|
12 | | - <form className="login-form"> |
| 56 | + <form className="login-form" onSubmit={handleSubmit}> |
13 | 57 | <label htmlFor="username">아이디 *</label> |
14 | | - <input id="username" type="text" placeholder="사용할 아이디 입력" required /> |
| 58 | + <input id="username" type="text" placeholder="사용할 아이디 입력" value={formData.username} onChange={handleChange} required /> |
15 | 59 |
|
16 | 60 | <label htmlFor="email">이메일 *</label> |
17 | | - <input id="email" type="email" placeholder="name@example.com" required /> |
| 61 | + <input id="email" type="email" placeholder="name@example.com" value={formData.email} onChange={handleChange} required /> |
18 | 62 | <small>인증번호를 받을 이메일 주소를 입력해주세요.</small> |
19 | 63 |
|
20 | 64 | <label htmlFor="password">비밀번호 *</label> |
21 | | - <input id="password" type="password" required /> |
| 65 | + <input id="password" type="password" value={formData.password} onChange={handleChange} required /> |
22 | 66 | <small>비밀번호는 8자 이상이어야 합니다.</small> |
23 | 67 |
|
24 | | - <label htmlFor="confirm-password">비밀번호 확인 *</label> |
25 | | - <input id="confirm-password" type="password" required /> |
| 68 | + <label htmlFor="confirmPassword">비밀번호 확인 *</label> |
| 69 | + <input id="confirmPassword" type="password" value={formData.confirmPassword} onChange={handleChange} required /> |
26 | 70 |
|
27 | 71 | <label htmlFor="name">이름 *</label> |
28 | | - <input id="name" type="text" placeholder="홍길동" required /> |
| 72 | + <input id="name" type="text" placeholder="홍길동" value={formData.name} onChange={handleChange} required /> |
29 | 73 |
|
30 | 74 | <div className="remember-me"> |
31 | 75 | <input type="checkbox" id="terms" required /> |
|
0 commit comments