userController.js 3.93 KB
Newer Older
1
import db from "../db/index";
2
import dotenv from "dotenv";
3
import jwt from "jsonwebtoken";
4
5
import nodemailer from "nodemailer";
import { serverMSG, statusCode } from "../serverinfo";
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 사용자 계정 확인용 메일.",
29
30
31
32
33
    html: `<a href="${process.env.PROTOCOL}://${process.env.HOST}:${
      process.env.PORT
    }${routes.base + routes.confirm}?token=${token}">${
      process.env.PROTOCOL
    }://${process.env.HOST}:${process.env.PORT}${
34
35
      routes.base + routes.confirm
    }?token=${token}</a>`,
36
37
38
39
40
41
42
43
44
45
46
  };

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

47
48
49
50
51
52
53
54
55
56
57
58
59
// 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 {
60
    body: { email, nick_name },
61
62
  } = req;

63
  const result = await db.User.findAll({
64
65
66
67
    where: { email: email },
    logging: false,
  });

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

80
export const postLogin = async (req, res) => {
81
82
83
84
  const {
    body: { email },
  } = req;

85
  const result = await db.User.findAll({
86
87
88
89
    where: { email: email },
    logging: false,
  });

90
  if (result.length != 0) {
91
    // token 발행
92
93
94
95
    const mail_token = jwt.sign(
      {
        email: email,
      },
96
      process.env.AUTH_MAIL_SECRETKEY,
97
98
99
      {
        expiresIn: 10 * 60,
        issuer: "eue.com",
100
        subject: "auth_checker",
101
102
103
      }
    );

104
    // 토큰이 포함된 로그인 링크 전송
105
106
    postMail(email, mail_token);

107
108
109
110
    res
      .status(statusCode.ok)
      .json({ msg: serverMSG.server_ok, content: "Send Mail Successfully." });
  } else {
111
112
113
114
    res.status(statusCode.err).json({
      msg: serverMSG.server_err,
      content: "You are not one of our user yet.",
    });
115
116
  }
};
117

118
export const getConfirm = async (req, res) => {
119
  const {
120
    query: { token },
121
122
  } = req;

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
  try {
    const decoded = jwt.verify(token, process.env.AUTH_MAIL_SECRETKEY); // return payload.

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

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

    const accessT = jwt.sign(payload, process.env.AUTH_ACCESS_SECRETKEY, {
      expiresIn: "6h",
      issuer: "eue.com",
      subject: "userInfo",
    });

    const refreshT = jwt.sign(payload, process.env.AUTH_REFRESH_SECRETKEY, {
      expiresIn: "14d",
      issuer: "eue.com",
      subject: "userInfo",
    });

    res
      .status(statusCode.ok)
      .cookie("access_token", accessT)
      .cookie("refresh_token", refreshT)
      .redirect("/api");
  } catch (err) {
    res
      .status(statusCode.err)
      .json({ msg: serverMSG.server_err, content: `${err}` });
  }
160
};