Use organizations in your application
Learn how to use organizations in your supastarter application.
In the supastarter application, the active organization is determined from the organizationSlug
path parameter of the URL.
Whenever you are on a path that starts with /app/my-organization
, the organization my-organization
is active.
There is another common pattern to store the active organization in the session of the user, but we decided to use the path parameter as it enables a few benefits:
- It's easier to understand and reason about the active organization from the URL
- You can easily share a link to a specific organization or some organization page
- You can easily navigate to a different organization by changing the path parameter
- You can use different organizations in the same browser session in different tabs
To use the active organization in your application, you can use the useActiveOrganization
hook.
It provides the following properties:
activeOrganization
: The active organizationsetActiveOrganization
: A function to set the active organization by the organization idloaded
: A boolean indicating if the active organization has been loaded yetisOrganizationAdmin
: A boolean indicating if the user is an admin or the owner of the active organizationrefetchActiveOrganization
: A function to refetch the active organization
Usually when you are inside a page that is part of the /app/[organizationSlug]
path, you the active organization is already loaded and should be set.
Get active organization on server side
To get the active organization on the server side, you can use the getActiveOrganization
function and pass it the organization slug from the url.
Get organization without slug
In some cases you want to get organization data without a slug being present in the URL.
In general the user can access the organization data for the organizations they are a member of.
To get the organization data for a specific organization, you can use the authClient
on client side:
On server side you can use the auth
class to get the organization data. Make sure to pass the headers to the request, as the cookies are not automatically attached to the request.
To learn more about how to use organizations, please refer to the better-auth documentation.