Session is always null inside a "before hook"

I am trying to access ctx.context.session inside a "before hook", and it is always returning null even when user is signed in. Is this an expected behavior? For context, I set up secondary storage (Upstash Redis), so sessions are there and not persisted on main database (postgreSQL with DrizzleORM). Also: no cookie caching.
2 Replies
bekacru
bekacru3mo ago
use getSessionFromCtx(ctx). On before hook session is most likely not have been fetched by the endpoint, so it could be null.
import {getSessionFromCtx} from "better-auth/api"
import {getSessionFromCtx} from "better-auth/api"
Karamazov89
Karamazov89OP3mo ago
That works, thanks

Did you find this page helpful?