gotrue-csharp icon indicating copy to clipboard operation
gotrue-csharp copied to clipboard

Fix tokens not refreshing when calling RefreshToken() or RetrieveSessionAsync()

Open pBlackCreek opened this issue 8 months ago • 1 comments

What kind of change does this PR introduce?

Removed the expiry checks from RefreshToken() and RetrieveSessionAsync() to fix tokens not refreshing when access token is expired.

What is the current behavior?

When RetrieveSessionAsync() is called the expiry is checked and assumes that the session is invalid. The same check happens in RefreshToken() before actually trying to refresh it, causing it to never properly try to refresh the tokens.

What is the new behavior?

If the access token has expired and AutoRefreshToken is enabled RetrieveSessionAsync() wil try to refresh the tokens. And when manually calling RefreshToken() the method will continue to refresh the token

Additional context

It seems like it was assumed that the expiry was for the whole session, but it is specifically for the access token.

This would close issue #108 and PR #106

pBlackCreek avatar Mar 20 '25 11:03 pBlackCreek

I think there is also a problem in the TokenRefresh, were the client signsout the user in the refresh token function, by checking the access token expiry date instead:


private void CreateNewTimer()
		{
			if (_client.CurrentSession == null || _client.CurrentSession.ExpiresIn == default)
			{
				if (Debug)
					_client.Debug($"No session, refresh timer not started");
				return;
			}

			if (_client.CurrentSession.Expired())
			{
				if (Debug)
					_client.Debug($"Token expired, signing out");
				// TODO: CHECK THIS COULD BE WRONG
				// _client.NotifyAuthStateChange(SignedOut);
				return;
			}

			try
			{
				TimeSpan interval = GetInterval();
				_refreshTimer?.Dispose();
				_refreshTimer = new Timer(HandleRefreshTimerTick, null, interval, Timeout.InfiniteTimeSpan);

				if (Debug)
					_client.Debug($"Refresh timer scheduled {interval.TotalMinutes} minutes");
			}
			catch (Exception e)
			{
				if (Debug)
					_client.Debug($"Failed to initialize refresh timer", e);
			}
		}

mcombalia avatar May 15 '25 06:05 mcombalia