Internal adapter can't get session from secondary storage #978

Closed
opened 2026-03-13 08:14:36 -05:00 by GiteaMirror · 6 comments
Owner

Originally created by @ChetdeJong on GitHub (Apr 4, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

When session is created it's saved in list:
https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/db/internal-adapter.ts#L263

When getting the session it uses session token and never finds the session:
https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/db/internal-adapter.ts#L284

Same issue is present for other functions in internal adapter, where session is being retrieved from secondary storage by token.

Seems like issue was introduced in b27b502055 or in 950969c991

Current vs. Expected behavior

Currently internal adapter cannot get session from secondary storage.

What version of Better Auth are you using?

1.2.6.beta.4

Provide environment information

-

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

export const auth = betterAuth({
	session: {
		storeSessionInDatabase: true,
		preserveSessionInDatabase: true,
		cookieCache: {
			enabled: true,
			maxAge: 60 * 10
		}
	},

	secondaryStorage: {
		get: async (key) => {
			const value = await redis.get(key);
			return value ? value : null;
		},
		set: async (key, value, ttl) => {
			if (ttl) await redis.set(key, value, 'EX', ttl)
			else await redis.set(key, value);
		},
		delete: async (key) => {
			await redis.del(key);
		}
	}
});

Additional context

No response

Originally created by @ChetdeJong on GitHub (Apr 4, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce When session is created it's saved in list: https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/db/internal-adapter.ts#L263 When getting the session it uses session token and never finds the session: https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/db/internal-adapter.ts#L284 Same issue is present for other functions in internal adapter, where session is being retrieved from secondary storage by token. Seems like issue was introduced in https://github.com/better-auth/better-auth/commit/b27b5020554d25b6a24bef475a4bd3909b44ee9b or in https://github.com/better-auth/better-auth/commit/950969c9911a0c4874d41fdcd9f938c984cc546f ### Current vs. Expected behavior Currently internal adapter cannot get session from secondary storage. ### What version of Better Auth are you using? 1.2.6.beta.4 ### Provide environment information ```bash - ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript export const auth = betterAuth({ session: { storeSessionInDatabase: true, preserveSessionInDatabase: true, cookieCache: { enabled: true, maxAge: 60 * 10 } }, secondaryStorage: { get: async (key) => { const value = await redis.get(key); return value ? value : null; }, set: async (key, value, ttl) => { if (ttl) await redis.set(key, value, 'EX', ttl) else await redis.set(key, value); }, delete: async (key) => { await redis.del(key); } } }); ``` ### Additional context _No response_
GiteaMirror added the bug label 2026-03-13 08:14:36 -05:00
Author
Owner

@Bekacru commented on GitHub (Apr 4, 2025):

it's not set in the internal adapter, it's actually set with session cookie to keep it updated whenever a user or session object changes:
https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/cookies/index.ts#L184-L201

@Bekacru commented on GitHub (Apr 4, 2025): it's not set in the internal adapter, it's actually set with session cookie to keep it updated whenever a user or session object changes: https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/cookies/index.ts#L184-L201
Author
Owner

@Bekacru commented on GitHub (Apr 4, 2025):

The code block you mention is just there for listSession use case. If it's not working, check the response await redis.get(key) is actually a string session object.

@Bekacru commented on GitHub (Apr 4, 2025): The code block you mention is just there for `listSession` use case. If it's not working, check the response `await redis.get(key)` is actually a string session object.
Author
Owner

@ChetdeJong commented on GitHub (Apr 4, 2025):

Sorry, my bad. However it's still inconvenient that its only set to secondary storage when session should be updated. If I want to do something with internal adapter it would still return null because session is not set yet to secondary storage.

@ChetdeJong commented on GitHub (Apr 4, 2025): Sorry, my bad. However it's still inconvenient that its only set to secondary storage when session should be updated. If I want to do something with internal adapter it would still return null because session is not set yet to secondary storage.
Author
Owner

@Bekacru commented on GitHub (Apr 5, 2025):

yeah but I recommend using redis directly when you need to update the SS. Also if it's related to pre, post process when an endpoint is hit. Check if you can utilize the hooks api instead

@Bekacru commented on GitHub (Apr 5, 2025): yeah but I recommend using redis directly when you need to update the SS. Also if it's related to pre, post process when an endpoint is hit. Check if you can utilize the `hooks` api instead
Author
Owner

@ChetdeJong commented on GitHub (Apr 5, 2025):

I can put it in SS myself, it just seems to me more logical if it's there by default upon creation. Up to you however, you can close this issue if you don't consider doing it.

@ChetdeJong commented on GitHub (Apr 5, 2025): I can put it in SS myself, it just seems to me more logical if it's there by default upon creation. Up to you however, you can close this issue if you don't consider doing it.
Author
Owner

@Bekacru commented on GitHub (Apr 12, 2025):

unless we have good reason to move it to the internal adapter, we currently don't plan to do this.

@Bekacru commented on GitHub (Apr 12, 2025): unless we have good reason to move it to the internal adapter, we currently don't plan to do this.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#978