Use Cache Directive with getSession
for anyone using getSession in nextjs, and not using cookieCache for the sessions but instead the 'use cache' directive, be aware that calling await headers() like this will make caching not work since headers are non-serializable, @bekacru any workaround here? Maybe only passing the authorization header in the getSession parameter? doing await headers() inside the api.getSession will not be allowed with the use cache directive, so it has to be outside of the functions scope

4 Replies
I've also tried just directly passing down the better-auth.session_token, this doesn't work either

could you use await auth.$context
and use context methods to get session rather than using the endpoint
You could check out source for https://github.com/better-auth/better-auth/blob/dca7fe539d0e1eebd4c92a2e2d016049fcf84cec/packages/better-auth/src/api/routes/session.ts#L94
to see how the get session endpoint is using context
GitHub
better-auth/packages/better-auth/src/api/routes/session.ts at dca7f...
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
specifically how betterauth gets the session using the context internal adapter
https://github.com/better-auth/better-auth/blob/dca7fe539d0e1eebd4c92a2e2d016049fcf84cec/packages/better-auth/src/api/routes/session.ts#L164
GitHub
better-auth/packages/better-auth/src/api/routes/session.ts at dca7f...
The most comprehensive authentication framework for TypeScript - better-auth/better-auth