简体   繁体   English

如何允许特定的用户类别访问特定的应用程序路线?

[英]How can i allow specific user categories to access specific routes of application?

My application consists of two type of users.我的应用程序由两种类型的用户组成。 Let's say A and B. Both of them first needs to authenticated.假设 A 和 B。他们都首先需要进行身份验证。 Authentication is done.认证完成。 But now i want A to access specific routes and if it tries to access B routes i want to give A error like access denied to this route and same for B. A is type=0 and B is type=1.但是现在我希望 A 访问特定的路由,如果它尝试访问 B 路由,我想给 A 错误,例如拒绝访问此路由,对于 B 也是如此。A 是 type=0,B 是 type=1。
For authetication i am using this middleware which uses token:对于身份验证,我正在使用这个使用令牌的中间件:

auth.js: auth.js:

const authenticate = (req, res, next)  => {

    var token = req.cookies['x-auth'];

    User.findByToken(token).then(user => {
        if(!user){
            return Promise.reject();
        }
        req.user = user;
        next();

    }).catch(err => {
        console.log(err);
        var response = {
            status:'failure',
            message: err.message
        };
        res.status(401).send(response);
    })
};

How should i proceed to achieve this?我应该如何着手实现这一目标?

Here's your solution, where roles is an array of strings containing the allowed roles for that specific route.这是您的解决方案,其中roles是一个字符串数组,其中包含该特定路线的允许角色。 This also implies that the users (in your MongoDB model) have a role field.这也意味着用户(在您的 MongoDB 模型中)具有role字段。

const authenticateWithRole = (roles = []) => {
   return async (req, res next) => {
      const token = req.cookies['x-auth'];

      try {
         const user = await User.findByToken(token);

         if (roles.includes(user.role)) {
            // Store the informations for the middleware
            req.user = user;

            next();
         } else {
            res.status(403).send({
               // Your error-response here...
            });
         }
      }
      catch (e) {
         res.status(401).send({
            // Your error-response here...
         });
      }
   }
}

// Later in your APIs

app.use('/limited-route', authenticateWithRole([
   'admin', 
   'student'
]), (req, res) => {

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在我的应用程序中为特定路径禁用NodeJS Express,并允许其中之一访问特定的正则表达式? - How can I disable NodeJS Express for specific paths in my app and allow access to a specific regex in one of them? 如何立即撤销特定用户的 jwt 访问令牌? - How can I immediately revoke jwt access token for specific user? 如何访问 mongoDB 集合中特定文档的特定属性? - How can I access a specific attribute of a specific document in a mongoDB collection? 如何创建特定于用户的实体? - How can I create an entity specific to a user? 如何允许特定服务器访问我的API? - How to allow a specific server to access my API? 我可以使用Stormpath访问特定用户的自定义数据吗? - Can I access the custom data of a specific user with Stormpath? 我可以允许特定设备访问node.js服务器并阻止所有其他设备吗? - Can i allow specific device to access node.js server and block all the others? 如何将特定的特定 model 表连接到特定的用户表? - how can i connect a specific a specific model table to a specific user table? 为了安全起见,除非添加特定于身份验证的中间件,否则如何强制所有路由未经授权? - For security purposes, how can I force all routes to be unauthorized unless I add auth-specific middleware? ng-socket-io允许房间/加入房间吗? 如果没有,如何使特定用户加入会议室? - Does ng-socket-io allow rooms/joining? If not, how do I make a specific user join a room?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM