user.controller.js 1.52 KB
Newer Older
Kim, Subin's avatar
Kim, Subin committed
1
import User from "../models/User.js";
kusang96's avatar
kusang96 committed
2
3
import isLength from 'validator/lib/isLength.js';
import bcrypt from 'bcryptjs';
Kim, Subin's avatar
Kim, Subin committed
4
5

const signup = async (req, res) => {
이재연's avatar
이재연 committed
6
7
8
    console.log(req.body)
    console.log('req.body.name=', req.body.name)
    const { name, number1, number2, id, password, password2, tel } = req.body
Kim, Subin's avatar
Kim, Subin committed
9
    try {
kusang96's avatar
kusang96 committed
10
11
        if (!isLength(password, { min: 8, max: 15 })) {
            return res.status(422).json({ message: '비밀번호는 8-15자리로 입력해주세요.' })
이재연's avatar
이재연 committed
12
        }
kusang96's avatar
kusang96 committed
13
14
15
16
17
18
19
20
21
22
23
        // if (!isLength(name, { min: 3, max: 10 })) {
        //     return res.status(422).send('이름은 3-10자로 입력해주세요.')
        // } else if (!isLength(password, { min: 8, max: 15 })){
        //     return res.status(422).json({message: '비밀번호는 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({
Kim, Subin's avatar
Kim, Subin committed
24
            name,
이재연's avatar
이재연 committed
25
26
            number1,
            number2,
이재연's avatar
이재연 committed
27
            id,
kusang96's avatar
kusang96 committed
28
            password: hash,
이재연's avatar
이재연 committed
29
30
            password2,
            tel
Kim, Subin's avatar
Kim, Subin committed
31
32
33
        }).save()
        console.log(newUser)
        res.json(newUser)
kusang96's avatar
kusang96 committed
34

Kim, Subin's avatar
Kim, Subin committed
35
36
    } catch (error) {
        console.log(error)
kusang96's avatar
kusang96 committed
37
        res.status(500).json({ message: '죄송합니다. 다시 입력해 주십시오.' })
Kim, Subin's avatar
Kim, Subin committed
38
39
40
41
42
43
44
    }
}

const hello = (req, res) => {
    res.send('Hello from users contriller')
}

kusang96's avatar
kusang96 committed
45
export default { signup, hello }