user.controller.js 2.61 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
0726    
우지원 committed
7
8
9
10
const user = async (req, res) => {
  console.log(req.body)
  const user = await User.findAll({ where: { id: req.body } });
  req.json(user)
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
11
12
};

Kim, Chaerin's avatar
Kim, Chaerin committed
13
14
const login = async (req, res) => {
  try {
seoyeon's avatar
0726    
seoyeon committed
15
16
17
    console.log('login= ', req.body)
    const { email, password } = req.body
    const user = await User.findOne({ where: { email: email } })
Kim, Chaerin's avatar
Kim, Chaerin committed
18
    if (!user)
seoyeon's avatar
0726    
seoyeon committed
19
      return res.status(422).send(`${email} 사용자가 존재하지 않습니다.`)
Kim, Chaerin's avatar
Kim, Chaerin committed
20

seoyeon's avatar
0726    
seoyeon committed
21
    const passworMatch = await user.comparePassword(password)
Kim, Chaerin's avatar
Kim, Chaerin committed
22
23
24
    if (passworMatch) {
      const token = jwt.sign({ userID: user.id }, config.jwtSecret, {
        expiresIn: config.jwtExpires,
seoyeon's avatar
0726    
seoyeon committed
25
      })
Kim, Chaerin's avatar
Kim, Chaerin committed
26
      res.cookie(config.cookieName, token, {
seoyeon's avatar
0726    
seoyeon committed
27
        path: '/',
Kim, Chaerin's avatar
Kim, Chaerin committed
28
29
        httpOnly: true,
        secure: true,
seoyeon's avatar
0726    
seoyeon committed
30
31
      })
      res.json({ user })
Kim, Chaerin's avatar
Kim, Chaerin committed
32
    } else {
seoyeon's avatar
0726    
seoyeon committed
33
      res.status(401).send('비밀번호가 일치하지 않습니다.')
Kim, Chaerin's avatar
Kim, Chaerin committed
34
35
    }
  } catch (error) {
seoyeon's avatar
0726    
seoyeon committed
36
37
    console.log(error)
    return res.status(500).send('로그인 중 에러')
Kim, Chaerin's avatar
Kim, Chaerin committed
38
  }
seoyeon's avatar
0726    
seoyeon committed
39
}
Kim, Chaerin's avatar
Kim, Chaerin committed
40

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
41
42
const signup = async (req, res) => {
  try {
seoyeon's avatar
0726    
seoyeon committed
43
44
45
46
47
48
49
50
51
    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
52
    }
seoyeon's avatar
0726    
seoyeon committed
53
54

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

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

seoyeon's avatar
0726    
seoyeon committed
82

Kim, Chaerin's avatar
Kim, Chaerin committed
83
export default {
우지원's avatar
0726    
우지원 committed
84
  user,
Kim, Chaerin's avatar
Kim, Chaerin committed
85
  login,
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
86
  signup,
seoyeon's avatar
0726    
seoyeon committed
87
}