use `updateUser` in route handler

yo so ik u use authClient on client side but if i want to Update User Information on server side (from a route handler) what do i do? docs show await authClient.updateUser({ image: "https://example.com/image.jpg", name: "John Doe", }) but idt i can use authClient and pass user info on a route handler I want to set that value to a field the client cant see my config: user: { additionalFields: { // when user uploads / or edits raw data, it gets added to their user object raw_data_mongo_ref_id: { // (TODO: make sure this is updated when edited) type: "string", required: false, default: "null", input: false, // don't allow users to set this value }, }, }, using mongo as my db
Solution:
And for server from your auth.ts
Jump to solution
18 Replies
Soheel
Soheel4mo ago
Get session and then call auth.api.updateUser on the server https://www.better-auth.com/docs/concepts/api
API | Better Auth
Better Auth API.
Elite
EliteOP4mo ago
So can I not do it directly In my route handler? The instantiation of the auth object Or do I just import the connection I made in lib folder
Soheel
Soheel4mo ago
Can not do what directly in the route handler ? Ahh
Soheel
Soheel4mo ago
https://www.better-auth.com/docs/basic-usage Do you understand the basic usage of authClient and auth ?
Basic Usage | Better Auth
Getting started with Better Auth
Soheel
Soheel4mo ago
You define your auth and authClient, on the server you can use auth.api, on client authClient The server has equivalent functionality, only difference is that you need to pass some identifiers (often userId that you can get from the session) to update user data
Elite
EliteOP4mo ago
Ah so I import my auth from the lib file right
Soheel
Soheel4mo ago
Yes
Elite
EliteOP4mo ago
For server Alr ty
Soheel
Soheel4mo ago
For client it’s from your e.g. auth-client.ts
Solution
Soheel
Soheel4mo ago
And for server from your auth.ts
Elite
EliteOP4mo ago
yes tysm
Elite
EliteOP4mo ago
hey so im not able to update a field properly
await auth.api.updateUser({
raw_data_mongo_ref_id: updatedDoc._id.toString(),
});
await auth.api.updateUser({
raw_data_mongo_ref_id: updatedDoc._id.toString(),
});
Object literal may only specify known properties, and 'raw_data_mongo_ref_id' does not exist in type '{ body: Partial<Prettify<{} & {} & { name?: string | undefined; image?: string | null | undefined; }>>; method?: "POST" | undefined; query?: Record<string, any> | undefined; ... 6 more ...; path?: string | undefined; }'.ts(2353) what im updating is an additional value to the schema can i not do that with this method do i need to go into db and change it
Soheel
Soheel4mo ago
Hm you are right I also dont find a way to address the user id in the api.updateUser function I looked up my codebase and found that I update user data (except for the password) using my prisma adapter myself The password I update using auth.$context
Elite
EliteOP4mo ago
im trying to just update an existing field. so would i go into the db and send a query? using mongo
Soheel
Soheel4mo ago
I have no experience about mongodb to be clear but yes you can just use your way of changing data in the DB
Elite
EliteOP4mo ago
alr do i change the user table or accounts table?
Soheel
Soheel4mo ago
Depends on what data you need to change ?
Elite
EliteOP4mo ago
its an additional value i added to the schema in auth.ts

Did you find this page helpful?