User self-registration with different roles based on some parameter

My application has 2 different types of users - “USERS” and “PROVIDERS”, which I map to different roles in Keycloak. Think of it like task rabbit where normal people can ask for some service, and service providers can bid on providing that service.

I need to allow both “USERS” and “PROVIDERS” to register and log in with multiple identity providers, but I need them to automatically have different roles assigned based on where they originated from. i.e. if a normal user clicked “register” from the home page, they would be assigned the “USER” role. If they clicked “register” from the provider landing page, they would be assigned the “PROVIDER” role.

Is there any way of doing this in Keycloak through configuration? Or if code is the only way, are there any examples of how to do this? Since I’m allowing them to use social logins to register, I don’t know of a way to programmatically register them since I wouldn’t have any credentials to register them with.

I believe you would have to modify the registration form or build your own.

If you modify, you add a field (which you want to validate somehow!?) and then use it to assign a role when data is stored in idp.

If you build your own registration on another server you would have to use the admin api to create the user object including the associated roles in the idp.

Idea: add the users to a group like “providers” and associate the group with the role.