-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSignUpForm.jsx
89 lines (80 loc) ยท 2.94 KB
/
SignUpForm.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { createUserWithEmailAndPassword, updateProfile } from 'firebase/auth';
import { auth } from '../../../firebaseConfig';
import './signup.css';
function SignUpForm() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [nickname, setNickname] = useState('');
const navigate = useNavigate();
async function handleSignUpSubmit(ev) {
ev.preventDefault();
const inputEmail = ev.target.elements.email.value;
const inputPassword = ev.target.elements.password.value;
const inputNickname = ev.target.elements.nickname.value;
const exp = /^[A-Za-z0-9_\.\-]+@[A-Za-z0-9\-]+\.[A-Za-z0-9_\-]+/;
setEmail('');
setPassword('');
setNickname('');
if (!exp.test(inputEmail) || inputPassword.length < 6 || inputNickname === '') {
if (!exp.test(inputEmail)) {
setEmail('์ฌ๋ฐ๋ฅธ ํ์์ ์ด๋ฉ์ผ์ด ์๋๋๋ค.');
if (inputEmail === '') {
setEmail('์ด๋ฉ์ผ์ ์
๋ ฅํด์ฃผ์ธ์.');
}
}
if (inputPassword.length < 6) {
setPassword('๋น๋ฐ๋ฒํธ๋ 6๊ธ์ ์ด์์ด์ด์ผ ํฉ๋๋ค.');
if (inputPassword === '') {
setPassword('๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.');
}
}
if (inputNickname === '') {
setNickname('๋๋ค์์ ์
๋ ฅํด์ฃผ์ธ์.');
}
} else {
try {
const userCredential = await createUserWithEmailAndPassword(auth, inputEmail, inputPassword);
const user = userCredential.user;
await updateProfile(user, {
displayName: inputNickname,
});
navigate('/login');
} catch (error) {
if (error.code === 'auth/missing-email') {
setEmail('์ด๋ฏธ ์กด์ฌํ๋ ์ด๋ฉ์ผ์
๋๋ค.');
}
if (error.code === 'auth/invalid-email') {
setEmail('์๋ชป๋ ํ์์ ์ด๋ฉ์ผ์
๋๋ค.');
}
console.log('Failed!');
}
}
}
return (
<>
<form action='post' className='sign-up-form' onSubmit={handleSignUpSubmit}>
<label htmlFor='email'>์ด๋ฉ์ผ</label>
<input type='email' id='email' name='email' placeholder='Email' className='signup-email-input' />
<div className='check'>{email}</div>
<label htmlFor='password'>๋น๋ฐ๋ฒํธ</label>
<input type='password' id='password' name='password' placeholder='๋น๋ฐ๋ฒํธ' className='signup-password-input' />
<div className='check'>{password}</div>
<label htmlFor='nickname'>๋๋ค์</label>
<input
type='text'
id='nickname'
name='nickname'
placeholder='์ฌ์ฉํ์ค ๋๋ค์'
className='signup-nickname-input'
/>
<div className='check'>{nickname}</div>
<button type='submit' className='signup-button'>
ํ์ ๊ฐ์
</button>
</form>
</>
);
}
export default SignUpForm;