user.controller.js 3.63 KB
Newer Older
이재연's avatar
오잉    
이재연 committed
1
2
3
4
import { User } from "../models/index.js";
import jwt from "jsonwebtoken";
import config from "../config/app.config.js";
import isLength from "validator/lib/isLength.js";
Kim, Chaerin's avatar
Kim, Chaerin committed
5

이재연's avatar
오잉    
이재연 committed
6
7
const multer = require("multer");
const uploadimg = multer({ dest: "uploads/" });
이재연's avatar
aa    
이재연 committed
8

이재연's avatar
오잉    
이재연 committed
9
const imgUpload = uploadimg.fields([{ name: "img", maxCount: 1 }]);
이재연's avatar
aa    
이재연 committed
10
11

const update = async (req, res) => {
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
12
  try {
이재연's avatar
오잉    
이재연 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) {
이재연's avatar
오잉    
이재연 committed
21
22
    console.log(error);
    res.status(500).send("이미지 업데이트 실패");
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
23
  }
이재연's avatar
오잉    
이재연 committed
24
};
seoyeon's avatar
0727    
seoyeon committed
25
const getUser = async (req, res) => {
우지원's avatar
0728    
우지원 committed
26
  const user = await User.findOne({ where: { id: req.params.id } });
우지원's avatar
0727    
우지원 committed
27
  res.json(user)
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
28
29
};

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

seoyeon's avatar
0716    
seoyeon committed
79
80
const signup = async (req, res) => {
  try {
우지원's avatar
0728    
우지원 committed
81
    // console.log('sign up= ', req.body)
seoyeon's avatar
0726    
seoyeon committed
82
83
    const { name, email, password, gender, phone } = req.body
    const id = Math.floor(Math.random() * (9999 - 1000) + 1000)
우지원's avatar
0728    
우지원 committed
84
    // console.log('id:', id)
seoyeon's avatar
0726    
seoyeon committed
85
    const Id = await User.findOne({ where: { id: id } })
우지원's avatar
0728    
우지원 committed
86
    // console.log('Id 중복확인:', Id)
seoyeon's avatar
0726    
seoyeon committed
87
    while (Id) {
이재연's avatar
오잉    
이재연 committed
88
89
      const id = Math.floor(Math.random() * (9999 - 1000) + 1000);
      const Id = await User.findOne({ where: { id: id } });
seoyeon's avatar
0726    
seoyeon committed
90
91
    }

이재연's avatar
오잉    
이재연 committed
92
    const user = await User.findOne({ where: { email: email } });
seoyeon's avatar
0724    
seoyeon committed
93
    if (user)
이재연's avatar
오잉    
이재연 committed
94
      return res.status(422).send(`${email} 이미 존재하는 사용자입니다.`);
seoyeon's avatar
0724    
seoyeon committed
95

seoyeon's avatar
0726    
seoyeon committed
96
    if (!isLength(name, { min: 3, max: 10 })) {
이재연's avatar
오잉    
이재연 committed
97
      return res.status(422).send("이름은 3-10자 사이입니다");
seoyeon's avatar
0726    
seoyeon committed
98
    } else if (!isLength(password, { min: 6 })) {
이재연's avatar
오잉    
이재연 committed
99
      return res.status(422).send("비밀번호는 6자이상 입니다");
seoyeon's avatar
0726    
seoyeon committed
100
    } else if (!isLength(email, { min: 3, max: 10 })) {
이재연's avatar
오잉    
이재연 committed
101
      return res.status(422).send("아이디는 3-10자 사이입니다");
seoyeon's avatar
0724    
seoyeon committed
102
    }
seoyeon's avatar
0726    
seoyeon committed
103
104
    const newUser = await User.create({
      id: id,
seoyeon's avatar
0724    
seoyeon committed
105
106
      name: name,
      email: email,
seoyeon's avatar
0726    
seoyeon committed
107
      password: password,
seoyeon's avatar
0724    
seoyeon committed
108
109
      gender: gender,
      phone: phone,
우지원's avatar
0728    
우지원 committed
110
    })
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
111
  } catch (error) {
이재연's avatar
오잉    
이재연 committed
112
113
    console.log(error);
    return res.status(500).send("회원가입 중 에러");
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
114
  }
이재연's avatar
오잉    
이재연 committed
115
};
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
116

seoyeon's avatar
seoyeon committed
117
const logout = (req, res) => {
이재연's avatar
오잉    
이재연 committed
118
119
120
  res.clearCookie("token");
  res.send("Logout Successful");
};
seoyeon's avatar
seoyeon committed
121

Kim, Chaerin's avatar
Kim, Chaerin committed
122
export default {
seoyeon's avatar
0727    
seoyeon committed
123
  getUser,
Kim, Chaerin's avatar
Kim, Chaerin committed
124
  login,
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
125
  signup,
seoyeon's avatar
seoyeon committed
126
  logout,
이재연's avatar
오잉    
이재연 committed
127
128
129
130
  imgUpload,
  update,
  updateinfo,
};