Update session manually

Hello ! So in my project I have the user login with google setup properly and working. But then the user is redirected to a page where he has to pick one of his projects by selecting it, and I'd like to set the project.id as an additionnalField inside the session. So the session would store: session, user, projectId How can I update the session manually from an api endpoint ? (for example on a POST /api/select-project/{projectId}, the function should setSession key with the projectId value)
12 Replies
G$Row
G$Row5d ago
I'm trying to figure out the same thing.
King Alastor
King Alastor4d ago
this is the only way i've found so far to update the user with additional info
King Alastor
King Alastor4d ago
in combination with the "additionalFields"
Exoden
ExodenOP4d ago
Thank you for the answer 🙂 I actually looked into this, my issue with that is that my user doesn't have the projectId inside his schema, so I was looking for a way to update the additionnalField inside the session. I have seen the updateEmail & updateUser functions, but those are very specific and do not allow you to update the session as you'd want... My guess is that what I want to do is not possible at the moment, but this assumption is very weird based on the depth of the possibilities of the library 🤔
bekacru
bekacru4d ago
you can always make an api endpoint that update the session. and if you want to access it on the session returned from better auth, you can just declare it as additional field.
Exoden
ExodenOP4d ago
What better-auth function should I use to update the session ?
bekacru
bekacru4d ago
you should use your orm directly
Exoden
ExodenOP4d ago
oh ! update directly from the database, I see Why didn't I think about it... I'll try this and let you know It works, thanks @bekacru 😁
varundeva
varundeva3d ago
Session table you can update using orm Also by adding additional field you can get that key value along with better auth Mine also similar case I added many profile fields, i want that to get when user login I'm getting it but I'm using secondary database redis to reduce main db calls on each refresh. Otherwise it call session and user table on each hard refresh There Is no way to update redis session cache when i make change in user table If i logout and login it works fine, i get values. I didn't found any api to update session
Exoden
ExodenOP3d ago
Yes I understand I just noticed that there is a get session query quite often

Did you find this page helpful?