简体   繁体   English

用户角色:node.js express sequelize react.js

[英]User roles: node.js express sequelize react.js

I am using this tutorial on bezkoder to create user roles.我在 bezkoder 上使用本教程来创建用户角色。 However, by default the user is given role: 1 [subscriber] during sign-up.但是,默认情况下,用户在注册期间被赋予角色:1 [订阅者]。 I'm new to a lot of this, so I'm not sure how much I need to change in order to create two sign-up pages: one that will automatically register a user with the role 1 [subscriber] and one that will automatically register a user with the role 2 [expert].我对这方面很陌生,所以我不确定我需要进行多少更改才能创建两个注册页面:一个将自动注册具有角色 1 [订阅者] 的用户,另一个将自动注册具有角色 2 [专家] 的用户。

front-end https://bezkoder.com/react-jwt-auth/前端https://bezkoder.com/react-jwt-auth/

back-end https://bezkoder.com/node-js-jwt-authentication-mysql/后端https://bezkoder.com/node-js-jwt-authentication-mysql/

I think the role is being automatically registered as 1 here in the controller, but I'm not even certain this is where the role is being recorded.我认为角色在 controller 中被自动注册为 1,但我什至不确定这是角色被记录的地方。 Any help much appreciated.非常感谢任何帮助。

exports.signup = (req, res) => {
  // Save User to Database
  User.create({
    username: req.body.username,
    email: req.body.email,
    password: bcrypt.hashSync(req.body.password, 8)
  })
    .then(user => {
      if (req.body.roles) {
        Role.findAll({
          where: {
            name: {
              [Op.or]: req.body.roles
            }
          }
        }).then(roles => {
          user.setRoles(roles).then(() => {
            res.send({ message: "User was registered successfully!" });
          });
        });
      } else {
        // user role = 1
        user.setRoles([1]).then(() => {
          res.send({ message: "User was registered successfully!" });
        });
      }
    })
    .catch(err => {
      res.status(500).send({ message: err.message });
    });
};

After skimming through the provided Frontend tutorial, I think I can narrow the problem down to the component Authservice .在浏览了提供的前端教程之后,我想我可以将问题缩小到组件Authservice It has a method named register :它有一个名为register的方法:

  register(username, email, password) {
    return axios.post(API_URL + "signup", {
      username,
      email,
      password
    });
  }

Because currently you never send a role when the user registers.因为目前您在用户注册时从不发送角色 So new users always end up with the default role.所以新用户总是以默认角色结束。 If you would want to give the user another role than the default one at registration, you would have to add the parameter roles to the config object in the axios.post function.如果您想在注册时给用户另一个角色而不是默认角色,您必须将参数角色添加到 axios.post ZC1C425268E68385D1AB5074C17A94F14 中的配置object For example like this:例如像这样:

  register(username, email, password) {
    return axios.post(API_URL + "signup", {
      username,
      email,
      password,
      roles=[ "ROLE_ADMIN" ]
    });
  }

So you could create another signup page where you use this modified register function instead of the default one.因此,您可以创建另一个注册页面,在其中使用此修改后的寄存器function 而不是默认的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM