#Help with better-auth + convex

17 messages · Page 1 of 1 (latest)

formal cryptBOT
#

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

#

The error you are encountering, Validator error: Expected one of object..., usually indicates a mismatch between the data being passed to the database and the schema defined for that table. In Convex, the internalAdapter.createUser method eventually triggers a database insert, and if the object contains extra fields or types that don't match your users table definition, it will throw this validation error [Convex test does not validate schemas].

Based on the knowledge sources, here are the likely causes and how to resolve them:

1. Schema Inconsistency

The error message shows you are passing fields like banned and role within the data object:
{"data":{"banned":false, ... "role":"user"},"model":"user"}

If your schema (either the default Better Auth schema or your custom one) does not explicitly define these fields (e.g., banned, role), the validation will fail [Issues when adding additional fields to the user schema in Convex + Better Auth].

To fix this:

#

2. Using t.run for Setup

In Convex testing, while you can use t.run to modify data outside of functions, the internalAdapter methods are designed to work within the Convex environment [Using convex-test].

If you are trying to simulate a full user identity for your service functions, the recommended pattern is to:

  1. Insert the user document directly into the mock database using ctx.db.insert("user", { ... }) inside a t.run block.
  2. Use t.withIdentity to provide the subject (the id from the created user) and issuer that your functions expect [Authentication].

3. Alternative for "Full User Object"

If your functions rely on ctx.auth.getUserIdentity() and then fetch the full user from the database, you can satisfy this in tests by ensuring the subject in t.withIdentity matches the id of the user you manually inserted:

const user = await t.run(async (ctx) => {
  const id = await ctx.db.insert("user", {
    email: "[email protected]",
    name: "Hello World",
    emailVerified: true,
    // ensure all fields match your schema exactly
  });
  return await ctx.db.get(id);
});

const asUser = t.withIdentity({ subject: user.id });
// Now call your service function
await asUser.query(api.myService.getUserProfile);

The knowledge sources do not explicitly mention the @convex-dev/better-auth/test helper's specific internal implementation for createUser, but the error is a standard Convex Validator error indicating your schema is more restrictive than the data being provided by the adapter.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

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

honest pendant
#

const id = await ctx.db.insert("user", {
email: "[email protected]",
name: "Hello World",
emailVerified: true,
// ensure all fields match your schema exactly
});

Wouldn't work, as I don't have a "user" table. I have better-auth installed as a local component. The auto generated api does not contain a "user" table.

#

@formal crypt

formal cryptBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

Sorry @honest pendant, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:

Perhaps try again in 15mins?

honest pendant
#

@formal crypt

formal cryptBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

Sorry @honest pendant, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:

Perhaps try again in 15mins?

honest pendant
#

It seems to be the admin plugin.

Somehow, the schema is not applied properly, even though generating the schema through the CLI generates the role.

#

@formal crypt

formal cryptBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

Sorry @honest pendant, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:

Perhaps try again in 15mins?

honest pendant
#

@formal crypt

formal cryptBOT
#

Let me check the knowledge base for you again. One moment! :books: