user.controller.js 1.34 KB
Newer Older
Kim, Chaerin's avatar
Kim, Chaerin committed
1
2
3
import { User } from "../models/index.js";
import config from "../config/app.config.js";

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
4
5
6
7
8
9
10
11
12
13
const test = async (req, res) => {
  try {
    console.log(req);
    res.json("안녕");
  } catch (error) {
    console.log(error);
    return res.status(500).send("test 중 에러");
  }
};

Kim, Chaerin's avatar
Kim, Chaerin committed
14
15
16
17
const login = async (req, res) => {
  try {
    console.log("login= ", req.body);
    const { email, password } = req.body;
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
18
    const user = await User.findOne({ where: { email: email } });
Kim, Chaerin's avatar
Kim, Chaerin committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    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
42
43
44
45
46
47
48
49
50
const signup = async (req, res) => {
  try {
    console.log("sign up= ", req.body);
  } catch (error) {
    console.log(error);
    return res.status(500).send("회원가입 중 에러");
  }
};

Kim, Chaerin's avatar
Kim, Chaerin committed
51
export default {
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
52
  test,
Kim, Chaerin's avatar
Kim, Chaerin committed
53
  login,
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
54
  signup,
Kim, Chaerin's avatar
Kim, Chaerin committed
55
};