Sequelize:如何将我的解密值与 req.body 进行比较

Sequelize: How to compare my decrypted values with req.body

我正在尝试查找其手机号码 = req.body.mobilenumber 的用户。我遇到的问题是我数据库中的 mobilenumber 字段已加密。

当我在前端输入他们的手机号码时,我如何实现我在我的数据库中解密手机号码并检查我的req.body.mobilenumber。

我正在使用 Sequelize 作为我的 ORM。

我第一次有这个:

User.findOne({
               where: {
                       mobilenumber: req.body.countrycode + req.body.mobilenumber,
                       },
               }).then((user) => {
                                   if (user) {
                                               res
                                               .status(400)
                                               .send({ error: "A user with the given mobile number exists." });
                                               return;
                                             }
                                   res.status(201).send(user);
                                 });

然后我注意到我在数据库中的值已加密,因此所有手机号码都通过了,所以我尝试了以下但它不起作用:

router.post(
"/",
(req, res) => {      
User.findAll().then((user)=>{

   console.log(user.map(x=>x.mobilenumber))

   const user_mobilenumber=user.map(x=>x.mobilenumber);

   const new_user =req.body.countrycode + req.body.mobilenumber;

const aes256gcm = (key) => {

    const decrypt = (enc) => {
      enc = Buffer.from(enc, "base64");
      const iv = enc.slice(enc.length - 28, enc.length - 16);
      const tag = enc.slice(enc.length - 16);
      enc = enc.slice(0, enc.length - 28);
      const decipher = crypto.createDecipheriv('aes-256-gcm', key, iv);
      decipher.setAuthTag(tag);
      let str = decipher.update(enc, null, 'utf8');
      str += decipher.final('utf8');
      return str;
    };
  
    return {
      decrypt,
    };
  };

  const aesCipher = aes256gcm(key);

   user_mobilenumber.forEach((x)=>{
     const y = aesCipher.decrypt(x)
     console.log(y)

     if(new_user===y){
      res.status(400).send({ error: "A user with the given mobile number exists." });
     }
   res.status(201).send(new_user);


   })

 })
});

我一直收到这个错误:

(node:6456) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

好的,我知道了,所以问题出在代码的 forEach 部分,您发送响应两次,即

if(new_user===y){
      res.status(400).send({ error: "A user with the given mobile number exists." });
     }
   res.status(201).send(new_user);

要么 return 来自第一个 if,要么将第二个状态发送到 else 块中,即 像这样

for (const x of user_mobilenumber) {
  const y = aesCipher.decrypt(x);
  console.log(y);
  if (new_user === y) {
    res.status(400).send({ error: "A user with the given mobile number exists." });
    return;
  }
}

res.status(201).send(new_user);