TIL 2022.07.07 로그인시 AND 아이디와/비밀번호 잊었을때

박선우·2022년 7월 6일
0

로직 구현

목록 보기
1/4
post-thumbnail

오늘 한일 이메일인증을 통한 아이디찾기/비밀번호 변경,
AccessToken / RefreshToken 발급 하기

로그인시 AND 아이디와/비밀번호 잊었을때

// 이메일 인증
const emailauth = async (req, res, next) => {
  try {
    const { email } = req.body;
    // 인증메일 (번호)
    const emailAuth = Math.floor(Math.random() * 10000);

    await userService.emailauth(email, emailAuth);

    const transporter = nodemailer.createTransport({
      service: 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: email,
      subject: '[Paper] 인증번호가 도착했습니다.',
      text: `${emailAuth}`,
    });

    res.status(200).json({
      result: true,
    });
  } catch (error) {
    console.log(error);
    next(error);
  }
};
exports.emailauth = emailauth;

// 이메일 인증 체크
const check_emaliauth = async (req, res, next) => {
  try {
    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,
    });
  } catch (error) {
    console.log(error);
    next(error);
  }
};
exports.check_emaliauth = check_emaliauth;

// 비밀번호 변경
const change_password = async (req, res, next) => {
  try {
    const { email, password } = req.body;

    await userService.change_password(email, password);

    res.status(200).send({
      result: true,
    });
  } catch (error) {
    console.log(error);
    next(error);
  }
};
----------------------로그인 되어있을떄----------------------------
// 이메일 인증 (로그인 시)
const login_emailauth = async (req, res, next) => {
  try {
    const { user } = res.locals;
    // 인증메일 (번호)
    const emailAuth = Math.floor(Math.random() * 10000);

    await userService.login_emailauth(user, emailAuth);

    const transporter = nodemailer.createTransport({
      service: 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: user.email,
      subject: '[Paper] 인증번호가 도착했습니다.',
      text: `${emailAuth}`,
    });

    res.status(200).json({
      result: true,
    });
  } catch (error) {
    console.log(error);
    next(error);
  }
};
exports.login_emailauth = login_emailauth;

// 이메일 인증 체크(로그인 시)
const login_check_emaliauth = async (req, res, next) => {
  try {
    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,
    });
  } catch (error) {
    console.log(error);
    next(error);
  }
};
exports.login_check_emaliauth = login_check_emaliauth;

// 비밀번호 변경(로그인 시)
const login_change_password = async (req, res, next) => {
  try {
    const { user } = res.locals;
    const { password } = req.body;

    await userService.login_change_password(user, password);

    res.status(200).send({
      result: true,
    });
  } catch (error) {
    console.log(error);
    next(error);
  }
};
exports.login_change_password = login_change_password;
  • 처음 짠 로직은 이렇다... 코드가 너무 많이 겹치는 부분이 있는데
    로그인 되어있을때는 Auth가 인증이 되어있어야 해서 합치는 건 포기 했다 ..
profile
코린이 열심히 배우자!

0개의 댓글