Session Caching with Custom Fields Workaround

I've been using Next Auth and was thinking to switch over, however the line below is making me hesitate. I do not want to hit the db on every request, but I also want to use custom fields such as roles. Is there an alternative or work around for this?
Session caching, including secondary storage or cookie cache, does not include custom fields. Each time the session is fetched, your custom session function will be called.
Session caching, including secondary storage or cookie cache, does not include custom fields. Each time the session is fetched, your custom session function will be called.
7 Replies
DN_Dev
DN_DevOP19h ago
Is using additionalFields a viable option here? Would those fields be cached?
KiNFiSH
KiNFiSH18h ago
what was your usecase for caching may be i can navigate you around ?
DN_Dev
DN_DevOP16h ago
With Next Auth I used short lived JWTs with a table to refresh them so it hit the db every now and then. Better auth uses sessions which I'm ok with but I don't want to hit the db every request. The project I intend to use with better auth is new so not sure what I may need just yet, besides role(s). If I can extend the user and session objects with additionalFields and still use the cookie cache that's fine with me.
bekacru
bekacru15h ago
all additional fields will be cached. Only the callback you pass on custom session will be called
DN_Dev
DN_DevOP14h ago
Did some more reading, is it that additional fields is used to extend the better auth session response but custom session is used to (potentially) overwrite it in a sense?
bekacru
bekacru14h ago
custom session is if you want additional information like querying additional db both session & user table data are cached
DN_Dev
DN_DevOP8h ago
Hmm...I use the table inheritance pattern where user table is the base and the roles share a primary key with the user table. Seems like I would have to use the custom session plugin here, which means no caching

Did you find this page helpful?