美文网首页
node.js+express+mysql实现发送邮箱验证码以及

node.js+express+mysql实现发送邮箱验证码以及

作者: Poppy11 | 来源:发表于2020-06-27 20:35 被阅读0次

1、首先创建一个smtp服务器,并且写一个发送邮件的方法暴露出去,建立一个 nodemailer.js文件

//nodemailer.js
const nodemailer = require('nodemailer');

//创建一个smtp服务器
const config = {
    host: 'smtp.qq.com',
    port: 465,
    auth: {
        user: '自己的邮箱', //注册的邮箱账号
        pass: '自行搜索一下怎么获取自己邮箱的授权码哈' //邮箱的授权码,不是注册时的密码,等你开启的stmp服务自然就会知道了
    }
};
// 创建一个SMTP客户端对象
const transporter = nodemailer.createTransport(config);

//发送邮件
module.exports = function (mail){
    transporter.sendMail(mail, function(error, info){
        if(error) {
            return console.log(error);
        }
        console.log('mail sent:', info.response);
    });
};

2、写发送验证码的接口

router.get('/email', async (req, res) => {
    var email = req.query.email;//刚刚从前台传过来的邮箱
    var code = await createSixNum();//这里是我写的生成的随机六位数,等等下面给代码
    time = new Date().getTime()
    //去数据库中找有没有同名的用户名,这里就要自己写了,不同的数据库查询方法不同
    connection.query(`select * from user where email='${email}'`, (error, result) => {
        const type = result.length === 0
        if (type === false) {
            res.send({
                code: -1,
                message: "该邮箱已经注册"
            })
        } else {
            var mail = {
                // 发件人
                from: '<785820791@qq.com>',
                // 主题
                subject: '接受凭证',//邮箱主题
                // 收件人
                to: email,//前台传过来的邮箱
                // 邮件内容,HTML格式
                text: '用' + code + '作为你的验证码'//发送验证码
            };
            Initcode = code
            nodemail(mail)
  res.send({
                code: 0,
                message: "发送成功"
            })
        }
    })

})
function createSixNum() {
    var Num = "";
    for (var i = 0; i < 6; i++) {
        Num += Math.floor(Math.random() * 10);
    }
    return Num;
}

3、注册时校验,我想的是将请求验证码的时间和验证码存储到全局,在调用注册接口的时候调用,以便验证

router.post('/api/user/save', (req, res) => {
    const body = req.body
    const code = body.code
    delete body.code
    const attribute = []
    const data = []
    for (let i in body) {
        attribute.push(i)
        data.push(body[i])
    }
    const values = JSON.stringify(data).slice(1, JSON.stringify(data).length - 1)
    const registerTime = new Date().getTime()
    if (registerTime - time >= 5 *1000 * 60) {
        res.send({
            code: -1,
            msg: '验证码已过期'
        })
    }
    connection.query(`select * from user where username='${body.username}'`, (error, result) => {
        const type = result.length === 0
        if (type === true) {
            if(code === Initcode) {
                connection.query(`insert into user(${attribute.toString()}) values(${values})`, (error, result) => {
                    res.send({
                        code: 0,
                        msg: '注册成功'
                    })
                })
            }else{
                res.send({
                    code: -1,
                    msg: '验证码错误'
                })
            }
        } else {
            res.send({
                code: -1,
                msg: '账户名已经存在'
            })
        }
    })
})

相关文章

网友评论

      本文标题:node.js+express+mysql实现发送邮箱验证码以及

      本文链接:https://www.haomeiwen.com/subject/ecmvfktx.html