注册接口

注册接口:

Post 请求  local host:4000/api/user/register

步骤:

(1) 进行验证

(2) 查询数据库,查询的依据是用户的email

(3) 进行判断,如果查询得到的数据大于0,则邮箱已经存在,用户被注册了,如果查询数据小于0,就表示用户还没有被注册,我们可以注册。我们应该构建注册用户的数据结构,数据结构里面的password进行加密处理,构建好数据结构,利用await 进行数据库的储存,最后返回json 对象

验证:

 //input  验证
  const  {errors, isValid} = validatorRegisterInput(ctx.request.body);
  if(!isValid) {
      ctx.status=400;
      ctx.body =errors;
      return  ;
  }

 验证的内容 

const  Validator = require('validator');
const  isEmpty  = require('./isempty');
module.exports= function  validatorRegisterInput(data) {
    let errors= {};
    data.name        = !isEmpty(data.name) ? data.name :'';
    data.email       = !isEmpty(data.email) ? data.email :'';
    data.password    = !isEmpty(data.password) ? data.password :'';
    data.password2   = !isEmpty(data.password2) ? data.password2 :'';
    if (!Validator.isLength(data.name, { min: 2, max: 30 })) {
        errors.name = '名字的长度不能小于2位且不能超过30位';
      }
    
      if (Validator.isEmpty(data.name)) {
        errors.name = '名字不能为空';
      }
    
      if (!Validator.isEmail(data.email)) {
        errors.email = '邮箱不合法';
      }
    
      if (Validator.isEmpty(data.email)) {
        errors.email = '邮箱不能为空';
      }
    
      if (Validator.isEmpty(data.password)) {
        errors.password = 'password不能为空';
      }
    
      if (!Validator.isLength(data.password, { min: 6, max: 30 })) {
        errors.password = 'password的长度不能小于6位且不能超过30位';
      }
    
      if (Validator.isEmpty(data.password2)) {
        errors.password2 = 'password2不能为空';
      }
    
      if (!Validator.equals(data.password, data.password2)) {
        errors.password2 = '两次密码不一致';
      }
    return {
        errors,
        isValid:isEmpty(errors)
    };
};

  查询数据库,查询的依据是用户的email

const  findResult=await User.find({
     email:ctx.request.body.email
  });

  进行判断

if(findResult.length>0) {
    ctx.status=500;
    ctx.body= {
       email:'邮箱已经存在'
    }
  } 
  //2.没有查询到,储存起来
  else { 
      //全球公用头像的使用
      const avatar = gravatar.url(ctx.request.body.email, 
        {s: '200', r: 'pg', d: 'mm'
      });
      const  newUser=new User({
         name:ctx.request.body.name,
         email:ctx.request.body.email,
         avatar,
         password: tools.enbcrypt(ctx.request.body.password)
      });
        // console.log(newUser);
        // 存储到数据库
        await newUser
        .save()
        .then(user => {
          ctx.body = user;
        })
        .catch(err => {
          console.log(err);
        });
      // 返回json数据
      ctx.body = newUser;

  完整的demo

router.post('/register',async  ctx=>{
  //input  验证
  const  {errors, isValid} = validatorRegisterInput(ctx.request.body);
  if(!isValid) {
      ctx.status=400;
      ctx.body =errors;
      return  ;
  }
  //查询数据库并且储存得到的数据
  const  findResult=await User.find({
     email:ctx.request.body.email
  });
  // console.log(findResult);
  //1.在数据库中查询到该邮箱
  if(findResult.length>0) {
    ctx.status=500;
    ctx.body= {
       email:'邮箱已经存在'
    }
  } 
  //2.没有查询到,储存起来
  else { 
      //全球公用头像的使用
      const avatar = gravatar.url(ctx.request.body.email, 
        {s: '200', r: 'pg', d: 'mm'
      });
      const  newUser=new User({
         name:ctx.request.body.name,
         email:ctx.request.body.email,
         avatar,
         password: tools.enbcrypt(ctx.request.body.password)
      });
        // console.log(newUser);
        // 存储到数据库
        await newUser
        .save()
        .then(user => {
          ctx.body = user;
        })
        .catch(err => {
          console.log(err);
        });
      // 返回json数据
      ctx.body = newUser;
      
  }

})

  

router. post( '/register', async ctx =>{
//input 验证
const { errors, isValid} = validatorRegisterInput( ctx. request. body);
if(! isValid) {
ctx. status= 400;
ctx. body = errors;
return ;
}
//查询数据库并且储存得到的数据
const findResult= await User. find({
email:ctx. request. body. email
});
// console.log(findResult);
//1.在数据库中查询到该邮箱
if( findResult. length> 0) {
ctx. status= 500;
ctx. body= {
email: '邮箱已经存在'
}
}
//2.没有查询到,储存起来
else {
//全球公用头像的使用
const avatar = gravatar. url( ctx. request. body. email,
{ s: '200', r: 'pg', d: 'mm'
});
const newUser= new User({
name:ctx. request. body. name,
email:ctx. request. body. email,
avatar,
password: tools. enbcrypt( ctx. request. body. password)
});
// console.log(newUser);
// 存储到数据库
await newUser
. save()
. then( user => {
ctx. body = user;
})
. catch( err => {
console. log( err);
});
// 返回json数据
ctx. body = newUser;
 
}

})

猜你喜欢

转载自www.cnblogs.com/guangzhou11/p/10356324.html