user.controller.js 2.54 KB
Newer Older
Choi Ga Young's avatar
Choi Ga Young committed
1
2
import jwt from "jsonwebtoken";
import { User } from '../db/index.js';
3
import config from "../config/app.config.js";
Choi Ga Young's avatar
Choi Ga Young committed
4

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const getUser = async (req, res) => {
  try {
    if (req.cookies.todayku) {
      const token = req.cookies.todayku;
      const { id, role, name } = jwt.verify(token, config.jwtSecret);
      res.json({ id, role, name });
    } else {
      res.json({ id: "", role: "user", name: "" });
    }
  } catch (error) {
    console.error(error);
    return res.status(500).send("유저를 가져오지 못했습니다.");
  }
}


Choi Ga Young's avatar
Choi Ga Young committed
21
22
23
24
25
26
27
28
29
30
31
32
const signup = async (req, res) => {
  console.log('server/signup req.body', req.body)
  const { userId, password, userName, userStudNum } = req.body;
  try {
    const findId = await User.findOne({ where: { userID: userId } });
    if (findId) {
      throw new Error("이미 있는 회원정보 입니다.");
    }
    await User.create({
      userID: userId,
      password: password,
      userName: userName,
33
      studNum: userStudNum,
34
      role: "user"
Choi Ga Young's avatar
Choi Ga Young committed
35
36
37
38
    });
    res.status(201).json("success")
  } catch (error) {
    console.log(error)
39
    return res.status(500).send(error.message || "회원가입 에러발생")
Choi Ga Young's avatar
Choi Ga Young committed
40
41
42
43
44
  }
}

const login = async (req, res) => {
  console.log('server/login req.body', req.body)
45
46
47
48
49
50
51
52
53
54
55
  const { userId, password } = req.body;
  try {
    const user = await User.scope("withPassword").findOne({ where: { userID: userId } });
    console.log('user확인', user)
    if (!user) {
      return res.status(404).send(`일치하는 정보가 없습니다.`);
    }
    const passwordMatch = await user.comparePassword(password);
    if (passwordMatch) {

      const signData = {
56
        id: user.id,
57
58
59
        role: "user",
        name: user.userName,
      };
60
61
62
63
64
65
66
67
68
69
70
71

      const token = jwt.sign(signData, config.jwtSecret, {
        expiresIn: config.jwtExpires,
      });

      res.cookie(config.cookieName, token, {
        maxAge: config.cookieMaxAge,
        path: "/",
        httpOnly: config.env === "production",
        secure: config.env === "production",
      });

72
73
      res.status(201).json(signData)
    } else {
74
75
76
77
78
79
80
      res.status(401).send("비밀번호가 일치하지 않습니다.")
    }

  } catch (error) {
    console.log(error)
    return res.status(500).send("로그인 에러발생")
  }
Choi Ga Young's avatar
Choi Ga Young committed
81
82
}

83
84
85
86
87
88
89
90
91
92
93
94
95
96
const logout = async (req, res) => {
  try {
    res.clearCookie(config.cookieName);
    res.json({
      id:"",
      role:"user",
      name:""
    })
  } catch (error) {
    console.log(error);
    return res.status(500).send("로그아웃 에러발생")
  }
}

Choi Ga Young's avatar
Choi Ga Young committed
97
export default {
98
  getUser,
Choi Ga Young's avatar
Choi Ga Young committed
99
  signup,
100
101
  login,
  logout
Choi Ga Young's avatar
Choi Ga Young committed
102
}