如何从后端验证前端的管理员? (Node.js,Angular,Mongodb)

问题描述 投票:-2回答:1

在用户模型中,我具有'isAdmin'属性,其deflaut值为false。在mongodb中,我“手动”创建了在isAdmin中具有真正价值的管理员。当我以管理员身份登录时,请进行程序验证,然后终端显示“ admin”。但是,如何将这个真实值移到前端以检查是否为管理员呢?那我可以在前端写些什么?

    isAdmin: {
        type: Boolean,
        default: false
    },
router.post('/login', (req, res) => { 
  let userData = req.body;

  User.findOne({ email: userData.email }, (error, user) => {
    if (error) {
      console.log(error);
    } else {
      if (!user) {
        res.status(401).send('Invalid email');
      } else
        if (user.password !== userData.password) {
          res.status(401).send('Invalid password')
        } else {
          if (user.isAdmin) { // admin <--------------------
            console.log('admin');          
          }
          let payload = { subject: user._id };
          let token = jwt.sign(payload, 'secretKey');
          res.status(200).send({ token });
        }
    }

  })
})
node.js angular mongodb admin mean-stack
1个回答
0
投票

我在令牌旁边返回isAdmin标志,它起作用。

© www.soinside.com 2019 - 2024. All rights reserved.