userController.js 3.28 KB
Newer Older
1
import db from "../db/index";
2
import dotenv from "dotenv";
KangMin An's avatar
KangMin An committed
3
import jwt from "jsonwebtoken";
4
5
import nodemailer from "nodemailer";
import { serverMSG, statusCode } from "../serverinfo";
KangMin An's avatar
KangMin An committed
6
import routes from "../routes";
7
8
9

dotenv.config();

10
const postMail = async (email, token) => {
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  const transporter = nodemailer.createTransport({
    service: process.env.NODEMAILER_SERVICE,
    auth: {
      type: "OAuth2",
      user: process.env.NODEMAILER_USER,
      clientId: process.env.NODEMAILER_GAMIL_CLIENT_ID,
      clientSecret: process.env.NODEMAILER_GMAIL_CLIENT_PASSWORD,
      refreshToken: process.env.NODEMAILER_GMAIL_REFRESH_TOKEN,
    },
    tls: {
      rejectUnauthorized: false,
    },
  });

  const mailOptions = {
    from: `EUE Auth Supply <${process.env.NODEMAILER_USER}>`,
    to: email,
    subject: "EUE 사용자 계정 확인용 메일.",
KangMin An's avatar
KangMin An committed
29
30
31
32
33
    html: `<a href="${process.env.HOST}:${process.env.PORT}${
      routes.base + routes.confirm
    }?token=${token}">${process.env.HOST}:${process.env.PORT}${
      routes.base + routes.confirm
    }?token=${token}</a>`,
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  };

  try {
    const mailResult = await transporter.sendMail(mailOptions);
    console.log(`Mail sent - ID : ${mailResult.messageId}`);
    res
      .status(statusCode.ok)
      .json({ msg: serverMSG.server_ok, content: mailResult.response });
  } catch (err) {
    console.log("Mail Sending Failuer.");
    console.log(err);
    res
      .status(statusCode.err)
      .json({ msg: serverMSG.server_err, content: err });
  }
};

51
52
53
54
55
56
57
58
59
60
61
62
63
// 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" });
};

// Function for Signup Proccess.
export const postSignup = async (req, res) => {
  const {
KangMin An's avatar
KangMin An committed
64
    body: { email, nick_name },
65
66
67
68
69
70
71
  } = req;

  const result = db.User.findOne({
    where: { email: email },
    logging: false,
  });

KangMin An's avatar
KangMin An committed
72
  if (result.length != 0) {
73
74
75
76
77
    res.status(statusCode.err).json({
      msg: serverMSG.server_err,
      content: "You are aleady registered",
    });
  } else {
KangMin An's avatar
KangMin An committed
78
    db.User.create({ email: email, nick_name: nick_name }, { logging: false });
79
    // 로그인 페이지로 넘겨주기.
KangMin An's avatar
KangMin An committed
80
81
82
83
    res.status(statusCode.ok).json({
      msg: serverMSG.server_ok,
      content: "Successfully create user.",
    });
84
85
86
87
88
89
90
91
  }
};

export const postLogin = (req, res) => {
  const {
    body: { email },
  } = req;

KangMin An's avatar
KangMin An committed
92
  const result = db.User.findAll({
93
94
95
96
    where: { email: email },
    logging: false,
  });

KangMin An's avatar
KangMin An committed
97
  if (result.length != 0) {
98
    // token 발행
KangMin An's avatar
KangMin An committed
99
100
101
102
103
104
105
106
107
108
109
110
111
    const mail_token = jwt.sign(
      {
        email: email,
        nick_name: resutl[0]["nick_name"],
      },
      process.env.AUTH_SECRETKEY,
      {
        expiresIn: 10 * 60,
        issuer: "eue.com",
        subject: "userInfo",
      }
    );

112
    // 토큰이 포함된 로그인 링크 전송
KangMin An's avatar
KangMin An committed
113
114
    postMail(email, mail_token);

115
116
117
118
    res
      .status(statusCode.ok)
      .json({ msg: serverMSG.server_ok, content: "Send Mail Successfully." });
  } else {
KangMin An's avatar
KangMin An committed
119
120
121
122
    res.status(statusCode.err).json({
      msg: serverMSG.server_err,
      content: "You are not one of our user yet.",
    });
123
124
  }
};
KangMin An's avatar
KangMin An committed
125
126
127
128
129
130
131
132

export const getConfirm = (req, res) => {
  const {
    params: { token },
  } = req;

  console.log(`Hi, test token : ${token}`);
};