Best way for a plugin to extend session object
We’re trying to extend the session object in the
after
hook using createAuthMiddleware
. However, when we fetch the session on the client, it still uses the original session type.
What’s the recommended way to safely extend the session object with custom attributes while maintaining type safety?
We came across the customSession
plugin, which overrides the /get-session
route — but is that really the recommended approach for all plugins? It feels a bit hacky for something that should be more common.Solution:Jump to solution
Yeah I would recommend a new endpoint if using customSession isn't an option for you.
3 Replies
You can extend the session by adding
schema
to your BetterAuthPlugin. This is how you can extend tables.
When fetching on the client, it will always return the same type, and it's by design. We offer the customSession for you to modify the results of getSession
/useSession
to add any other fields you may want in the result.Is there a way to extend the session object at runtime in the middleware context (e.g. in the after hook) without relying on customSession / db changes, or is that fundamentally not supported by design? And if that’s the case, would creating a separate endpoint for enriched session data be considered a clean or recommended approach?
Solution
Yeah I would recommend a new endpoint if using customSession isn't an option for you.