겹치는 코드를 Class로 지정해서 사용하여 코드의 가독성을 높이는 작업을 했다.
// 이메일 인증
exports.emailauth = async (req, res, next) => {
const { email } = req.body;
// 인증메일 (번호)
const emailAuth = Math.floor(Math.random() * 10000);
await userService.emailauth(emailAuth);
const transporter = nodemailer.createTransport({
service: 'gmail',
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: process.env.NODEMAILER_USER,
pass: process.env.NODEMAILER_PASS,
},
});
let info = await transporter.sendMail({
from: `"Paper 환영합니다" <${process.env.NODEMAILER_USER}>`,
to: email,
subject: '[Paper] 인증번호가 도착했습니다.',
text: `${emailAuth}`,
});
res.status(200).json({
result: true,
});
};
// 이메일 인증 체크
exports.check_emaliauth = async (req, res, next) => {
const { emailAuth } = req.body;
const text = await userService.check_emaliauth(emailAuth);
await userService.delet_check_emaliauth(emailAuth);
console.log(text.emailAuth);
if (Number(emailAuth) === text.emailAuth) {
return res.status(200).send({
result: true,
});
}
res.status(400).send({
result: false,
});
};
// 비밀번호 변경
exports.change_password = async (req, res, next) => {
const { email, password } = req.body;
await userService.change_password(email, password);
res.status(200).send({
result: true,
});
};
// 이메일 인증 (로그인 시)
exports.login_emailauth = async (req, res, next) => {
const { user } = res.locals;
console.log(user.email);
// 인증메일 (번호)
const emailAuth = Math.floor(Math.random() * 10000);
await userService.login_emailauth(user, emailAuth);
const transporter = nodemailer.createTransport({
service: 'gmail',
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: process.env.NODEMAILER_USER,
pass: process.env.NODEMAILER_PASS,
},
});
let info = await transporter.sendMail({
from: `"Paper 환영합니다" <${process.env.NODEMAILER_USER}>`,
to: user.email,
subject: '[Paper] 인증번호가 도착했습니다.',
text: `${emailAuth}`,
});
res.status(200).json({
result: true,
});
};
// 이메일 인증 체크(로그인 시)
exports.login_check_emaliauth = async (req, res, next) => {
const { user } = res.locals;
const { emailAuth } = req.body;
const text = await userService.login_check_emaliauth(user);
await userService.login_delet_check_emaliauth(user);
if (Number(emailAuth) === text.emailAuth) {
res.status(200).send({
result: true,
});
return;
}
res.status(400).send({
result: false,
});
};
// 비밀번호 변경(로그인 시)
exports.login_change_password = async (req, res, next) => {
const { user } = res.locals;
const { password } = req.body;
await userService.login_change_password(user, password);
res.status(200).send({
result: true,
});
};
// 이메일 인증
exports.emailauth = async (emailAuth) => {
await User.create({ emailAuth });
};
// 이메일 인증 체크
exports.check_emaliauth = async (emailAuth) => {
return await User.findOne({
where: { emailAuth },
attributes: ['emailAuth'],
});
};
// 이메일 인증 삭제
exports.delet_check_emaliauth = async (emailAuth) => {
await User.destroy({ where: { emailAuth } });
};
// 비밀번호 변경
exports.change_password = async (email, password) => {
const salt = await Bcrypt.genSalt();
const pwhash = await Bcrypt.hash(password, salt);
await User.update({ password: pwhash }, { where: { email } });
};
// 이메일 인증 (로그인 시)
exports.login_emailauth = async (user, emailAuth) => {
await User.update({ emailAuth }, { where: { userId: user.userId } });
};
// 이메일 인증 체크 (로그인 시)
exports.login_check_emaliauth = async (user) => {
return await User.findOne({
where: { userId: user.userId },
attributes: ['emailAuth'],
});
};
// 이메일 인증 삭제
exports.login_delet_check_emaliauth = async (user) => {
await User.update({ emailAuth: null }, { where: { userId: user.userId } });
};
// 비밀번호 변경 (로그인 시)
exports.login_change_password = async (user, password) => {
const salt = await Bcrypt.genSalt();
const pwhash = await Bcrypt.hash(password, salt);
await User.update({ password: pwhash }, { where: { userId: user.userId } });
};
class User {
async emailAuth(params) {
// 인증메일 (번호)
const emailAuth = Math.floor(Math.random() * 10000);
await userService.emailauth(params, emailAuth);
const transporter = nodemailer.createTransport({
service: process.env.NODEMAILER_SERVICE,
host: process.env.NODEMAILER_HOST,
port: process.env.NODEMAILER_PORT,
secure: false,
auth: {
user: process.env.NODEMAILER_USER,
pass: process.env.NODEMAILER_PASS,
},
});
let info = await transporter.sendMail({
from: `"Paper 환영합니다" <${process.env.NODEMAILER_USER}>`,
to: params,
subject: '[Paper] 인증번호가 도착했습니다.',
text: `${emailAuth}`,
});
}
async emailAuth_check(params, emailAuth) {
const text = await userService.check_emaliauth(params, emailAuth);
if (emailAuth === text) {
await userService.delet_check_emaliauth(params);
return true;
}
return false;
}
async password_change(params, password) {
await userService.change_password(params, password);
}
}
// 이메일 인증
exports.emailauth = async (req, res, next) => {
const { email } = req.body;
const user = new User();
user.emailAuth(email);
res.status(200).json({
result: true,
});
};
// 이메일 인증 체크
exports.check_emaliauth = async (req, res, next) => {
const { email, emailAuth } = req.body;
const user = new User();
const emailauth_check = await user.emailAuth_check(email, emailAuth);
emailauth_check === true
? res.status(200).send({ result: true })
: res.status(400).send({
result: false,
});
};
// 비밀번호 변경
exports.change_password = async (req, res, next) => {
const { email, password, confirmPassword } = req.body;
if (password !== confirmPassword) {
return res.status(400).send({
result: false,
msg: '비밀번호가 틀립니다',
});
}
const users = new User();
users.password_change(email, password);
res.status(200).send({
result: true,
});
};
// 이메일 인증 (로그인 시)
exports.login_emailauth = async (req, res, next) => {
const { user } = res.locals;
const users = new User();
users.emailAuth(user.email);
res.status(200).json({
result: true,
});
};
// 이메일 인증 체크(로그인 시)
exports.login_check_emaliauth = async (req, res, next) => {
const { user } = res.locals;
const { emailAuth } = req.body;
const users = new User();
const emailauth_check = await users.emailAuth_check(user.email, emailAuth);
emailauth_check === true
? res.status(200).send({ result: true })
: res.status(400).send({
result: false,
});
};
// 비밀번호 변경(로그인 시)
exports.login_change_password = async (req, res, next) => {
const { user } = res.locals;
const { password, confirmPassword } = req.body;
if (password !== confirmPassword) {
return res.status(400).send({
result: false,
msg: '비밀번호가 틀립니다',
});
}
const users = new User();
users.password_change(user.email, password);
res.status(200).send({
result: true,
});
};
// 이메일 인증
exports.emailauth = async (parmas, emailAuth) => {
const emailcheck = await redisCliv4.get('email_auth ' + parmas);
!emailcheck
? await redisCli.setex('email_auth ' + parmas, 300, emailAuth)
: await redisCli.setex('email_auth ' + parmas, 300, emailAuth);
};
// 이메일 인증 체크
exports.check_emaliauth = async (parmas) => {
return await redisCliv4.get('email_auth ' + parmas);
};
// 이메일 인증 삭제
exports.delet_check_emaliauth = async (parmas) => {
const emailcheck = await redisCliv4.exists('email_auth ' + parmas); // true: 1 , false: 0
if (emailcheck) await redisCli.del('email_auth ' + parmas);
};
// 비밀번호 변경
exports.change_password = async (parmas, password) => {
const salt = await Bcrypt.genSalt();
const pwhash = await Bcrypt.hash(password, salt);
await User.update({ password: pwhash }, { where: { email: parmas } });
};
class로 만들어서, 로그인이 되어있을때와, 되어있지 않을때 값을 다르게 받아온다.
기존 user, email 두 인자 때문에 다른 두개의 API로 구성했다.
변경 user, email 두 인자를 다리게 받아오는 것은 맞지만 service부분을 하나로 통일 하면서 코드의 단순화를 시켜 보았다