user.controller.js 2.62 KB
Newer Older
Kim, Chaerin's avatar
Kim, Chaerin committed
1
import { User } from "../models/index.js";
Kim, Chaerin's avatar
a    
Kim, Chaerin committed
2
import jwt from "jsonwebtoken";
Kim, Chaerin's avatar
Kim, Chaerin committed
3
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
}
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
27

Kim, Chaerin's avatar
Kim, Chaerin committed
28
29
30
31
const login = async (req, res) => {
  try {
    console.log("login= ", req.body);
    const { email, password } = req.body;
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
32
    const user = await User.findOne({ where: { email: email } });
Kim, Chaerin's avatar
Kim, Chaerin committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    if (!user)
      return res.status(422).send(`${email} 사용자가 존재하지 않습니다.`);

    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("비밀번호가 일치하지 않습니다.");
    }
  } catch (error) {
    console.log(error);
    return res.status(500).send("로그인 중 에러");
  }
};

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
56
57
const signup = async (req, res) => {
  try {
seoyeon's avatar
0716    
seoyeon committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    console.log('signup= ', req.body);
    const { name, password, id } = req.body; 
    if (!isLength(name, {min: 3, max: 10})) {
      return res.status(422).send('이름은 3-10자 사이입니다')
    } else if (!isLength(password, {min: 6})) {
      return res.status(422).send('비밀번호는 6자 이상입니다')
    } else if (!isLength(id, {min:3, max10})) {
      return res.status(422).send('아이디는 3-10자 사이입니다')
    }
    const user = await User.scope("password").findOne({ where: email });
    if (user)
      return res.status(422).send(`${email} 이미 존재하는 사용자입니다/+ `);

    const hash = await bcrypt.hash(password, 10)
    const newUser = await new User ({
      name,
      password: hash,
      id
    }).save()
    console.log(newUser)
    res.json(newUser)
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
79
80
  } catch (error) {
    console.log(error);
seoyeon's avatar
0716    
seoyeon committed
81
    return res.status(500).send("회원가입 중 에러")
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
82
  }
seoyeon's avatar
0716    
seoyeon committed
83
}
Kim, Chaerin's avatar
Kim, Chaerin committed
84
85
export default {
  login,
이재연's avatar
z    
이재연 committed
86
87
88
  signup,
  update,
  imgUpload
Kim, Chaerin's avatar
Kim, Chaerin committed
89
};