getActiveOrganization how should such an algorithm be written or where does it come from? #332

Closed
opened 2026-03-13 07:42:04 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @productdevbook on GitHub (Dec 4, 2024).

We don't keep this value in the database, if we want to send headers, we can't get it because the event doesn't return. What should we do.

export const auth = betterAuth({
  databaseHooks: {
      session: {
          create: {
              before: async(session, // event(request) no see params // )=>{
                  const organization = await getActiveOrganization(session.userId)
                  return {
                    data: {
                      ...session,
                      activeOrganizationId: organization.id
                    }
                  }
              }
          }
      }
  }
})
Originally created by @productdevbook on GitHub (Dec 4, 2024). We don't keep this value in the database, if we want to send headers, we can't get it because the event doesn't return. What should we do. ```ts export const auth = betterAuth({ databaseHooks: { session: { create: { before: async(session, // event(request) no see params // )=>{ const organization = await getActiveOrganization(session.userId) return { data: { ...session, activeOrganizationId: organization.id } } } } } } }) ```
Author
Owner

@Kabeer10 commented on GitHub (Dec 5, 2024):

i believe you just use your own logic. like get users Organizations form DB using ur own way. SQL, mongo or any ORM.

@Kabeer10 commented on GitHub (Dec 5, 2024): i believe you just use your own logic. like get users Organizations form DB using ur own way. SQL, mongo or any ORM.
Author
Owner

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

not sure what exactly you're trying to achieve but if you mean that your getActiveOrg logic depends on the request object could you tell me how exactly? (just to gather the use case)

@Bekacru commented on GitHub (Dec 5, 2024): not sure what exactly you're trying to achieve but if you mean that your `getActiveOrg` logic depends on the request object could you tell me how exactly? (just to gather the use case)
Author
Owner

@Benjamin-Lee commented on GitHub (Mar 12, 2025):

Bringing in my own use case: I want to make the base case of a user being a member of only a single organization simpler by automatically making their active organization the only one it could be.

@Benjamin-Lee commented on GitHub (Mar 12, 2025): Bringing in my own use case: I want to make the base case of a user being a member of only a single organization simpler by automatically making their active organization the only one it could be.
Author
Owner

@radosek commented on GitHub (Jun 19, 2025):

@Bekacru it is related to this.

@radosek commented on GitHub (Jun 19, 2025): @Bekacru it is related to [this](https://www.better-auth.com/docs/plugins/organization#set-active-organization).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#332