userController.js 5.25 KB
Newer Older
1
import db from "../db/index";
2
import envs from "../../config/config";
3
import jwt from "jsonwebtoken";
4
import nodemailer from "nodemailer";
5
import server_status from "../server_status";
6
import routes from "../routes";
7

KangMin An's avatar
KangMin An committed
8
// 메일 전송 처리
9
const postMail = async (email, token) => {
10
  const transporter = nodemailer.createTransport({
11
    service: envs.api.nodemailer.service,
12
13
    auth: {
      type: "OAuth2",
14
15
16
17
      user: envs.api.nodemailer.user,
      clientId: envs.api.nodemailer.gmail_client_id,
      clientSecret: envs.api.nodemailer.gmail_client_secret,
      refreshToken: envs.api.nodemailer.gmail_refresh_token,
18
19
20
21
22
23
24
    },
    tls: {
      rejectUnauthorized: false,
    },
  });

  const mailOptions = {
25
    from: `EUE Auth Supply <${envs.api.nodemailer.user}>`,
26
27
    to: email,
    subject: "EUE 사용자 계정 확인용 메일.",
28
29
    html: `<a href="${envs.server.protocol}://${envs.server.host}:${
      envs.server.port
30
    }${routes.base + routes.confirm}?token=${token}">${
31
32
      envs.server.protocol
    }://${envs.server.host}:${envs.server.port}${
33
34
      routes.base + routes.confirm
    }?token=${token}</a>`,
35
36
37
38
39
40
41
42
43
44
45
  };

  try {
    const mailResult = await transporter.sendMail(mailOptions);
    console.log(`Mail sent - ID : ${mailResult.messageId}`);
  } catch (err) {
    console.log("Mail Sending Failuer.");
    console.log(err);
  }
};

46
47
48
49
50
51
52
53
54
55
// Page for Development Test.
export const getSignup = (req, res) => {
  res.render("signup", { pagename: "Sign Up" });
};

// Page for Development Test.
export const getLogin = (req, res) => {
  res.render("login", { pagename: "Log In" });
};

KangMin An's avatar
KangMin An committed
56
57
58
59
60
61
// Page for Development Test.
export const getSetLoccode = (req, res) => {
  res.render("setLoccode", { pagename: "Set Loccode" });
};

// 회원 가입 처리
62
63
export const postSignup = async (req, res) => {
  const {
64
    body: { email, nick_name },
65
66
  } = req;

67
  const result = await db.User.findAll({
68
69
70
71
    where: { email: email },
    logging: false,
  });

72
  if (result.length != 0) {
73
74
    res.status(server_status.code.err).json({
      msg: server_status.msg.err,
75
76
77
      content: "You are aleady registered",
    });
  } else {
78
    db.User.create({ email: email, nick_name: nick_name }, { logging: false });
79
    // 로그인 페이지로 넘겨주기.
80
    res.redirect("/api/login");
81
82
83
  }
};

KangMin An's avatar
KangMin An committed
84
// 메일 확인용 토큰 발행 및 전송 처리
85
export const postLogin = async (req, res) => {
86
87
88
89
  const {
    body: { email },
  } = req;

90
  const result = await db.User.findAll({
91
92
93
94
    where: { email: email },
    logging: false,
  });

95
  if (result.length != 0) {
96
    // token 발행
97
98
99
100
    const mail_token = jwt.sign(
      {
        email: email,
      },
101
      envs.secretKey.mail,
102
103
104
      {
        expiresIn: 10 * 60,
        issuer: "eue.com",
105
        subject: "auth_checker",
106
107
108
      }
    );

109
    // 토큰이 포함된 로그인 링크 전송
110
111
    postMail(email, mail_token);

112
    res
113
114
      .status(server_status.code.ok)
      .json({ msg: server_status.msg.ok, content: "Send Mail Successfully." });
115
  } else {
116
117
    res.status(server_status.code.err).json({
      msg: server_status.msg.err,
118
119
      content: "You are not one of our user yet.",
    });
120
121
  }
};
122

KangMin An's avatar
KangMin An committed
123
// 메일로 보낸 토큰의 유효성 검사 및 access 토큰 발행 처리
124
export const getConfirm = async (req, res) => {
125
  const {
126
    query: { token },
127
128
  } = req;

129
  try {
130
    const decoded = jwt.verify(token, envs.secretKey.mail); // return payload.
131
132
133
134
135
136
137
138
139
140
141

    const result = await db.User.findAll({
      where: { email: decoded.email },
      logging: false,
    });
    const user = result[0];

    const payload = {
      email: user.email,
    };

142
    const accessT = jwt.sign(payload, envs.secretKey.access_token, {
143
144
145
146
147
      expiresIn: "14d",
      issuer: "eue.com",
      subject: "userInfo",
    });

148
149
150
151
    res
      .status(server_status.code.ok)
      .cookie("acs_token", accessT)
      .redirect("/api");
152
153
  } catch (err) {
    res
154
155
      .status(server_status.code.err)
      .json({ msg: server_status.msg.err, content: `${err}` });
156
  }
157
};
KangMin An's avatar
KangMin An committed
158

159
160
161
162
163
164
165
166
167
168
// 사용자 정보 요청 처리
export const getUserInfo = async (req, res) => {
  const {
    cookies: { acs_token },
  } = req;

  const decoded = jwt.decode(acs_token);

  const result = await db.User.findAll({ where: { email: decoded.email } });

169
  res.status(server_status.code.ok).json({ user_info: result });
170
171
172
173
174
175
176
177
178
179
180
};

// 사용자 정보 수정 요청 처리
export const postEditProfile = (req, res) => {
  const {
    cookies: { acs_token },
  } = req;

  // 수신한 변경 내용들을 통해 DB Update.

  res
181
182
    .status(server_status.code.ok)
    .json({ msg: server_status.msg.ok, content: "Server OK" });
183
184
};

KangMin An's avatar
KangMin An committed
185
186
187
188
189
190
191
192
193
194
// 사용자의 지역 코드 설정 처리
export const postSetLoccode = async (req, res) => {
  const {
    cookies: { acs_token },
    body: { loccode },
  } = req;

  const decoded = jwt.decode(acs_token);

  await db.User.update(
195
196
    { loc_code: Number(loccode) },
    { where: { email: decoded.email }, logging: false }
KangMin An's avatar
KangMin An committed
197
198
199
200
201
202
203
204
  );

  const payload = {
    email: decoded.email,
    nick_name: decoded.nick_name,
    loc_code: loccode,
  };

205
  const accessT = jwt.sign(payload, envs.secretKey.access_token, {
KangMin An's avatar
KangMin An committed
206
207
208
209
210
211
    expiresIn: "14d",
    issuer: "eue.com",
    subject: "userInfo",
  });

  res
212
    .status(server_status.code.ok)
KangMin An's avatar
KangMin An committed
213
    .cookie("acs_token", accessT)
214
    .json({ msg: server_status.msg.ok, content: "Successfully Set Loccode" });
KangMin An's avatar
KangMin An committed
215
};