[GH-ISSUE #5700] Additional fields declared in the session configuration should be persisted into the cache #27653

Closed
opened 2026-04-17 18:47:06 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @Bruh-Codes on GitHub (Oct 31, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/5700

Is this suited for github?

  • Yes, this is suited for github

Additional fields defaulting to null when you use /api/auth/get-session #5698

it turns out that using /api/auth/get-session does not include additionalFields declared in config into cache.
They come through as null unless you manually merge them via the customSession plugin which even lacks types for including additionalFields

Describe the solution you'd like

calling "/api/auth/get-session" should include additionalFields declared in session configuration

	const { data: session } = await axios.get("/api/auth/get-session", {
		baseURL: request.nextUrl.origin,
		headers: {
			cookie: request.headers.get("cookie") || "",
		},
	});

Describe alternatives you've considered

I had to use the customSession plugin to make this work plus does not include typings for the additionalFieds resutls to default User and Session typings.

plugins: [
		customSession(
			async ({ user, session }: { user: User; session: Session }) => {
				return {
					...session,
					...user,
				};
			}
		),
	],

Additional context

It would be ideal that calling "/api/auth/get-session" would include additionalFields in session declared in auth config
Originally created by @Bruh-Codes on GitHub (Oct 31, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/5700 ### Is this suited for github? - [x] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. Additional fields defaulting to null when you use /api/auth/get-session #5698 it turns out that using /api/auth/get-session does not include additionalFields declared in config into cache. They come through as null unless you manually merge them via the customSession plugin which even lacks types for including additionalFields ### Describe the solution you'd like calling "/api/auth/get-session" should include additionalFields declared in session configuration ``` const { data: session } = await axios.get("/api/auth/get-session", { baseURL: request.nextUrl.origin, headers: { cookie: request.headers.get("cookie") || "", }, }); ``` ### Describe alternatives you've considered I had to use the customSession plugin to make this work plus does not include typings for the additionalFieds resutls to default User and Session typings. ``` plugins: [ customSession( async ({ user, session }: { user: User; session: Session }) => { return { ...session, ...user, }; } ), ], ``` ### Additional context It would be ideal that calling "/api/auth/get-session" would include additionalFields in session declared in auth config
GiteaMirror added the lockedenhancement labels 2026-04-17 18:47:06 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Oct 31, 2025):

Based on what you describe, it appears that the current caching behavior for the get‑session endpoint does not automatically merge or persist additionalFields declared in the session configuration. Instead, those extra fields arrive as null (unless you intervene with a customSession callback), which is consistent with reports that the cookie cache and secondary storage ignore custom fields by default to avoid unintended size or security issues [1].

The recommended short‑term workaround—as you’ve done—is to use the customSession plugin to merge the additionalFields manually. However, this approach suffers from two problems: you must make an extra merge step, and type inference for these added fields isn’t automatically supported.

Next steps include:

  1. Verify you’re using the latest version because similar caching issues have been addressed recently.
  2. If the issue persists, file an enhancement ticket to have the session caching mechanism automatically include additionalFields (with appropriate type inference support) so that get‑session returns the populated fields without a custom plugin.
  3. As a temporary measure, continue using your customSession callback and consider adjusting your TypeScript types manually or through helper functions so that the additionalFields are recognized in the merged result.

This approach should help mitigate the issue until an update allows automatic merging of additionalFields in the cache [2].

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3473102545 --> @better-auth-agent[bot] commented on GitHub (Oct 31, 2025): Based on what you describe, it appears that the current caching behavior for the get‑session endpoint does not automatically merge or persist additionalFields declared in the session configuration. Instead, those extra fields arrive as null (unless you intervene with a customSession callback), which is consistent with reports that the cookie cache and secondary storage ignore custom fields by default to avoid unintended size or security issues [[1]](https://github.com/better-auth/better-auth/issues/3117). The recommended short‑term workaround—as you’ve done—is to use the customSession plugin to merge the additionalFields manually. However, this approach suffers from two problems: you must make an extra merge step, and type inference for these added fields isn’t automatically supported. Next steps include: 1. Verify you’re using the latest version because similar caching issues have been addressed recently. 2. If the issue persists, file an enhancement ticket to have the session caching mechanism automatically include additionalFields (with appropriate type inference support) so that get‑session returns the populated fields without a custom plugin. 3. As a temporary measure, continue using your customSession callback and consider adjusting your TypeScript types manually or through helper functions so that the additionalFields are recognized in the merged result. This approach should help mitigate the issue until an update allows automatic merging of additionalFields in the cache [[2]](https://github.com/better-auth/better-auth/issues/2818). <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) • [Discord](https://discord.gg/better-auth) • [GitHub](https://github.com/better-auth/better-auth) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/better-auth) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/better-auth/better-auth)
Author
Owner

@Bekacru commented on GitHub (Nov 3, 2025):

can you tell me which version of Better Auth you're on?

<!-- gh-comment-id:3481309967 --> @Bekacru commented on GitHub (Nov 3, 2025): can you tell me which version of Better Auth you're on?
Author
Owner

@better-auth-agent[bot] commented on GitHub (Nov 3, 2025):

Based on the reported behavior, it looks like there was a known issue where fields declared in the session configuration weren’t persisted correctly when calling /api/auth/get-session, resulting in them defaulting to null. A recent fix was implemented that uses a proper parsing method to include these additionalFields in the cookie cache, which should resolve the problem [1][2].

To help troubleshoot further, could you please confirm which version of Better Auth you are currently using? If you’re on an older version (for example, 1.3.x), upgrading to a version that includes this fix should resolve the issue.

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3481312316 --> @better-auth-agent[bot] commented on GitHub (Nov 3, 2025): Based on the reported behavior, it looks like there was a known issue where fields declared in the session configuration weren’t persisted correctly when calling /api/auth/get-session, resulting in them defaulting to null. A recent fix was implemented that uses a proper parsing method to include these additionalFields in the cookie cache, which should resolve the problem [[1]](https://github.com/better-auth/better-auth/issues/5700)[[2]](https://github.com/better-auth/better-auth/pull/5735). To help troubleshoot further, could you please confirm which version of Better Auth you are currently using? If you’re on an older version (for example, 1.3.x), upgrading to a version that includes this fix should resolve the issue. <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) • [Discord](https://discord.gg/better-auth) • [GitHub](https://github.com/better-auth/better-auth) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/better-auth) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/better-auth/better-auth)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#27653