[GH-ISSUE #494] Importing User Additional Fields From Other File Failed!!! #8294

Closed
opened 2026-04-13 03:22:14 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @0xPratikPatil on GitHub (Nov 11, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/494

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). Original GitHub issue: https://github.com/better-auth/better-auth/issues/494 **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
GiteaMirror added the locked label 2026-04-13 03:22:14 -05:00
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.

<!-- gh-comment-id:2468208375 --> @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.

<!-- gh-comment-id:2468239222 --> @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;
<!-- gh-comment-id:2468363092 --> @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.

<!-- gh-comment-id:2468379372 --> @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#8294