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ïnsOP5mo ago
Thx for any help
Solution
Jïns
Jïns5mo 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ïnsOP5mo ago
But lets see what mclovin have to say 🙂
McLovin
McLovin5mo ago
Are you using authClient on the frontend? if so make sure you add the inferAdditionalFields plugin
Jïns
JïnsOP5mo 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
McLovin5mo 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ïnsOP5mo ago
thx a lot for your help the boss
McLovin
McLovin5mo 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ïnsOP5mo ago
Yeah Last question How you import the type in the front ?
McLovin
McLovin5mo ago
export type Session = typeof authClient.$Infer.Session; works
Jïns
JïnsOP5mo ago
Yeah but Thats the session I found thats quick fix export type User = typeof auth.$Infer.Session["user"];
McLovin
McLovin5mo 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ïnsOP5mo ago
thx a lot

Did you find this page helpful?