Commit a870ddb1 authored by 이재연's avatar 이재연
Browse files

aa

parent 0da1ed33
import React, { useState, useEffect, useRef } from 'react'; import React, { useState, useEffect, useRef } from 'react';
import { Redirect } from 'react-router-dom'; import axios from 'axios'
import Nav1 from '../Components/MainNav'; import Nav1 from '../Components/MainNav';
import Nav2 from '../Components/SubNav'; import Nav2 from '../Components/SubNav';
import { Form, Col, Container, Button, Row, Alert } from 'react-bootstrap' import { Form, Col, Container, Button, Row, Alert } from 'react-bootstrap'
import FormCheckInput from 'react-bootstrap/esm/FormCheckInput'; import catchErrors from '../utils/catchErrors'
const INIT_USER = { const INIT_USER = {
name: '', name: '',
...@@ -16,14 +16,8 @@ const INIT_USER = { ...@@ -16,14 +16,8 @@ const INIT_USER = {
function Signup() { function Signup() {
const [user, setUser] = useState(true) const [user, setUser] = useState(true)
//const [disabled, setDisabled] = useState(true)
const [error, setError] = useState('') const [error, setError] = useState('')
//useEffect(() => {
// const isUser = Object.values(user).every(el => Boolean(el))
// isUser ? setDisabled(false) : setDisabled(true)
//}, user)
function handleChange(event) { function handleChange(event) {
const { name, value } = event.target const { name, value } = event.target
setUser({ ...user, [name]: value }) setUser({ ...user, [name]: value })
...@@ -44,18 +38,10 @@ function Signup() { ...@@ -44,18 +38,10 @@ function Signup() {
try { try {
setError('') setError('')
const response = await fetch('/signup', { const response = await axios.post('/api/users/signup', user)
method: 'POST', console.log(response.data)
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(user)
})
const data = await response.json()
console.log(data)
} catch (error) { } catch (error) {
console.log(error) catchErrors(error, setError)
setError('다시 시도하세요.')
} }
} }
...@@ -64,12 +50,13 @@ function Signup() { ...@@ -64,12 +50,13 @@ function Signup() {
<Nav1 /> <Nav1 />
<Nav2 /> <Nav2 />
<Container className="my-5"> <Container className="my-5">
{error && <Alert variant='danger'>
{error}
</Alert>}
<Row className="justify-content-center"> <Row className="justify-content-center">
<Col md={6} xs={10} className="border" style={{ background: '#F7F3F3' }}> <Col md={6} xs={10} className="border" style={{ background: '#F7F3F3' }}>
<h2 className="text-center mt-5">Sign Up</h2> <h2 className="text-center mt-5">Sign Up</h2>
{error && <Alert variant='danger'>
{error}
</Alert>}
<Form <Form
noValidate validated={validated} noValidate validated={validated}
onSubmit={handleSubmit} onSubmit={handleSubmit}
......
function catchErrors(error,displayError){
let errorMsg
if(error.response){
errorMsg=error.response.data
console.log(errorMsg)
}else if(error.request){
errorMsg=error.request
console.log(errorMsg)
}else{
errorMsg=error.message
console.log(errorMsg)
}
displayError(errorMsg)
}
export default catchErrors
\ No newline at end of file
...@@ -7,15 +7,22 @@ const signup = async (req, res) => { ...@@ -7,15 +7,22 @@ const signup = async (req, res) => {
const { name, number1, number2, id, password, password2, tel } = req.body const { name, number1, number2, id, password, password2, tel } = req.body
try { try {
if(!isLength(password,{min:8, max:15})){ if(!isLength(password,{min:8, max:15})){
return res.status(422).json({message: '비밀번호는 8-15자리로 입력해주세요.'}) return res.status(422).send('비밀번호는 8-15자리로 입력해주세요.')
} }
const user=await User.findOne({id})
if(user){
return res.status(422).send(`${id}가 이미 사용중입니다.`)
}
const hash=await bcrypt.hash(password,10)
const newUser = await new User ({ const newUser = await new User ({
name, name,
number1, number1,
number2, number2,
id, id,
password, password:hash,
password2, password2:hash,
tel tel
}).save() }).save()
console.log(newUser) console.log(newUser)
...@@ -23,12 +30,9 @@ const signup = async (req, res) => { ...@@ -23,12 +30,9 @@ const signup = async (req, res) => {
} catch (error) { } catch (error) {
console.log(error) console.log(error)
res.status(500).json({message:'죄송합니다. 다시 입력해 주십시오.'}) res.status(500).send('죄송합니다. 다시 입력해 주십시오.')
} }
} }
const hello = (req, res) => {
res.send('Hello from users contriller')
}
export default { signup, hello } export default signup
\ No newline at end of file \ No newline at end of file
...@@ -5,6 +5,5 @@ const router = express.Router() ...@@ -5,6 +5,5 @@ const router = express.Router()
router.route('/signup') router.route('/signup')
.post(userCtrl.signup) .post(userCtrl.signup)
.get(userCtrl.hello)
export default router export default router
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment