@@ -2030,7 +2030,7 @@ async function getSessionImpl() {
20302030
20312031 const hdrs = await headers ( )
20322032 return recordSessionAuth (
2033- await withoutUnenrolledSession (
2033+ await enforceTwoFactorRequirement (
20342034 await auth . api . getSession ( {
20352035 headers : hdrs ,
20362036 } )
@@ -2046,9 +2046,12 @@ type ResolvedSession = Awaited<ReturnType<typeof auth.api.getSession>>
20462046 * requirement or the membership: someone who joined, or whose organization turned the requirement
20472047 * on, while signed in. Revoking rather than only hiding it means the session also stops working on
20482048 * Better Auth's own endpoints; the app's stale-session recovery then sends them to sign in, where
2049- * they are asked to enroll.
2049+ * they are asked to enroll. Every server read of the current session goes through this — `getSession`
2050+ * and the desktop pages that read the session directly to bypass the cookie cache.
20502051 */
2051- async function withoutUnenrolledSession ( session : ResolvedSession ) : Promise < ResolvedSession > {
2052+ export async function enforceTwoFactorRequirement (
2053+ session : ResolvedSession
2054+ ) : Promise < ResolvedSession > {
20522055 if ( ! session ?. user ?. id ) return session
20532056 /** Better Auth's inferred session type omits plugin fields; read them untyped. */
20542057 const user : Record < string , unknown > = session . user
@@ -2062,13 +2065,16 @@ async function withoutUnenrolledSession(session: ResolvedSession): Promise<Resol
20622065 /** The cookie cache may predate an enrollment; only a fresh read can condemn the session. */
20632066 if ( ! needsTwoFactorSetup ( await getTwoFactorState ( session . user . id ) ) ) return session
20642067 } catch ( error ) {
2065- /** Sign-in is the strict gate; a billing read failing here must not take every request down. */
2068+ /**
2069+ * Fails closed for this request only: a member whose requirement cannot be read is treated as
2070+ * signed out, without revoking a session that may well be compliant.
2071+ */
20662072 logger . error ( 'Could not check the organization two-factor requirement' , {
20672073 userId : session . user . id ,
20682074 organizationId,
20692075 error,
20702076 } )
2071- return session
2077+ return null
20722078 }
20732079
20742080 logger . warn ( 'Revoking a session that does not meet its organization two-factor requirement' , {
0 commit comments