There are several ways to fetch the current selected organization, depending on the use-case.
Getting the selected Organization ID
Getting the selected organization ID is done using different ways depending on the context.
- If you are in a Server Component under the
[organization]
route, theorganizationUid
is passed as a prop to the component. You can match theorganizationUid
with theuuid
property of the organization. - If you're not, use the cookie utility
parseOrganziationCookie
to get theorganizationUid
from the cookie. This is scoped by user, so you will need to pass theuserId
as well.
Using "parseOrganizationIdCookie" to get the Organization ID
You can use the parseOrganizationIdCookie
function in:
- API Routes
- Server Actions
- Server Components
import getSupabaseServerActionClient from '~/core/supabase/action-client';
import { parseOrganizationIdCookie } from '~/lib/server/cookies/organization.cookie';
import requireSession from '~/lib/user/require-session';
async function getOrganizationId() {
const client = getSupabaseServerActionClient();
const session = await requireSession(client);
return parseOrganizationIdCookie(session.user.id);
}
This can be nullish, so remember to check for that.
Fetching the selected Organization from the backend
To do so, use the function getCurrentOrganization
, which also takes care of redirecting the user to the login page if they are not signed in, and checking the correct MFA access level if the user opted in.
You can use this function in:
- API Routes
- Server Actions
- Server Components
This function requires the current user ID and the (optionally) organization UID.
- Get the current user ID from the session: we use the
requireSession
helper to do so. - Get the organization: we use the
getCurrentOrganization
helper to do so using the user ID and the organization UID.
const client = getSupabaseRouteHandlerClient();
const session = await requireSession(client);
const userId = session.user.id;
const organizationResponse = await getCurrentOrganization({
organizationUid,
userId,
});
Fetching the selected Organization from the frontend
Within the [organization]
context, the layout loader passes the current organization is passed from the backend to the frontend using the Context API. Therefore, we can require it using the useCurrentOrganization
hook.
Getting the Organization with "useCurrentOrganization"
To retrieve the signed in user from the frontend, you can use the useUserSession
hook:
import useCurrentOrganization from '~/lib/organizations/hooks/use-current-organization';
const organization = useCurrentOrganization();
This is a React hook and can only be used inside a React component.
Getting if the Organization subscription status is set to "active" on the frontend
You can use the useIsSubscriptionActive
hook to get the subscription status of the current organization. You may want to use this function to gate access to certain parts of the app.
import useCurrentOrganization from '~/lib/organizations/hooks/use-is-subscription-active';
const isActive = useIsSubscriptionActive();
This is a React hook and can only be used inside a React component.