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

const signup = async (req, res) => {
이재연's avatar
zz    
이재연 committed
7
    
이재연's avatar
이재연 committed
8
    const { name, number1, number2, id, password,  tel } = req.body
이재연's avatar
zz    
이재연 committed
9
10
    
    console.log(req.body)
Kim, Subin's avatar
Kim, Subin committed
11
    try {
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
12
        if (!isLength(password, { min: 8, max: 15 })) {
이재연's avatar
aa    
이재연 committed
13
            return res.status(422).send('비밀번호는 8-15자리로 입력해주세요.')
이재연's avatar
이재연 committed
14
        }
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
15
16
        const user = await User.findOne({ id })
        if (user) {
이재연's avatar
aa    
이재연 committed
17
18
19
            return res.status(422).send(`${id}가 이미 사용중입니다.`)
        }

Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
20
21
22
        const hash = await bcrypt.hash(password, 10)

        const newUser = await new User({
Kim, Subin's avatar
Kim, Subin committed
23
            name,
이재연's avatar
이재연 committed
24
25
            number1,
            number2,
이재연's avatar
이재연 committed
26
            id,
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
27
            password: hash,
이재연's avatar
이재연 committed
28
            tel,
Kim, Subin's avatar
Kim, Subin committed
29
        }).save()
30
        await new Cart({ userId: newUser._id }).save()
Kim, Subin's avatar
Kim, Subin committed
31
32
        console.log(newUser)
        res.json(newUser)
kusang96's avatar
kusang96 committed
33

Kim, Subin's avatar
Kim, Subin committed
34
35
    } catch (error) {
        console.log(error)
이재연's avatar
aa    
이재연 committed
36
        res.status(500).send('죄송합니다. 다시 입력해 주십시오.')
Kim, Subin's avatar
Kim, Subin committed
37
38
39
    }
}

40
export default { signup }