Revalidating cookie cache after updating session

Hey

I have a multi-tenancy setup with Nextjs and Better-auth (without using the organization plugin). I'm saving the user's active companyUuid to session and on the frontend side I call a Nextjs server function and update the companyUuid in the database session and refetch the session which kind of works, but after implementing the cookie cache the cache isn't invalidated. What should I do in this case? Or should I be implementing it differently?

export async function switchCompany(companyUuid: string) {
  const session = await validateSession();

  await validateCompanyAccess(session, companyUuid);

  // Update current session row
  await prisma.session.update({
    where: { id: session.session.id },
    data: { currentCompanyUuid: companyUuid },
  });

  return { success: true };
}


<StyledCompanyDiv
                key={company.uuid}
                onClick={async () => {
                  await switchCompany(company.uuid);
                  refetch();
                  clearSearch();
                }}
              >


 session: {
    expiresIn: 30 * 24 * 60 * 60, // 30 days
    cookieCache: {
      enabled: true,
      maxAge: 5 * 60, // Cache duration in seconds
    },
    additionalFields: {
      currentCompanyUuid: {
        type: "string",
        input: false,
        required: false,
      },
    },
  },
Was this page helpful?