Importing User Additional Fields From Other File Failed!!! #210

Closed
opened 2026-03-13 07:37:47 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @0xPratikPatil on GitHub (Nov 11, 2024).

Describe the bug
In my app, there are many user fields, so I thought of storing all the additional fields in a separate file to import them into the auth file, but I am unable to do that.

To Reproduce

import { additionalUserFields } from "./additional-fields";
import { betterAuth } from "better-auth";

export const auth = betterAuth({
  database: prismaAdapter(db, {
    provider: "mongodb",
  }),
  user: {
      additionalFields:additionalUserFields
  },
 });
 export const additionalUserFields = {
  timezone: {
    type: "string",
    required: false,
  },
  language: {
    type: "string",
    required: false,
  },
  urls: {
    type: "object",
    required: false,
  },
  pronouns: {
    type: "string",
    required: false,
  },
  bio: {
    type: "string",
    required: false,
  },
};
 

Error

(property) additionalFields?: {
    [key: string]: FieldAttribute<FieldType>;
} | undefined

Expected behavior
It must accept the imported additional fields with proper type safety

Originally created by @0xPratikPatil on GitHub (Nov 11, 2024). **Describe the bug** In my app, there are many user fields, so I thought of storing all the additional fields in a separate file to import them into the auth file, but I am unable to do that. **To Reproduce** ``` import { additionalUserFields } from "./additional-fields"; import { betterAuth } from "better-auth"; export const auth = betterAuth({ database: prismaAdapter(db, { provider: "mongodb", }), user: { additionalFields:additionalUserFields }, }); ``` ``` export const additionalUserFields = { timezone: { type: "string", required: false, }, language: { type: "string", required: false, }, urls: { type: "object", required: false, }, pronouns: { type: "string", required: false, }, bio: { type: "string", required: false, }, }; ``` **Error** ``` (property) additionalFields?: { [key: string]: FieldAttribute<FieldType>; } | undefined ``` **Expected behavior** It must accept the imported additional fields with proper type safety
Author
Owner

@0xPratikPatil commented on GitHub (Nov 11, 2024):

Right now, it works when I add any to additionalUserFields, but proper type safety is still needed. Also, add more documentation for additional user fields.

@0xPratikPatil commented on GitHub (Nov 11, 2024): Right now, it works when I add any to additionalUserFields, but proper type safety is still needed. Also, add more documentation for additional user fields.
Author
Owner

@0xPratikPatil commented on GitHub (Nov 11, 2024):

Also, please add JSON type in the additional fields and any other important field types.

@0xPratikPatil commented on GitHub (Nov 11, 2024): Also, please add JSON type in the additional fields and any other important field types.
Author
Owner

@Bekacru commented on GitHub (Nov 11, 2024):

This is cause the type has a mismatch since ts for example would infer type on the field type as just a string but Better Auth expects a "string" | "number" ... literal values instead. One way you could easily fix this is by casting the object with as const.

For example:

 export const additionalUserFields = {
  timezone: {
    type: "string",
    required: false,
  },
  language: {
    type: "string",
    required: false,
  },
  urls: {
    type: "object",
    required: false,
  },
  pronouns: {
    type: "string",
    required: false,
  },
  bio: {
    type: "string",
    required: false,
  },
} as const;
@Bekacru commented on GitHub (Nov 11, 2024): This is cause the type has a mismatch since ts for example would infer `type` on the field type as just a string but Better Auth expects a "string" | "number" ... literal values instead. One way you could easily fix this is by casting the object with `as const`. For example: ```ts export const additionalUserFields = { timezone: { type: "string", required: false, }, language: { type: "string", required: false, }, urls: { type: "object", required: false, }, pronouns: { type: "string", required: false, }, bio: { type: "string", required: false, }, } as const; ```
Author
Owner

@0xPratikPatil commented on GitHub (Nov 11, 2024):

Yes, it worked. Thank you. You can close the issue.

@0xPratikPatil commented on GitHub (Nov 11, 2024): Yes, it worked. Thank you. You can close the issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#210