#Issue creating organization

6 messages · Page 1 of 1 (latest)

frigid folio
#

Hi All, I am having some issues where I get these types of errors in a bunch of places on operations with the authClient with better-auth using the Component for Convex.

It happens on the listOrganizations and the createOrganization specifically, so mabye its some issue with how i set up the organizations plugin?

[Error: Uncaught TypeError: Cannot read properties of undefined (reading 'fields')
at isUniqueField (../../node_modules/@convex-dev/better-auth/src/client/adapterUtils.ts:67:9)
at <anonymous> (../../node_modules/@convex-dev/better-auth/src/client/adapterUtils.ts:487:19)
at find [as find] (<anonymous>)
at paginate (../../node_modules/@convex-dev/better-auth/src/client/adapterUtils.ts:487:19)
at handler (../../node_modules/@convex-dev/better-auth/src/client/index.ts:198:5)

export const createAuth = (
  ctx: GenericCtx<DataModel>,
  { optionsOnly } = { optionsOnly: false },
) => {
  return betterAuth({
    // Disable logging when createAuth is called just to generate options
    logger: {
      disabled: optionsOnly,
    },
    baseURL: siteUrl,
    database: authComponent.adapter(ctx),

    // Email/password authentication (non-verified for now)
    emailAndPassword: {
      enabled: true,
      requireEmailVerification: false,
      // Optional: add password requirements
      minPasswordLength: 8,
    },

    // User table configuration
    user: {
      // Additional user fields beyond Better Auth defaults
      additionalFields: {
        // Platform-level role - stored in userProfiles instead
        // Don't add custom fields here as they cause validation errors
        // Use triggers to sync to userProfiles table
      },
    },

    plugins: [
      // The Convex plugin is required for Convex compatibility
      convex(),

      // Organization plugin for multi-company support with custom RBAC
      organization({
        // Access Control configuration
        ac,

        // Custom roles with fine-grained permissions
        roles: {
          owner: ownerRole,
          admin: adminRole,
          contest_admin: contestAdminRole,
        },

        // Set creator as owner by default
        creatorRole: "owner",

        // We don't need teams - companies are top-level organizations
        teams: {
          enabled: false,
        },
      }),
    ],
  });
};```
limpid locustBOT
#

Hi @frigid folio. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on your error and the provided code, it appears that the issue is related to the setup of the organization plugin with Better Auth in Convex. The error message:

Cannot read properties of undefined (reading 'fields')
    at isUniqueField (../../node_modules/@convex-dev/better-auth/src/client/adapterUtils.ts:67:9)

suggests that the schema for the organization (or possibly user) table is not being correctly recognized or initialized, likely due to a misconfiguration in your local install or schema generation.

Here are some key points and troubleshooting steps based on the knowledge sources:

  1. Schema Generation and Deployment
    If you are using the local install method to customize your schema (including organizations), you must ensure that:

    • You have generated the schema using the Better Auth CLI.
    • The generated schema is correctly imported and used in your Convex project.
    • The schema is deployed and matches what is expected by Better Auth and Convex.

    If the schema is not deployed or mismatched, you may see errors about missing fields or undefined properties. This is a common issue when adding custom fields or plugins, as described in a Discord support thread. Make sure that after generating your schema, you run npx convex dev to deploy it, and check that the schema in your dashboard matches your generated schema. If the _generated folder is missing or out of sync, this can also cause issues. See the troubleshooting steps here: Discord support thread: Issues when adding additional fields to the user schema in Convex + Better Auth.

#
  1. Plugin Installation
    If you are using the organization plugin, ensure it is properly included in your plugins array in the createAuth function. If the plugin is not installed or Convex is not successfully deploying, you may see 404 or undefined errors when calling organization-related APIs. See: Discord support thread: Use convex-better-auth with organization plugin.

  2. Custom Fields and Validation
    If you add custom fields to the user or organization tables, ensure that these fields are correctly defined in your schema and that the schema is up to date. Mismatches between the schema and the code can cause validation errors or runtime exceptions.

  3. Check for Typos and Adapter Setup
    Double-check that your adapter setup is correct and that there are no typos in your schema or adapter exports. A misspelled adapter or missing export can cause the system to fail to find the necessary fields.

Summary of steps to resolve:

  • Regenerate your schema after any changes using the Better Auth CLI.
  • Ensure the generated schema is imported and used in your Convex schema.
  • Run npx convex dev to deploy the updated schema.
  • Verify in the Convex dashboard that the schema matches your expectations.
  • Ensure the organization plugin is correctly included in your createAuth plugins array.

If you follow these steps and the issue persists, please check your Convex logs for more detailed error messages, as they can provide further clues about what is missing or misconfigured.

If you need more specific help, sharing your schema files and the exact steps you took to generate and deploy them can help pinpoint the issue further.

#

@frigid folio Kapa.ai is still learning and improving, please let me know how I did by reacting below.