nodejs验证码获取及注册

在制作一个项目的时候用户的登录注册是必不可少的一部分。并且为了账户的安全性一般会在注册账户的时候进行验证。验证码的相关内容主要在两个部分一个是mail.js,一个存在于路由上。在写验证码的首要前提是安装nodemailer插件。先展示mail.js的内容:

"use strict";
//引入第三方模块
const nodemailer = require("nodemailer");

//1.创建发送邮件的请求对象
let transporter = nodemailer.createTransport({
  host: "smtp.qq.com",//发送方邮箱,可以通过nodemailer/lib/well-know,去查找自己想要的邮箱和端口号,如:qq
  port: 465,//发送方对应的端口号
  secure: true, // true for 465, false for other ports
  auth: {
    user: 'xxx', // 发送方的邮箱地址
    pass: 'zaiqq邮箱的个人设置中获取对应的授权码', // mtp验证码
  },
});


function send(mail, code) {
  //2.邮件信息
  let mailobj = {
    from: '"Fred Foo 👻" <填写相应地qq邮箱>', // sender address,从哪里发,与上面保持一致
    to: mail, // list of receivers,发给谁
    subject: "1902", // Subject line
    //下面这两个只能有一个
    text: `你的验证码是${code},有效期5分钟`, // plain text body,只能发送字符串,数字不可以
  }
  return new Promise(function (resolve,reject) {
    //3.发送邮件,异步操作
    transporter.sendMail(mailobj, (err, data) => {
      if (err) {
        reject('发送失败')
      } else {
        resolve('发送成功')
      }
    });
  })
}
//把send抛出去
module.exports = { send }

其中第一行代码可写可不写,它的作用是:如果将"use strict";指令添加到 JavaScript 程序的第一行,则表示整个脚本都会处于严格模式。如果在函数的第一行代码中添加"use strict";,则表示只在该函数中启用严格模式。

接下来是路由中的:

/**
 * @api {POST} /user/getCode: 获取邮箱验证码
 * @apiName mail
 * @apiGroup User
 *
 * @apiParam {String} mail 邮箱.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
//发送邮箱验证码
router.post('/getCode',function(req,res){
    let {mail}=req.body
    let code=parseInt(Math.random()*10000)//产生随机验证码
    Mail.send(mail,code)
    .then(function(data){
        console.log('我进来了');
        console.log(data);
        codes[mail]=code
        res.send({err:0,msg:'验证码发送成功'})
    })
    .catch(function(err){
        console.log('进err');
        console.log(err)
        res.send({err:-1,msg:'验证码发送失败'})
    })
})

里面的Math.random是用于产生随机数的它的范围是0到1。代码中的一些在控制台输出的语句并不用写,这些是为了在测试的时候便于发现自己的错误之处的。

注册代码:


/**
 * @api {POST} /user/reg: 用户注册
 * @apiName region
 * @apiGroup User
 *
 * @apiParam {String} us 用户名.
 * @apiParam {String} ps 用户密码.
 * @apiParam {String} mail 邮箱.
 * @apiParam {Number} code 邮箱验证码.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */

//注册接口
router.post('/reg',function(req,res){
//获取数据  接受post数据 放在消息体  请求体之间 用req.body
let{us,ps,code,mail}=req.body
if(us&&ps&&code&&mail){
  //表示两者都为真(无undefined),继续往下进行
  //使用mongoose insertMany()函数将多个文档插入MongoDB和Node.js中的集合的方法
if(codes[mail]!=code){
    console.log(code);
    console.log(codes[mail]!=code);
    return res.send({err:-4,msg:'验证码错误'})
}
//查询用户名是否存在
User.find({us})
.then(function(data){
    if(data.length==0){
        //用户名不存在,可以注册
      return  User.insertMany({us:us,ps:ps})
    }else{
        res.send({err:-3,msg:'该用户名已经存在'})
    }
})
.then(function(){
    res.send({err:0,msg:'注册成功'})
})
.catch(function(err){
    res.send({err:-2,msg:'注册失败'})
})
}else{
    return res.send({err:-1,msg:'参数错误'})
}
console.log(us,ps);

因为post请求用的是req.body所以要引入第三方插件解析相关内容

//它需要通过第三方插件(body-parser)实现解析
const bodypaser=require('body-parser')
//解析表单数据  x-www-form-urlencode
app.use(bodypaser.urlencoded({extended:false}))
//解析json数据
app.use(bodypaser.json())

下一阶段的目标是学习一些在项目中需要用到的知识并且去写项目。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值