user.controller.js 2.69 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

seoyeon's avatar
0727    
seoyeon committed
7
const getUser = async (req, res) => {
우지원's avatar
0728    
우지원 committed
8
9
10
  // console.log('req.params:',req.params)
  const user = await User.findOne({ where: { id: req.params.id } });
  // console.log('user:',user)
우지원's avatar
0727    
우지원 committed
11
  res.json(user)
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
12
13
};

Kim, Chaerin's avatar
Kim, Chaerin committed
14
15
const login = async (req, res) => {
  try {
우지원's avatar
0728    
우지원 committed
16
    // console.log('login= ', req.body)
seoyeon's avatar
0726    
seoyeon committed
17
18
    const { email, password } = req.body
    const user = await User.findOne({ where: { email: email } })
seoyeon's avatar
0726    
seoyeon committed
19
    if (!user) {
seoyeon's avatar
0726    
seoyeon committed
20
      return res.status(422).send(`${email} 사용자가 존재하지 않습니다.`)
Kim, Chaerin's avatar
Kim, Chaerin committed
21
    } else {
seoyeon's avatar
0726    
seoyeon committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
      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
36
37
    }
  } catch (error) {
우지원's avatar
0728    
우지원 committed
38
    // console.log(error)
seoyeon's avatar
0726    
seoyeon committed
39
    return res.status(500).send('로그인 중 에러')
Kim, Chaerin's avatar
Kim, Chaerin committed
40
  }
seoyeon's avatar
0726    
seoyeon committed
41
}
Kim, Chaerin's avatar
Kim, Chaerin committed
42

seoyeon's avatar
0716    
seoyeon committed
43
44
const signup = async (req, res) => {
  try {
우지원's avatar
0728    
우지원 committed
45
    // console.log('sign up= ', req.body)
seoyeon's avatar
0726    
seoyeon committed
46
47
    const { name, email, password, gender, phone } = req.body
    const id = Math.floor(Math.random() * (9999 - 1000) + 1000)
우지원's avatar
0728    
우지원 committed
48
    // console.log('id:', id)
seoyeon's avatar
0726    
seoyeon committed
49
    const Id = await User.findOne({ where: { id: id } })
우지원's avatar
0728    
우지원 committed
50
    // console.log('Id 중복확인:', Id)
seoyeon's avatar
0726    
seoyeon committed
51
52
53
54
55
56
    while (Id) {
      const id = Math.floor(Math.random() * (9999 - 1000) + 1000)
      const Id = await User.findOne({ where: { id: id } })
    }

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

seoyeon's avatar
0726    
seoyeon committed
60
61
62
    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
63
      return res.status(422).send('비밀번호는 6자이상 입니다')
seoyeon's avatar
0726    
seoyeon committed
64
    } else if (!isLength(email, { min: 3, max: 10 })) {
seoyeon's avatar
0724    
seoyeon committed
65
66
      return res.status(422).send('아이디는 3-10자 사이입니다')
    }
seoyeon's avatar
0726    
seoyeon committed
67
68
    const newUser = await User.create({
      id: id,
seoyeon's avatar
0724    
seoyeon committed
69
70
      name: name,
      email: email,
seoyeon's avatar
0726    
seoyeon committed
71
      password: password,
seoyeon's avatar
0724    
seoyeon committed
72
73
      gender: gender,
      phone: phone,
우지원's avatar
0728    
우지원 committed
74
    })
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
75
  } catch (error) {
seoyeon's avatar
0726    
seoyeon committed
76
77
    console.log(error)
    return res.status(500).send('회원가입 중 에러')
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
78
  }
seoyeon's avatar
0726    
seoyeon committed
79
}
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
80

seoyeon's avatar
seoyeon committed
81
82
83
84
85
const logout = (req, res) => {
  res.clearCookie('token')
  res.send('Logout Successful')
}

Kim, Chaerin's avatar
Kim, Chaerin committed
86
export default {
seoyeon's avatar
0727    
seoyeon committed
87
  getUser,
Kim, Chaerin's avatar
Kim, Chaerin committed
88
  login,
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
89
  signup,
seoyeon's avatar
seoyeon committed
90
  logout,
seoyeon's avatar
0726    
seoyeon committed
91
}