p2-inc / keycloak-orgs

Single realm, multi-tenancy for SaaS apps
https://phasetwo.io
Other
406 stars 72 forks source link

How to create new Organization when user registered? #87

Closed darknos closed 1 year ago

darknos commented 1 year ago

Hi, thank you for your organization extension - it is almost what I need for our SaaS. But I need to clarify some points.

In my case when user self registered in keycloak I need to create new Organization with name from user custom attribute called "Company" and assign user to newly created organization.

Also I have to validate Company name to be not exists when user registered.

Could you suggest a best way to do it?

paulwer commented 1 year ago

Hey @darknos also, just a user of this here... we used the events extension from keycloak to build this behavior. we implemented a webhook based solution, but i guess it would also be possible with the function aproach mentioned there.

If there is a better solution I would also gladly like to hear them.

xgp commented 1 year ago

@darknos Use the API https://phasetwo.io/api/create-organization

We do a similar thing in our own application to create an org when the user first registers. We grant the realm-management role create-organization by default to every new user, and then the user can call the API method to create a new organization. That way we can do it all in frontend/react code. If you are doing it in your own Keycloak extension, the suggestion by @paulwer to do it in an event listener or webhook will work.