Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2k
How do I update additionalFields on a session?#6148
-
I have additonalFields specified on my session as such: database schema is updated to reflect this. How do I update the activeAdvertiserId on the session and have that re-sync with the client? Currently I'm able to write to the session table directly but this bypasses the cookie cache and means I'm unable to use a secondary storage if I choose to. I installed the openAPI plugin but it only seems possible to update the user, not the session. Should I be storing activeAdvertiserId on the user instead? |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 1 comment
-
Hi@baseonmars! I'mDosu and I’m helping the better-auth team. There isn’t a built-in API in better-auth for updating session.additionalFields (like activeAdvertiserId) and having that change automatically sync with the client. Directly updating the session table bypasses the cookie cache and secondary storage, which can cause desynchronization issues, especially if you plan to use features like secondaryStorage or cookie caching reference. The openAPI plugin only supports updating user fields, not session fields. The recommended pattern is to store fields like activeAdvertiserId on the user instead, since user updates are supported by the /update-user endpoint and are naturally reflected in the session cookie and client session state reference. After updating the user, you can trigger a session refresh on the client (for example, using the refetch method from useSession) to get the updated value. If you must keep activeAdvertiserId on the session, you’d need to implement a custom endpoint that updates the session via the framework’s internalAdapter and then calls setSessionCookie to sync the change with the client, but this is not officially supported and can be brittle. In summary: for reliable persistence and sync, store activeAdvertiserId on the user and update it via /update-user, then refresh the session on the client reference. To reply, just mention@dosu. How did I do?Good |Irrelevant |Incorrect |Verbose |Hallucination |Report 🐛 |Other |
BetaWas this translation helpful?Give feedback.