setSessionCookie never clears dont_remember: a persistent sign-in after a rememberMe: false session is never refreshed
Is this suited for github?
- Yes, this is suited for github
To Reproduce
This is the general case behind #10734. #10735 fixes it for stopImpersonating only, by expiring the marker in that route. The same residue comes from any persistent sign-in or sign-up that follows a non-remembered session in the same browser:
- Sign in with
rememberMe: false. The response setssession_tokenwithoutMax-Ageand sets the signeddont_remembercookie. Both are expected. - Without closing the browser, sign in again, as the same or another user, with
rememberMeleft at its defaulttrue.session_tokennow hasMax-Age, butdont_rememberis still in the browser. - Call
/get-sessionafterupdateAgehas passed. The session is not refreshed andexpiresAtdoes not slide, becausegetSessionreturns before the refresh branch whiledont_rememberis present.
The same happens after sign-up, email or username sign-in, and any other route that calls setSessionCookie(ctx, session, false).
Current vs. Expected behavior
Expected: a session the user asked to be remembered is refreshed like any remembered session, whatever an earlier session in the same browser chose.
Current: setSessionCookie sets dont_remember when dontRememberMe is true, but has no branch that expires it when dontRememberMe is explicitly false. In 1.7.6 dist/cookies/index.mjs:
const maxAge = dontRememberMe ? void 0 : ctx.context.sessionConfig.expiresIn;
await ctx.setSignedCookie(ctx.context.authCookies.sessionToken.name, session.session.token, ...);
if (dontRememberMe) await ctx.setSignedCookie(ctx.context.authCookies.dontRememberToken.name, "true", ...);getSession (dist/api/routes/session.mjs, if (dontRememberMe || ctx.query?.disableRefresh)) then skips the refresh for the new, persistent session until the browser session ends. With session restore, that can outlast expiresIn, and the session then expires at its creation time plus expiresIn instead of sliding.
Suggested fix: in setSessionCookie, expire dontRememberToken when dontRememberMe === false is passed explicitly. When it is undefined, it is derived from the existing cookie, so keep it as is there. This would also cover the stopImpersonating case that #10735 fixes in the route.
What version of Better Auth are you using?
1.7.5 in production use. Verified unchanged in 1.7.6 (dist/cookies/index.mjs).
Provide environment information
Node 24, Fastify backend using the Node handler. Not environment-specific: it follows from the cookie logic above.Which area(s) are affected? (Select all that apply)
Backend
Auth config (if applicable)
betterAuth({
session: { expiresIn: 60 * 60 * 24 * 7, updateAge: 60 * 60 * 24 },
plugins: [username(), admin()],
});Additional context
We work around it with an after hook. On a successful response that issued a new session (ctx.context.newSession) without issuing dont_remember itself, it expires a validly signed marker the request carried.
Source: better-auth/better-auth