How to update / refresh Better Auth session without disabling cookie cache?
By Kundan Bhosale
Last Updated on Feb 15, 2026
Auth
Better Auth
Sessions
While I am working on Lipy — India’s local commerce app. I wanted users to be able to create their store and after that I want to quickly update their sessions with their store details.
But I had cookie cache enabled + I have secondary store where I am caching user session in Redis DB and I wanted to update session directly.
So there were two options in front of me —
- Either disable cookie caches & secondary storage — ( which means long response time.)
- Update Redis store directly & expire cookie cache.
I went with second option, Below is an function which helps you update session directly from Redis & then expire cookie data.
Assuming you’re not beginner with Better Auth, I have shared the helper function below:
If you find this function difficult to understand, feel free to ask questions or refer to Better Auth documentation.
import type { Context } from "hono";
import { deleteCookie } from "hono/cookie";
import { auth } from "@/auth";
import env from "@/env";
import { redis } from "@/services/cache";
import type { ServerContext } from "@/types";
export const updateSession = async (
c: Context<ServerContext>,
data: NonNullable<Awaited<ReturnType<typeof auth.api.getSession>>>
) => {
if (!data?.session?.token) throw Error("No token!");
await redis.set(data.session.token, JSON.stringify(data));
await clearSessionDataCookie(c);
return true;
};
export const clearSessionDataCookie = async (c: Context<ServerContext>) => {
const sessData = (await auth.$context).authCookies.sessionData.name;
deleteCookie(c, sessData, {
path: "/",
maxAge: 0,
expires: new Date(0),
sameSite: env.IN_PROD ? "None" : "Lax",
...(env.IN_PROD && {
secure: true,
httpOnly: true,
domain: `.${env.BETTER_AUTH_URL.split(".").slice(1).join(".")}`,
}),
});
};
If you know any better way to accomplish this feel free to comment!