Extend type user

user: {
changeEmail: {
enabled: true,
sendChangeEmailVerification: async ({ newEmail, url }) => {
///
},
},
additionalFields: {
truc_de_con: {
type: "string",
nullable: true,
},
},
user: {
changeEmail: {
enabled: true,
sendChangeEmailVerification: async ({ newEmail, url }) => {
///
},
},
additionalFields: {
truc_de_con: {
type: "string",
nullable: true,
},
},
I added an additionalFields but when I import type { User } from "better-auth"; I get only the default form
(alias) type User = {
id: string;
name: string;
email: string;
emailVerified: boolean;
createdAt: Date;
updatedAt: Date;
image?: string | null | undefined;
}
(alias) type User = {
id: string;
name: string;
email: string;
emailVerified: boolean;
createdAt: Date;
updatedAt: Date;
image?: string | null | undefined;
}
Solution:
Found a quick fix
export type User = typeof auth.$Infer.Session["user"];
export type User = typeof auth.$Infer.Session["user"];
adding this in the auth.ts
Jump to solution
13 Replies
Jïns
JïnsOP2w ago
Thx for any help
Solution
Jïns
Jïns2w ago
Found a quick fix
export type User = typeof auth.$Infer.Session["user"];
export type User = typeof auth.$Infer.Session["user"];
adding this in the auth.ts
Jïns
JïnsOP2w ago
But lets see what mclovin have to say 🙂
McLovin
McLovin2w ago
Are you using authClient on the frontend? if so make sure you add the inferAdditionalFields plugin
Jïns
JïnsOP2w ago
import { createAuthClient } from "better-auth/client";

export const authClient = createAuthClient({
// URL de base de votre API auth
baseURL: "http://localhost:3000",
});
import { createAuthClient } from "better-auth/client";

export const authClient = createAuthClient({
// URL de base de votre API auth
baseURL: "http://localhost:3000",
});
I have only this in my authClient
McLovin
McLovin2w ago
Here's an example in my code, you can copy the additional fields from your backend code and remove any unneccesary fields like references etc
No description
Jïns
JïnsOP2w ago
thx a lot for your help the boss
McLovin
McLovin2w ago
If you are satisfied with my answer please take the time and use the context window to mark it as solution 🙂
Jïns
JïnsOP2w ago
Yeah Last question How you import the type in the front ?
McLovin
McLovin2w ago
export type Session = typeof authClient.$Infer.Session; works
Jïns
JïnsOP2w ago
Yeah but Thats the session I found thats quick fix export type User = typeof auth.$Infer.Session["user"];
McLovin
McLovin2w ago
Yea I think thats the best way to get the User type. If you use the inferAdditionalFields plugin the type will be inferred automagically though
Jïns
JïnsOP2w ago
thx a lot

Did you find this page helpful?