typescriptnext.jsauthorizationregistration

How to extend the core user schema in better-auth


I'm trying to extend the shema of a user account in better-auth (I'm using NextJS and MongoDB). I tried to follow the steps in the documentation, but at the end TypeScript is telling me that the new fields I'm adding are not recognized.

This is my Auth.ts file: (this is not very)

export const auth = betterAuth({
  database: mongodbAdapter(client.db()), // adapt it into mongodb (our database)
  emailAndPassword: {
    enabled: true,
  },
  user: {
    additionalFields: { 
      full_name: {
          type: "string",
          required: true,
          defaultValue: "NO_NAME",
      },
     ... among other fields
    }
  },

});

type Session = typeof auth.$Infer.Session

This is my auth_client file, where I infer the fields that I added in auth

export const authClient = createAuthClient({
  plugins: [inferAdditionalFields<typeof auth>()],
  baseURL: clientEnvs.NEXT_PUBLIC_DOMAIN,
});

This is where I try to do the registration

  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const [name, setName] = useState('');
  const [full_name, setFullName] = useState('');
  .. among others
...

      const { data, error } = await authClient.signUp.email(
        {
          email,
          password,
          name,
          full_name,
          ... among others
        },
        ... the rest is how the program will react

At the end, I recieve this error (in the editor):


[{
    "code": "2353",
    "message": "Object literal may only specify known properties, and 'full_name' does not exist in type 'Prettify<InferSignUpEmailCtx<{ plugins: { id: \"additional-fields-client\"; $InferServerPlugin: { id: \"additional-fields\"; schema: { user: { fields: { full_name: { type: \"string\"; required: true; defaultValue: string; }; description: { ...; }; languages: { ...; }; role: { ...; }; phone_number: { ...; }; }; }; session:...'.",
}]

What I find very strange (probabily because of my lack of understanding of TypeScript) that full_name IS included in the error message (InferServerPlugin: { id: \"additional-fields\"; schema: { user: { fields(HERE): { full_name: { )


Solution

  • Edit because It wasn't too clear. At auth.ts you should add the fields you want in the configuration object.

    export const auth = betterAuth({
      user: {
           additionalFields: {
            phone: {
             type: 'string',
             required: false,
             defaultValue: null,
           input: true // allow user to set role - false with hide this field,
         }
        }
      }
    })
    

    Also you need to add the fields on the auth-client configuration object. I tried as the docs says, using the plugin "inferAddition..." with the typeof auth, but it wasn't working, so this aproach helped and fixed the issue for me.

    export const { signIn, signOut, signUp, useSession } = createAuthClient({
      baseURL: process.env.BETTER_AUTH_URL,
      // plugins: [inferAdditionalFields<typeof auth>()]
      plugins: [
        inferAdditionalFields({
          user: {
            phone: {
              type: 'string',
              required: false
            }
          }
        })
      ]
    });
    

    Lastly add the field in the prisma schema if you are using it and this should fix the problem.

    const { error } = await signUp.email({
            name: data.name,
            email: data.email,
            password: data.password,
            phone: '123' // autocomplete should work
          });