How to update additionalFields of user?

I found this in the docs https://www.better-auth.com/docs/plugins/username#update-username and I can see in the update-user.ts file that there is something with additional fields but I can't figure out how to access them.
Username | Better Auth
Username plugin
13 Replies
The Untraceable
The Untraceable2mo ago
you want to add additional fields to users?
Laurin
LaurinOP2mo ago
My user already has additional fields. But I want to know how to use the updateUser function to update those fields. Because now I can only update the username and email
The Untraceable
Ah for your own fields I think you'd need to make your own endpoints for that, but I'm not 100% sure
Hiếu Trần
Hiếu Trần5w ago
I have read the updateUser function of Better Auth code, you can not update email field. And but if you want to update the additional fields, just need to pass them in request body. I just tested. Eg: if your user table has a columnage, then request body would be: {"age": 125, "username": "abcxyzporn"} But if you have a separate table to manage those additional fields, then you can either do it like The Untraceable suggested, or can use databaseHooks -> user -> update -> before/after
Laurin
LaurinOP5w ago
Thank you. I will try it I have tried the client and server variant of it and I get Errors even when I am trying to update just the name.
"use client";

import { authClient } from "@/app/lib/auth-client";

export default function UpdateUser() {
return (
<button
onClick={async () =>
await authClient.updateUser({ name: "new-username" })
}
>
Update User
</button>
);
}
"use client";

import { authClient } from "@/app/lib/auth-client";

export default function UpdateUser() {
return (
<button
onClick={async () =>
await authClient.updateUser({ name: "new-username" })
}
>
Update User
</button>
);
}
ERROR [Better Auth]: ReplyError [ReplyError: ERR value is not an integer or out of range] { command: [Object] } and the server version:
const data = await auth.api.updateUser({
body: {
name: "new-username",
},
});
const data = await auth.api.updateUser({
body: {
name: "new-username",
},
});
[Error [APIError]: ] { status: 'UNAUTHORIZED', body: undefined, headers: {}, statusCode: 401, digest: '223797055' } but the user is signed in
Hiếu Trần
Hiếu Trần5w ago
What Better Auth version and database are you using? For the server code, I noticed that you’re not passing the headers Eg: Using Hono fw
app.post("custom-user-update", async (ctx) => {
const data = await auth.api.updateUser({
body: {
name: "hieudien22222",
},
headers: ctx.req.header(),
});
return ctx.text("dqwdqw");
});
app.post("custom-user-update", async (ctx) => {
const data = await auth.api.updateUser({
body: {
name: "hieudien22222",
},
headers: ctx.req.header(),
});
return ctx.text("dqwdqw");
});
Laurin
LaurinOP5w ago
'use server'

import { auth } from "@/auth";
import { headers } from "next/headers";

export async function updateUser(name: string) {
await auth.api.updateUser({
body: {
name: name,
},
headers: await headers(),
});
}
'use server'

import { auth } from "@/auth";
import { headers } from "next/headers";

export async function updateUser(name: string) {
await auth.api.updateUser({
body: {
name: name,
},
headers: await headers(),
});
}
Error: [ReplyError: ERR value is not an integer or out of range] { command: [Object], digest: '3363862555' } I am using Redis as SecondaryStorage, Postgres with Drizzle, CookieCache and "better-auth": "^1.3.7"
Hiếu Trần
Hiếu Trần5w ago
To resolve this, upgrade to the latest version of better-auth v1.3.13
Laurin
LaurinOP5w ago
I did upgrade but the error is still the same. Even deleted node modules. If I log my secondary storage the ttl is a float 520661.312. But how is this possible?
secondaryStorage: {
get: async (key) => {
const value = await redis.get(key);
return value ? value : null;
},
set: async (key, value, ttl) => {
console.log("setting key", key, value, ttl);
if (ttl) await redis.set(key, value, "EX", ttl);
else await redis.set(key, value);
},
delete: async (key) => {
await redis.del(key);
},
},
secondaryStorage: {
get: async (key) => {
const value = await redis.get(key);
return value ? value : null;
},
set: async (key, value, ttl) => {
console.log("setting key", key, value, ttl);
if (ttl) await redis.set(key, value, "EX", ttl);
else await redis.set(key, value);
},
delete: async (key) => {
await redis.del(key);
},
},
Hiếu Trần
Hiếu Trần5w ago
Oh sorry. I debugged your issue from the source code on GitHub and saw that this bug has already been fixed. It seems that the Better Auth team hasn’t released the latest version yet. Don’t worry, this bug will likely be fixed in the next release, I hope 😁 .
Laurin
LaurinOP5w ago
Which file did you debug?
Hiếu Trần
Hiếu Trần5w ago
internal-adapter.ts At line 57
Laurin
LaurinOP5w ago
Thank you.

Did you find this page helpful?