user.controller.js 3.49 KB
Newer Older
seoyeon's avatar
0804    
seoyeon committed
1
2
3
4
5
import { User } from '../models/index.js'
import jwt from 'jsonwebtoken'
import config from '../config/app.config.js'
import isLength from 'validator/lib/isLength.js'
import multer from 'multer'
Kim, Chaerin's avatar
Kim, Chaerin committed
6

seoyeon's avatar
0804    
seoyeon committed
7
const uploadimg = multer({ dest: 'uploads/' })
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
8

seoyeon's avatar
0804    
seoyeon committed
9
const imgUpload = uploadimg.fields([{ name: 'img', maxCount: 1 }])
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
10
11

const update = async (req, res) => {
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
12
  try {
seoyeon's avatar
0804    
seoyeon committed
13
14
15
16
17
18
19
    console.log('id:', req.body.id)
    const id = req.body.id
    const avatar = req.files['img'][0]
    const img = avatar.filename
    console.log(img)
    await User.update({ img: img }, { where: { id: id } })
    res.json(img)
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
20
  } catch (error) {
seoyeon's avatar
0804    
seoyeon committed
21
22
    console.log(error)
    res.status(500).send('이미지 업데이트 실패')
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
23
  }
seoyeon's avatar
0804    
seoyeon committed
24
}
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
25
const getUser = async (req, res) => {
seoyeon's avatar
0804    
seoyeon committed
26
  const user = await User.findOne({ where: { id: req.params.id } })
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
27
  res.json(user)
seoyeon's avatar
0804    
seoyeon committed
28
}
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
29

Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
30
const updateinfo = async (req, res) => {
seoyeon's avatar
0804    
seoyeon committed
31
32
33
  console.log(req.body)
  const { id, name, email, phone, img } = req.body
  console.log('id:', id)
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
34
35
36
37
38
  const A = {
    name: name,
    email: email,
    phone: phone,
    img: img,
seoyeon's avatar
0804    
seoyeon committed
39
40
41
  }
  await User.update(A, { where: { id: id } })
  const user = await User.findOne({ where: { id: id } })
Kim, Chaerin's avatar
Kim, Chaerin committed
42
  console.log('user:', user)
seoyeon's avatar
0804    
seoyeon committed
43
}
Kim, Chaerin's avatar
Kim, Chaerin committed
44
45
const login = async (req, res) => {
  try {
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
46
47
48
    const { email, password } = req.body
    const user = await User.findOne({ where: { email: email } })
    if (!user) {
seoyeon's avatar
0804    
seoyeon committed
49
      return res.status(422).send(`${email} 사용자가 존재하지 않습니다.`)
Kim, Chaerin's avatar
Kim, Chaerin committed
50
    } else {
seoyeon's avatar
0804    
seoyeon committed
51
      const passworMatch = await user.comparePassword(password)
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
52
53
54
      if (passworMatch) {
        const token = jwt.sign({ userID: user.id }, config.jwtSecret, {
          expiresIn: config.jwtExpires,
seoyeon's avatar
0804    
seoyeon committed
55
        })
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
56
        res.cookie(config.cookieName, token, {
seoyeon's avatar
0804    
seoyeon committed
57
          path: '/',
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
58
59
          httpOnly: true,
          secure: true,
seoyeon's avatar
0804    
seoyeon committed
60
61
        })
        res.json(user)
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
62
      } else {
seoyeon's avatar
0804    
seoyeon committed
63
        res.status(401).send('비밀번호가 일치하지 않습니다.')
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
64
      }
Kim, Chaerin's avatar
Kim, Chaerin committed
65
66
    }
  } catch (error) {
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
67
    return res.status(500).send('로그인 중 에러')
Kim, Chaerin's avatar
Kim, Chaerin committed
68
  }
seoyeon's avatar
0804    
seoyeon committed
69
}
Kim, Chaerin's avatar
Kim, Chaerin committed
70

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
71
const signup = async (req, res) => {
Kim, Chaerin's avatar
Kim, Chaerin committed
72
73
74
75
76
77
78
  console.log('sign up= ', req.body)
  const { name, email, password, phone } = req.body
  const id = Math.floor(Math.random() * (9999 - 1000) + 1000)
  // console.log('id:', id)
  const Id = await User.findOne({ where: { id: id } })
  // console.log('Id 중복확인:', Id)
  while (Id) {
seoyeon's avatar
0804    
seoyeon committed
79
80
    const id = Math.floor(Math.random() * (9999 - 1000) + 1000)
    const Id = await User.findOne({ where: { id: id } })
Kim, Chaerin's avatar
Kim, Chaerin committed
81
82
  }
  try {
seoyeon's avatar
0804    
seoyeon committed
83
    const user = await User.findOne({ where: { email: email } })
Kim, Chaerin's avatar
Kim, Chaerin committed
84
    if (user) {
seoyeon's avatar
0804    
seoyeon committed
85
      return res.status(422).send(`${email} 이미 존재하는 사용자입니다.`)
Kim, Chaerin's avatar
Kim, Chaerin committed
86
87
    } else {
      if (!isLength(name, { min: 2, max: 10 })) {
seoyeon's avatar
0804    
seoyeon committed
88
        return res.status(422).send('이름은 2-10자 사이입니다')
Kim, Chaerin's avatar
Kim, Chaerin committed
89
      } else if (!isLength(email, { min: 3, max: 10 })) {
seoyeon's avatar
0804    
seoyeon committed
90
91
92
        return res.status(422).send('아이디는 3-10자 사이입니다')
      } else if (!isLength(password, { min: 6 })) {
        return res.status(422).send('비밀번호는 6자이상 입니다')
Kim, Chaerin's avatar
Kim, Chaerin committed
93
94
95
96
97
98
99
100
      }
      const newUser = {
        id: id,
        name: name,
        email: email,
        password: password,
        phone: phone,
      }
seoyeon's avatar
0804    
seoyeon committed
101
      console.log('newUser:', newUser)
Kim, Chaerin's avatar
Kim, Chaerin committed
102
103
      await User.create(newUser)
      res.json(true)
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
104
    }
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
105
  } catch (error) {
seoyeon's avatar
0804    
seoyeon committed
106
107
    console.log(error)
    return res.status(500).send('회원가입 중 에러')
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
108
  }
seoyeon's avatar
0804    
seoyeon committed
109
}
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
110
111

const logout = (req, res) => {
seoyeon's avatar
0804    
seoyeon committed
112
113
114
  res.clearCookie('token')
  res.send('Logout Successful')
}
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
115

Kim, Chaerin's avatar
Kim, Chaerin committed
116
export default {
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
117
  getUser,
Kim, Chaerin's avatar
Kim, Chaerin committed
118
  login,
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
119
  signup,
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
120
121
122
123
  logout,
  imgUpload,
  update,
  updateinfo,
seoyeon's avatar
0804    
seoyeon committed
124
}