useActiveOrganization is returning null always

This does not work even if setting active organization id on the create and in manually in middleware also
30 Replies
Rohit
RohitOP4w ago
helper function
"use server";
import prisma from "../prisma";

export const getActiveOrganization = async (
userId: string
): Promise<{
name: string;
id: string;
createdAt: Date;
slug: string | null;
logo: string | null;
metadata: string | null;
} | null> => {
const organizations = await prisma.organization.findFirst({
where: {
members: { some: { userId } },
},
orderBy: { createdAt: "desc" },
});
return organizations;
};

export const getActiveTeamInTheOrganization = async (
organizationId: string
): Promise<{
name: string;
id: string;
createdAt: Date;
updatedAt: Date | null;
organizationId: string;
} | null> => {
const team = await prisma.team.findFirst({
where: { organizationId },
orderBy: { createdAt: "desc" },
});
return team;
};
"use server";
import prisma from "../prisma";

export const getActiveOrganization = async (
userId: string
): Promise<{
name: string;
id: string;
createdAt: Date;
slug: string | null;
logo: string | null;
metadata: string | null;
} | null> => {
const organizations = await prisma.organization.findFirst({
where: {
members: { some: { userId } },
},
orderBy: { createdAt: "desc" },
});
return organizations;
};

export const getActiveTeamInTheOrganization = async (
organizationId: string
): Promise<{
name: string;
id: string;
createdAt: Date;
updatedAt: Date | null;
organizationId: string;
} | null> => {
const team = await prisma.team.findFirst({
where: { organizationId },
orderBy: { createdAt: "desc" },
});
return team;
};
i think there is issue with use hooks dont know why but nether useSession nor any use hooks is working @admin can someone help me with this am i doing something wrong ?
Ping
Ping4w ago
in your authClient you're passing some server plugins. nextCookies is a server only plugin and passkeys is server too, you need to use passkeyClient() you're also missing some plugins on the server auth instance, like passkey needs to be in the server auth config's plugins list. you're also missing twoFactor()
Rohit
RohitOP4w ago
yes backend i removed plugins before sharing to reduce message size but i am using them
plugins: [
openAPI({
theme: "deepSpace",
}),
organization({
allowCreatingOrganizations: false,
teams: {
enabled: true,
maximumTeams: 10,
allowRemovingAllTeams: true,
maximumMembersPerTeam: 10,
},
}),
lastLoginMethod(),
emailHarmony(),
oneTap(),
haveIBeenPwned(),
twoFactor(),
passkey(),
nextCookies(),
],
plugins: [
openAPI({
theme: "deepSpace",
}),
organization({
allowCreatingOrganizations: false,
teams: {
enabled: true,
maximumTeams: 10,
allowRemovingAllTeams: true,
maximumMembersPerTeam: 10,
},
}),
lastLoginMethod(),
emailHarmony(),
oneTap(),
haveIBeenPwned(),
twoFactor(),
passkey(),
nextCookies(),
],
this is server side this is client side
export const authClient = createAuthClient({
plugins: [
organizationClient({
teams: {
enabled: true,
},
}),
emailHarmony(),
oneTapClient({
clientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID!,
autoSelect: false,
cancelOnTapOutside: true,
context: "signin",
promptOptions: {
baseDelay: 1000,
maxAttempts: 5,
},
}),
lastLoginMethodClient(),
twoFactorClient(),
passkeyClient(),
],
});
export const authClient = createAuthClient({
plugins: [
organizationClient({
teams: {
enabled: true,
},
}),
emailHarmony(),
oneTapClient({
clientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID!,
autoSelect: false,
cancelOnTapOutside: true,
context: "signin",
promptOptions: {
baseDelay: 1000,
maxAttempts: 5,
},
}),
lastLoginMethodClient(),
twoFactorClient(),
passkeyClient(),
],
});
still the issue persists @Ping can you please help me ? its urgent
Ping
Ping4w ago
if you check ur db, does each session actually have an activeOrganizationId?
Rohit
RohitOP4w ago
@Ping yes
No description
Rohit
RohitOP4w ago
and this is happening with all use hooks from better auth
Ping
Ping4w ago
can you show me how you're calling the hooks?
Rohit
RohitOP4w ago
const { data: organization } = authClient.useActiveOrganization(); this way
Ping
Ping4w ago
I mean the environment it's at, such as within rsc or normal components.
Rohit
RohitOP4w ago
Rohit
RohitOP4w ago
whatever hook i use it is not working if it is useSession or useActiveOrganization or any
Ping
Ping4w ago
are you checking the isPending state? In your code I noticed you're passing the organization to a commented out codeblock where you grab the team based on the org - but you need to check if useActiveOrganization is pending first, since while it's pending the result is always null
Rohit
RohitOP4w ago
i think no but is it not cached ?
Ping
Ping4w ago
it will still hit your server, the caching prevents it from hitting your DB. it still takes time to respond the result from the server I recommend checking your dev tools in your browser to check the request and see it's results from there
Rohit
RohitOP4w ago
ok let me check it and there is another issue don't know it that is the issue when i use use hook from react for authClient promis functions like getSession then it is infinite
Ping
Ping4w ago
can you clarify? I don't understand your question
Rohit
RohitOP4w ago
const { data: session, isPending } = React.use(
authClient.getSession()
);
const { data: session, isPending } = React.use(
authClient.getSession()
);
when i do this the api cals done is infinite
Rohit
RohitOP4w ago
No description
Rohit
RohitOP4w ago
@Ping
const { data: organization, isPending } = authClient.useActiveOrganization();

React.useEffect(() => {
console.log("organization:", organization);
}, [organization]);

if (isPending) {
return <div>Loading...</div>;
}
const { data: organization, isPending } = authClient.useActiveOrganization();

React.useEffect(() => {
console.log("organization:", organization);
}, [organization]);

if (isPending) {
return <div>Loading...</div>;
}
this should work right for atleast logging ? even then it is not working
Ping
Ping4w ago
Check your network tab in dev tools, what does that show?
Rohit
RohitOP4w ago
when i use hooks there is api call done { "name": "Testing", "slug": "testing", "logo": null, "createdAt": "2025-09-23T04:56:20.405Z", "metadata": null, "id": "gtdauEtdizzbc09NpkAzTyM7o4rnYlnW", "invitations": [], "members": [ { "organizationId": "gtdauEtdizzbc09NpkAzTyM7o4rnYlnW", "userId": "R9o0j3Nx9tWyWhJkrZR0OTqhKuvWnWZU", "role": "owner", "createdAt": "2025-09-23T04:56:21.527Z", "id": "WGM0IcoiQOfYJyQd7HiqX78VRkz76Kqs", "user": { "id": "R9o0j3Nx9tWyWhJkrZR0OTqhKuvWnWZU", "name": "Engineer Boy", "email": "engineerboy.ran@gmail.com", "image": "https://lh3.googleusercontent.com/a/ACg8ocIfABbnoYxPH7tPLh9kyu42SXdLi2mZVkKWf-eVATnE2OYGAgM=s96-c" } } ], "teams": [ { "name": "Testing", "organizationId": "gtdauEtdizzbc09NpkAzTyM7o4rnYlnW", "createdAt": "2025-09-23T04:56:22.058Z", "updatedAt": "2025-09-23T04:56:22.060Z", "id": "0Z9PPwQXl1uNGCpVdrxl5B397cNwEv0n" } ] } and also responde but the hook in the code return null
Ping
Ping4w ago
try to modify your useEffect to add isPending logs, and update the dependency array too also add error in case something popped up
Rohit
RohitOP4w ago
const { isPending, data, error, isRefetching } =
authClient.useActiveOrganization();

React.useEffect(() => {
console.log(isPending, data, error, isRefetching);
}, [isPending, data, error, isRefetching]);
const { isPending, data, error, isRefetching } =
authClient.useActiveOrganization();

React.useEffect(() => {
console.log(isPending, data, error, isRefetching);
}, [isPending, data, error, isRefetching]);
Rohit
RohitOP4w ago
this is printed
No description
Rohit
RohitOP4w ago
only three logs and pending is never gets false and data is not returned
Ping
Ping4w ago
it could be possible that there are other authClient requests that are on-going causing it to triggering these. Try making a test page in your nextjs app and specifically run this useEffect hook to test
Rohit
RohitOP4w ago
i did not get it
Ping
Ping4w ago
Just to test - please make a new page.tsx and test and see if you run into the same issue on a fresh page ^^ and add the useEffect of course

Did you find this page helpful?