Function to get active organization in server components #245

Closed
opened 2026-03-13 07:39:29 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @focux on GitHub (Nov 19, 2024).

Is your feature request related to a problem? Please describe.
I'm using Next.js with an external API, which is where I have my better-auth server. However, I want to also take advantage of server components so whenever I can, I'm using the better-auth client imperative API—like getSession—in server components and so far it's been working good.

The problem is that the only way to get the active organization using the imperative API is to first fetch the session and then get the activeOrganizationId from the session and use that id to then get the organization. I would love if there was a similar API to the useActiveOrganization hook, so we can get the active organization in server components with a single API call.

Describe the solution you'd like
Add an imperative API, similar to getSession to get the active organization from server components.

Describe alternatives you've considered
The current workaround, as I described before, it's to first get the session and. then the organization. Two api calls.

Originally created by @focux on GitHub (Nov 19, 2024). **Is your feature request related to a problem? Please describe.** I'm using Next.js with an external API, which is where I have my `better-auth` server. However, I want to also take advantage of server components so whenever I can, I'm using the `better-auth` client imperative API—like getSession—in server components and so far it's been working good. The problem is that the only way to get the active organization using the imperative API is to first fetch the session and then get the `activeOrganizationId` from the session and use that id to then get the organization. I would love if there was a similar API to the `useActiveOrganization` hook, so we can get the active organization in server components with a single API call. **Describe the solution you'd like** Add an imperative API, similar to `getSession` to get the active organization from server components. **Describe alternatives you've considered** The current workaround, as I described before, it's to first get the session and. then the organization. Two api calls.
Author
Owner

@Bekacru commented on GitHub (Dec 2, 2024):

you can use

auth.api.getFullOrganization({
   headers: await headers()
})
@Bekacru commented on GitHub (Dec 2, 2024): you can use ```ts auth.api.getFullOrganization({ headers: await headers() }) ```
Author
Owner

@focux commented on GitHub (Dec 2, 2024):

you can use

auth.api.getFullOrganization({
   headers: await headers()
})

Oooh, good to know, didn't see that in the docs but I've might missed it. Thank you!

@focux commented on GitHub (Dec 2, 2024): > you can use > > ```ts > auth.api.getFullOrganization({ > headers: await headers() > }) > ``` Oooh, good to know, didn't see that in the docs but I've might missed it. Thank you!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#245