sKeИ ちりこ
sKeИ ちりこ2mo ago

Issues when adding additional fields to the user schema in Convex + Better Auth

So I am using the Local Install method to create my schema and setup convex with better auth. I'm following the processes in the guide docs for convex and better auth. https://convex-better-auth.netlify.app/local-install The issue is that the scheme is being generated when I run the following command:
pnpx @better-auth/cli generate -y --output generatedSchema.ts
pnpx @better-auth/cli generate -y --output generatedSchema.ts
I can see the additional fields that I setup for my table in this generated file but when I try to login now I get an error from betterauth saying unable to create user. Note: Everything was working until I added additional fields. Additional fields in my auth.ts file:
export const createAuth = (
ctx: GenericCtx<DataModel>,
{ optionsOnly } = { optionsOnly: false }
) => {
return betterAuth({
logger: {
disabled: optionsOnly,
},
baseURL: siteUrl,
database: authComponent.adapter(ctx),
user: {
// Add any additional fields to the user model here
additionalFields: {
bio: {
type: "string",
required: false,
},
about: {
type: "string",
required: false,
},
isPrivate: {
type: "boolean",
required: false,
defaultValue: false,
},
isImageStorage: {
type: "boolean",
required: false,
defaultValue: false,
},
badges: {
type: "string[]",
required: false,
},
},
},
plugins: [
// The Convex plugin is required for Convex compatibility
convex(),
passkey(),
username(),
],
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
},
github: {
clientId: process.env.GITHUB_CLIENT_ID as string,
clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
},
},
});
};
export const createAuth = (
ctx: GenericCtx<DataModel>,
{ optionsOnly } = { optionsOnly: false }
) => {
return betterAuth({
logger: {
disabled: optionsOnly,
},
baseURL: siteUrl,
database: authComponent.adapter(ctx),
user: {
// Add any additional fields to the user model here
additionalFields: {
bio: {
type: "string",
required: false,
},
about: {
type: "string",
required: false,
},
isPrivate: {
type: "boolean",
required: false,
defaultValue: false,
},
isImageStorage: {
type: "boolean",
required: false,
defaultValue: false,
},
badges: {
type: "string[]",
required: false,
},
},
},
plugins: [
// The Convex plugin is required for Convex compatibility
convex(),
passkey(),
username(),
],
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
},
github: {
clientId: process.env.GITHUB_CLIENT_ID as string,
clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
},
},
});
};
No description
19 Replies
erquhart
erquhart2mo ago
Are you importing everything from generatedSchema.ts into schema.ts?
sKeИ ちりこ
sKeИ ちりこOP2mo ago
yes
erquhart
erquhart2mo ago
If so, check your Convex logs in the dashboard when this happens, there should be useful error messages
sKeИ ちりこ
sKeИ ちりこOP2mo ago
import { defineSchema } from "convex/server";
import { tables } from "./generatedSchema";

const schema = defineSchema({
...tables,
});

export default schema;
import { defineSchema } from "convex/server";
import { tables } from "./generatedSchema";

const schema = defineSchema({
...tables,
});

export default schema;
I have this in my schema file Here is the public repo link if the overall context is helpful: https://github.com/fensken/Convex-Better-Auth-Local-Install So the issue is that my schema is generated in generatedSchema.ts file properly, but the schema is not the same in my dashboard. The one on my dashboard does not include the additional fields that I added. The logs in my dashboard also tell me that the value is not matching the validator. Is it something that is wrong with my configuration?
erquhart
erquhart2mo ago
Sounds like your schema isn't getting deployed. If this is in development, make sure npx convex dev is running
sKeИ ちりこ
sKeИ ちりこOP2mo ago
I got through the issue where the schema was mismatched between the generatedSchema file and the dashboard. Basically after setting up the new convex/convex.config.ts file for local install, the _generated folder was not created within the betterAuth directory. After this folder was created, the schema seems to be matching on both of the sides and my additional fields are also showing. Now, I'm having a new issues which says the following:
Sep 25, 21:05:59.134
H

POST /api/auth/sign-in/social
error
# SERVER_ERROR: [Error: Couldn't resolve betterAuth.adapter.create]
Sep 25, 21:05:59.134
H

POST /api/auth/sign-in/social
error
# SERVER_ERROR: [Error: Couldn't resolve betterAuth.adapter.create]
This makes me believe that previously the local was not being used and my auth was working as a default setup. I'm stuck at this part right now.
No description
erquhart
erquhart2mo ago
Note: generatedSchema.ts isn't a requirement, it's a pattern to use when you need to add custom indexes. Can you share your convex/betterAuth/adapter.ts file
sKeИ ちりこ
sKeИ ちりこOP2mo ago
This is the file;
import { createApi } from "@convex-dev/better-auth";
import schema from "./schema";
import { createAuth } from "../auth";

export const {
create,
findOne,
findMany,
updateOne,
updateMany,
deleteOne,
deleteMany,
} = createApi(schema, createAuth);
import { createApi } from "@convex-dev/better-auth";
import schema from "./schema";
import { createAuth } from "../auth";

export const {
create,
findOne,
findMany,
updateOne,
updateMany,
deleteOne,
deleteMany,
} = createApi(schema, createAuth);
erquhart
erquhart2mo ago
Is npx convex dev running and successfully deploying?
sKeИ ちりこ
sKeИ ちりこOP2mo ago
Ohh okay so if my custom fields do not need indexes then i can just proceed with generating it on the schema file Yes
erquhart
erquhart2mo ago
yep Can you share your convex/auth.ts
sKeИ ちりこ
sKeИ ちりこOP2mo ago
Here is the latest commit on the repo I'm trying to create a started kit or myself so I'm updating it on the repo
erquhart
erquhart2mo ago
was there supposed to be a link
sKeИ ちりこ
sKeИ ちりこOP2mo ago
GitHub
GitHub - fensken/Convex-Better-Auth-Local-Install
Contribute to fensken/Convex-Better-Auth-Local-Install development by creating an account on GitHub.
erquhart
erquhart2mo ago
If it can't resolve betterAuth.adapter.create, it sounds like the adapter functions aren't deployed. You can check in your dashboard to confirm.
sKeИ ちりこ
sKeИ ちりこOP2mo ago
Adapter functions are there and they do work. I was able to create a test user from here.
No description
erquhart
erquhart2mo ago
adapter is spelled wrong
No description
sKeИ ちりこ
sKeИ ちりこOP2mo ago
Ohh okay. 😅 a silly mistake on my part. It is working now. Thanks a ton!! 🙏 One more thing: On the client side it is recommend that we use the convexe's useConvexAuth() instead of the useSession() provided by better auth since better auth can give the session before convex. (or wrap with Authenticated component before using the useSession() hook) Is there a reason why we don't provide the user data on this hook? Or is there another hook that I may have missed. I do understand that because of how query subscriptions work this might have been omitted but I was just curious since better auth, clerk and others provide user information either thorugh useSession() or useUser().
erquhart
erquhart2mo ago
User data isn't provided because it has to be fetched from the db, and Convex doesn't make any assumptions about how you do that. User data from the jwt claims isn't assumed to be accurate, it could change after the token is created. So in keeping with how Convex generally works, the recommendation is to query your user data with a Convex function.

Did you find this page helpful?