user.controller.js 2.56 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';
박상호's avatar
qwe    
박상호 committed
4
5
6
7
8
9
10
import multer from "multer";

const upload = multer({ dest: 'uploads/' }) //multer 홈페이지 참고 // uploads가 어디인지 dest에 저장함.

const profileUpload = upload.fields([ //.single 한개, .fileds 여러개
    { name: 'avatar', maxCount: 1 },
])
Kim, Subin's avatar
Kim, Subin committed
11
12

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

Kim, Subin's avatar
Kim, Subin committed
42
43
    } catch (error) {
        console.log(error)
kusang96's avatar
kusang96 committed
44
        res.status(500).json({ message: '죄송합니다. 다시 입력해 주십시오.' })
Kim, Subin's avatar
Kim, Subin committed
45
46
47
    }
}

박상호's avatar
qwe    
박상호 committed
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

const update = async (req, res) => {
    try {
        const { name } = req.body
        const avatar = req.files['avatar'][0]
        const user = req.profile
        user.avatarUrl = avatar.filename
        const updateUser = await user.save()
        res.json(updateUser)
    } catch (error) {
        console.log(error);
        res.status(500).send('프로파일 업데이트 실패')
    }
}

const getProfile = (req, res) => {
    res.json(req.profile)
}

const userById = async (req, res, next, id) => {
    try {
        const user = await User.findById(id)
        if (!user) {
            res.status(404).send('사용자를 찾을 수 없습니다')
        }
        req.profile = user
        next()
    } catch (error) {
        console.log(error);
        res.status(500).send('사용자 아이디 검색 실패')
    }
Kim, Subin's avatar
Kim, Subin committed
79
80
}

박상호's avatar
qwe    
박상호 committed
81
export default { signup, profileUpload, update, getProfile, userById}