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
I'm trying to figure out the same thing.
User & Accounts | Better Auth
User and account management.
this is the only way i've found so far to update the user with additional info
Database | Better Auth
Learn how to use a database with Better Auth.
in combination with the "additionalFields"
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 🤔
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.
What better-auth function should I use to update the session ?
you should use your orm directly
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 😁
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
Yes I understand I just noticed that there is a get session query quite often