user.controller.js 3.43 KB
Newer Older
seoyeon's avatar
0726    
seoyeon committed
1
2
3
import { User } from '../models/index.js'
import jwt from 'jsonwebtoken'
import config from '../config/app.config.js'
seoyeon's avatar
0716    
seoyeon committed
4
5
import isLength from 'validator/lib/isLength.js'
import bcrypt from "bcryptjs";
Kim, Chaerin's avatar
Kim, Chaerin committed
6

이재연's avatar
aa    
이재연 committed
7
8
9
10
11
12
13
14
const multer = require('multer');
const uploadimg = multer({ dest: 'uploads/' });

const imgUpload = uploadimg.fields([
    { name: 'avatarUrl', maxCount: 1 }
])

const update = async (req, res) => {
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
15
  try {
이재연's avatar
aa    
이재연 committed
16
17
18
19
20
21
       const avatar = req.files['avatarUrl'][0]
       const avatarUrl = avatar.filename
       console.log(avatarUrl)
       await User.update({'img':avatarUrl},{where :{id:'9999'}} )
       const userId = await User.findOne({where : {id:'9999'}})
       return res.json(userId)
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
22
  } catch (error) {
이재연's avatar
aa    
이재연 committed
23
24
      console.log(error);
      res.status(500).send('이미지 업데이트 실패')
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
25
  }
이재연's avatar
aa    
이재연 committed
26
}
seoyeon's avatar
0727    
seoyeon committed
27
28
29
30
31
32
const getUser = async (req, res) => {
  console.log('유저가져왔다아아아ㅏㅇ:',req.body)
  console.log('id:',req.body.id)
  const id = req.body.id
  const user = await User.findOne({ where: { id: id } });
  console.log('user:',user)
우지원's avatar
0726    
우지원 committed
33
  req.json(user)
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
34
35
};

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

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
65
66
const signup = async (req, res) => {
  try {
seoyeon's avatar
0726    
seoyeon committed
67
68
69
70
71
72
73
74
75
    console.log('sign up= ', req.body)
    const { name, email, password, gender, 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) {
      const id = Math.floor(Math.random() * (9999 - 1000) + 1000)
      const Id = await User.findOne({ where: { id: id } })
seoyeon's avatar
0716    
seoyeon committed
76
    }
seoyeon's avatar
0726    
seoyeon committed
77
78

    const user = await User.findOne({ where: { email: email } })
seoyeon's avatar
0716    
seoyeon committed
79
    if (user)
seoyeon's avatar
0726    
seoyeon committed
80
      return res.status(422).send(`${email} 이미 존재하는 사용자입니다.`)
seoyeon's avatar
0716    
seoyeon committed
81

seoyeon's avatar
0726    
seoyeon committed
82
83
84
    if (!isLength(name, { min: 3, max: 10 })) {
      return res.status(422).send('이름은 3-10자 사이입니다')
    } else if (!isLength(password, { min: 6 })) {
seoyeon's avatar
0724    
seoyeon committed
85
      return res.status(422).send('비밀번호는 6자이상 입니다')
seoyeon's avatar
0726    
seoyeon committed
86
    } else if (!isLength(email, { min: 3, max: 10 })) {
seoyeon's avatar
0724    
seoyeon committed
87
88
      return res.status(422).send('아이디는 3-10자 사이입니다')
    }
seoyeon's avatar
0726    
seoyeon committed
89
90
    const newUser = await User.create({
      id: id,
seoyeon's avatar
0724    
seoyeon committed
91
92
      name: name,
      email: email,
seoyeon's avatar
0726    
seoyeon committed
93
      password: password,
seoyeon's avatar
0724    
seoyeon committed
94
95
      gender: gender,
      phone: phone,
seoyeon's avatar
0726    
seoyeon committed
96
97
98
    }).then(_ =>
      console.log('회원가입 정보', id, name, email, password, gender, phone),
    )
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
99
  } catch (error) {
seoyeon's avatar
0726    
seoyeon committed
100
101
    console.log(error)
    return res.status(500).send('회원가입 중 에러')
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
102
  }
seoyeon's avatar
0716    
seoyeon committed
103
}
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
104

seoyeon's avatar
seoyeon committed
105
106
107
108
109
const logout = (req, res) => {
  res.clearCookie('token')
  res.send('Logout Successful')
}

Kim, Chaerin's avatar
Kim, Chaerin committed
110
export default {
seoyeon's avatar
0727    
seoyeon committed
111
  getUser,
Kim, Chaerin's avatar
Kim, Chaerin committed
112
  login,
이재연's avatar
z    
이재연 committed
113
  signup,
seoyeon's avatar
seoyeon committed
114
  logout,
이재연's avatar
a    
이재연 committed
115
  imgUpload,update
seoyeon's avatar
0726    
seoyeon committed
116
}