如何管理node.js中的角色?

发布于 2025-02-12 23:09:40 字数 1433 浏览 3 评论 0原文

我在Auth模型中有三种不同类别的用户,如下所示:

accountType: {
    type: String,
    required: true,
    trim: true,
    default: "user",
    enum: ["merchant", "user", "provider"],
  }

现在,我想简单地让商家上传停车信息。这是查询:

exports.parkingAdd = async (req, res) => {
    try {
        const { parkingName, price, address, name, phoneNumber, about, parkingType, city, state, zipCode } = req.body;
        const check_exist = await Auth.findById(req.data.id);
        if (!check_exist) return res.status(404).json({ error: 'User not found' })

        let new_parking = new Parking({
            merchantId: req.data.id,
            parkingName,
            price,
            contactInfo: {
                name,
                phoneNumber
            },
            about,
            parkingType,
            address: {
                address,
                city,
                state,
                zipCode
            }
        });
        const save = await new_parking.save();
        return res.status(200).json({
            success: true,
            msg: "Parking has been added successfully",
            data: { parkingDetails: save },
        });
    }
    catch (error) {
        return error.message;
    }
}

如何更改上述请求,以便只有商人才能上传有关停车详细信息的信息。因为普通用户也能够添加有关停车的详细信息。 我想限制这一点。

routing.post("/parking/add",middleware.authenticateToken,merchant.parkingAdd);

I have three distinct categories of users in the auth model, as follows:

accountType: {
    type: String,
    required: true,
    trim: true,
    default: "user",
    enum: ["merchant", "user", "provider"],
  }

Now I want to simply let the merchant upload the parking information. Here is the query:

exports.parkingAdd = async (req, res) => {
    try {
        const { parkingName, price, address, name, phoneNumber, about, parkingType, city, state, zipCode } = req.body;
        const check_exist = await Auth.findById(req.data.id);
        if (!check_exist) return res.status(404).json({ error: 'User not found' })

        let new_parking = new Parking({
            merchantId: req.data.id,
            parkingName,
            price,
            contactInfo: {
                name,
                phoneNumber
            },
            about,
            parkingType,
            address: {
                address,
                city,
                state,
                zipCode
            }
        });
        const save = await new_parking.save();
        return res.status(200).json({
            success: true,
            msg: "Parking has been added successfully",
            data: { parkingDetails: save },
        });
    }
    catch (error) {
        return error.message;
    }
}

how to change the above request so that only the merchant may upload the information regarding parking details. Because normal user is able to add the details regarding parking as well.
I want to restrict this.

routing.post("/parking/add",middleware.authenticateToken,merchant.parkingAdd);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狼性发作 2025-02-19 23:09:40

假设loggin用户在他的会话中具有帐户,

const authenticateToken=(req,res,next)=>{
  if(req.user.accountType !== "merchant"){
      return res
        .status(403)
        .json({status: false, message:"Forbidden"})
  }
  next();
}
exports.authenticateToken=authenticateToken;

Suppose loggin user has accountType in his session,

const authenticateToken=(req,res,next)=>{
  if(req.user.accountType !== "merchant"){
      return res
        .status(403)
        .json({status: false, message:"Forbidden"})
  }
  next();
}
exports.authenticateToken=authenticateToken;
牛↙奶布丁 2025-02-19 23:09:40

好的,我也找到了解决方案。
在查询中应该是

if (check_exist.accountType !== "merchant")
  return res
    .status(401)
    .json({
      error: "You must be a merchant user to post the parking details",
    });

Ok, I found the solution as well.
In the query it should be

if (check_exist.accountType !== "merchant")
  return res
    .status(401)
    .json({
      error: "You must be a merchant user to post the parking details",
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文