user.controller.js 2.83 KB
Newer Older
우지원's avatar
우지원 committed
1
2
3
import User from "../models/User.js"
import isLength from 'validator/lib/isLength.js'
import isEmail from 'validator/lib/isEmail.js'
JeongYeonwoo's avatar
   
JeongYeonwoo committed
4
5
6
import bcrypt from "bcryptjs";
import jwt from 'jsonwebtoken'
import config from "../config.js"
JeongYeonwoo's avatar
JeongYeonwoo committed
7
8
9
10
11
12
13
14
import multer from "multer";
import catchErrors from "../../client/src/utils/catchErrors.js";

const upload = multer({ dest: 'uploads/' })
const profileUpload = upload.fields([
    { name: 'imageUrl', maxCount: 1 },
])

우지원's avatar
우지원 committed
15
16

const signup = async (req, res) => {
우지원's avatar
a    
우지원 committed
17
18

    const { username, nickname, email, password } = req.body
우지원's avatar
우지원 committed
19
20

    try {
JeongYeonwoo's avatar
JeongYeonwoo committed
21
        if (!isLength(username, { min: 3, max: 10 })) {
우지원's avatar
01_06    
우지원 committed
22
            return res.status(422).send('이름은 3-10자 사이입니다')
JeongYeonwoo's avatar
JeongYeonwoo committed
23
        } else if (!isLength(nickname, { min: 2, max: 10 })) {
우지원's avatar
01_06    
우지원 committed
24
            return res.status(422).send('별명은 2-10자 사이입니다.')
JeongYeonwoo's avatar
JeongYeonwoo committed
25
        } else if (!isLength(password, { min: 6 })) {
우지원's avatar
01_06    
우지원 committed
26
27
28
            return res.status(422).send('비밀번호는 6자 이상입니다.')
        } else if (!isEmail(email)) {
            return res.status(422).send('유효하지 않은 이메일 형식입니다')
JeongYeonwoo's avatar
JeongYeonwoo committed
29
        }
우지원's avatar
01_06    
우지원 committed
30

JeongYeonwoo's avatar
JeongYeonwoo committed
31
32
        const user = await User.findOne({ email })
        if (user) {
우지원's avatar
01_06    
우지원 committed
33
            return res.status(422).send(`${email}이 이미 사용중입니다.`)
우지원's avatar
우지원 committed
34
35
        }

우지원's avatar
01_06    
우지원 committed
36
37
        const hash = await bcrypt.hash(password, 10)

우지원's avatar
우지원 committed
38
        const newUser = await new User({
우지원's avatar
a    
우지원 committed
39
            username,
우지원's avatar
우지원 committed
40
41
            nickname,
            email,
42
            password: hash,
우지원's avatar
우지원 committed
43
44
45
46
47
        }).save()
        res.json(newUser)
    } catch (error) {
        //알수없는 모든 에러발생시 처리
        console.log(error)
48
        res.status(500).send('회원가입 에러발생하였습니다.')
우지원's avatar
우지원 committed
49
50
    }
}
JeongYeonwoo's avatar
   
JeongYeonwoo committed
51

JeongYeonwoo's avatar
JeongYeonwoo committed
52
53
54
const update = async (req, res) => {
    const username = req.user.username
    const newNickname = req.body.newNickname
JeongYeonwoo's avatar
JeongYeonwoo committed
55
    try {
JeongYeonwoo's avatar
JeongYeonwoo committed
56
57
58
59
60
61
        await User.updateOne({ 'username': username }, { 'nickname': newNickname })
        const imageUrl = req.files['imageUrl'][0]
        const user = req.user
        user.profileimg = imageUrl.filename //우리가 사용할 uesr.profileimg에다가 imageUrl의 filename을 저장
        const updatedUser = await user.save()
        res.json(updatedUser)
JeongYeonwoo's avatar
JeongYeonwoo committed
62
    } catch (error) {
JeongYeonwoo's avatar
JeongYeonwoo committed
63
64
        console.log(error)
        res.status(500).send('프로필 업데이트 실패')
JeongYeonwoo's avatar
JeongYeonwoo committed
65
66
    }
}
JeongYeonwoo's avatar
JeongYeonwoo committed
67
68

const userById = async (req, res, next, id) => {    //id로 찾아와서 req.user를 우리가 찾는 user로 저장
JeongYeonwoo's avatar
JeongYeonwoo committed
69
    try {
JeongYeonwoo's avatar
JeongYeonwoo committed
70
71
72
73
74
75
        const user = await User.findById(id)
        if (!user) {
            res.status(404).send('사용자를 찾을 수 없습니다.')
        }
        req.user = user
        next()
JeongYeonwoo's avatar
JeongYeonwoo committed
76
    } catch (error) {
JeongYeonwoo's avatar
JeongYeonwoo committed
77
78
        console.log(error)
        res.status(500).send('사용자 아이디 검색 실패')
JeongYeonwoo's avatar
JeongYeonwoo committed
79
    }
JeongYeonwoo's avatar
   
JeongYeonwoo committed
80
}
JeongYeonwoo's avatar
JeongYeonwoo committed
81
82
83
84
const getProfile = (req, res) => {
    res.json(req.user)
}

JeongYeonwoo's avatar
   
JeongYeonwoo committed
85
86


JeongYeonwoo's avatar
JeongYeonwoo committed
87
export default { signup, profileUpload, update, userById, getProfile }
우지원's avatar
우지원 committed
88
// {} : 객체로 return함