Skip to content

set_session() emits TOKEN_REFRESHED for a token that is still valid #1748

Closed

Description

@hsusul

Describe the bug

set_session() notifies listeners with TOKEN_REFRESHED even when the access token has not expired.

Restoring a session from a stored access token and refresh token is a normal call. auth-js setSession emits SIGNED_IN when exp is still in the future, and TOKEN_REFRESHED only after it actually refreshes an expired token. This client emits TOKEN_REFRESHED for both.

An on_auth_state_change listener that shows the signed-in UI on SIGNED_IN does not run. A listener that treats TOKEN_REFRESHED as "new tokens were issued" records a refresh that did not happen. Sync and async clients share the same notify call.

Reproduction

import base64
import json
import time

import httpx
from supabase_auth import SyncGoTrueClient

user = {
    "id": "11111111-1111-1111-1111-111111111111",
    "aud": "authenticated",
    "app_metadata": {},
    "user_metadata": {},
    "created_at": "2026-01-01T00:00:00Z",
}

def b64(value):
    raw = json.dumps(value, separators=(",", ":")).encode()
    return base64.urlsafe_b64encode(raw).rstrip(b"=").decode()

exp = int(time.time()) + 3600
access_token = ".".join(
    [
        b64({"alg": "HS256", "typ": "JWT"}),
        b64({"exp": exp, "sub": user["id"]}),
        b64({"sig": 1}),
    ]
)

client = SyncGoTrueClient(
    url="http://localhost",
    http_client=httpx.Client(
        transport=httpx.MockTransport(lambda request: httpx.Response(200, json=user))
    ),
    auto_refresh_token=False,
)
events = []
client.on_auth_state_change(lambda event, session: events.append(event))
client.set_session(access_token, "refresh-token")
print(events)

Observed on upstream main 2de8af0ade465cfcf96a305ea388ea5820e587e6 before the patch:

['TOKEN_REFRESHED']

Expected:

['SIGNED_IN']

An already-expired access token still notifies TOKEN_REFRESHED after the refresh request. No GoTrue server was required. The mock returns the user for GET /user, which is the request set_session() makes when the token is still valid.

Library affected / version

supabase_auth 2.32.0. Python 3.13.5.

Related work

Searches of open and closed issues and PRs, local and remote branches, and commits found no report of this event. Discussions are disabled. #1742 is verify_otp() and recovery. #1715 is the refresh callback cancelling itself. Neither changes set_session().

Activity

  1. added
    python
    bug
    on Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    authbugpython

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions