This discussion has been migrated from our Github Discussion #7328
EKarpinsky140d ago
I have 2 authenticated roles: Role A and Role B. How can I let the user choose which role he is and set it upon registration?
Thanks
This discussion has been migrated from our Github Discussion #7328
I have 2 authenticated roles: Role A and Role B. How can I let the user choose which role he is and set it upon registration?
Thanks
Responses to the discussion on Github
Author
SOLVED: You have to change the register function that comes with Strapi, now located in the ânode_modulesâ directory. Create a new file âAuth.jsâ in /extensions/controllers (you will have to create the controllers directory as well). In that Auth.js file, copy paste the register function from ânode_modules/strapi-plugin-users-permissionsâ, as well as the consts at the top such as lodash, and edit the role declaration into the following line:
const role = await strapi.query(âroleâ, âusers-permissionsâ).findOne({id: ctx.request.body.role.id}, );
Quite the long approach, so hopefully Strapi will release an easier way of specifying role id on register, instead of just using the default one.
Maintainer
Thank you for this answer, got a lot of this question. We will probably have to reference that somewhere
Author
My pleasure! Was bugging me since yesterday and even Google had a tough one with this!
Collaborator
Quite the long approach, so hopefully Strapi will release an easier way of specifying role id on register, instead of just using the default one.
Previously our stance on this has been it can be a security risk so we generally advise against allowing the user to pick their role. I know this isnât always the case for all users but in the few that I have seen it would have been a pretty major security risk. My suggestion in the past was to register them under the default and then via either an automated review process or manual review to then reassign their role after that.
Either way thank you for the solution you found
Thanks, @EKarpinsky for this solution.
I really needed this feature and was happy this helped me.
Just a point to note, putting the logic in **/extensions/controllers/Auth.js**
, the custom roles logic doesnât work. The default logic from the node_modules was still called. To correct this i tried:
extensions\users-permissions\controllers\Auth.js
and this worked perfectly.
This seems like a âwrong questionâ The roles are allowing the application to categorize users in the process of authentication - and allow / disallow authenticated users (a process called authorization) certain privileges. A trivial and often used example is the classification of users into three groups (bronze silver and gold) based on the subscription price. If the user is able to select his role, the classification becomes meaningless
@adriatic this discussion is quite old and predates our RBAC feature, the role the user is talking about is from the users-permissions plugin and doesnât depend on any license.
I migrated it from GitHub Discussions as we are shutting our GitHub Discussions down this week.
Thanks for the explanation Derrick
I try to use this solution but when i make a mutation I get an error
i dont know what am i doing wrong
i copied the file from ânode_modules\strapi-plugin-users-permissions\controllers\Auth.jsâ
and pasted it here âextensions\users-permissions\controllers\Auth.jsâ
and edited the declaration of role, but now i have an error while making a mutation in graphql
i have this same problem and the solutions proposed here are not working for me⌠does anyone know any better way?