user.controller.js 3.42 KB
Newer Older
우지원's avatar
우지원 committed
1
2
import User from "../models/User.js"
import isLength from 'validator/lib/isLength.js'
우지원's avatar
01_06    
우지원 committed
3
import isEmail from 'validator/lib/isEmail.js'
JeongYeonwoo's avatar
   
JeongYeonwoo committed
4
import bcrypt from "bcryptjs";
JeongYeonwoo's avatar
JeongYeonwoo committed
5
import multer from "multer";
Choi Ga Young's avatar
x    
Choi Ga Young committed
6
import AccessInfo from "../models/AccessInfo.js"
JeongYeonwoo's avatar
JeongYeonwoo committed
7
8
9
10
11

const upload = multer({ dest: 'uploads/' })
const profileUpload = upload.fields([
    { name: 'imageUrl', maxCount: 1 },
])
Choi Ga Young's avatar
Choi Ga Young committed
12

우지원's avatar
우지원 committed
13
const signup = async (req, res) => {
우지원's avatar
a    
우지원 committed
14
15

    const { username, nickname, email, password } = req.body
Choi Ga Young's avatar
x    
Choi Ga Young committed
16

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

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

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

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

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

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

Choi Ga Young's avatar
x    
Choi Ga Young committed
87
88
const checkUser = async (req, res) => {
    try {
Choi Ga Young's avatar
xxxxx    
Choi Ga Young committed
89
        const info = await User.findOne({ _id: req.query._id })
Choi Ga Young's avatar
x    
Choi Ga Young committed
90
91
92
93
94
95
96
        console.log('info 확인', info)
        return res.json(info)
    } catch (error) {
        console.log(error)
        res.status(500).send('사용자 검색 실패')
    }
}
우지원's avatar
0113    
우지원 committed
97

우지원's avatar
우지원 committed
98
99
100
101
102
103
104
105
const loginNavbar = async (req, res) => {
    try {
        let user = await User.findOne(req.body).select('username').exec()
        return res.json(user)
    } catch (error) {
        alert('올바르지 못한 접근입니다.')
    }
}
우지원's avatar
수정    
우지원 committed
106

Choi Ga Young's avatar
x    
Choi Ga Young committed
107
export default { signup, profileUpload, update, userById, getProfile, loginNavbar, checkUser }
108
// {} : 객체로 return함