Skip to content

Commit 8438efd

Browse files
Merge pull request #190 from frontegg/FR-22335-session-for-refresh-token-lifetime
Improvement for autorefresh token. Disabled session alive-time for offline mode.
2 parents 11320a2 + 27fad98 commit 8438efd

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

android/src/main/java/com/frontegg/android/services/FronteggAuthService.kt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ class FronteggAuthService(
6666
// Reconnecting state
6767
val isReconnecting = MutableLiveData<Boolean>()
6868

69-
// Maximum wait time for network recovery (3 minutes)
70-
private val maxWaitTimeMs = 3 * 60 * 1000L
69+
// No timeout for network recovery - wait indefinitely
70+
private val maxWaitTimeMs = Long.MAX_VALUE
7171

7272

7373
companion object {
@@ -278,14 +278,14 @@ class FronteggAuthService(
278278
}
279279

280280
if (!networkOk) {
281-
Log.w(TAG, "Network quality check timeout after ${maxWaitTimeMs}ms, clearing credentials")
281+
Log.w(TAG, "Network quality check timeout, clearing credentials")
282282
clearCredentials()
283283
return false
284284
}
285285

286286
isReconnecting.postValue(false)
287287

288-
val success = sendRefreshToken()
288+
val success = sendRefreshToken(isManualCall = true)
289289
if (success) {
290290
val processedCount = requestQueue.processAll()
291291
Log.d(TAG, "Processed $processedCount queued requests")
@@ -369,10 +369,8 @@ class FronteggAuthService(
369369

370370
override fun refreshTokenIfNeeded(): Boolean {
371371
return synchronized(refreshMutex) {
372-
// Check if auto refresh is disabled
373-
if (disableAutoRefresh) {
374-
return false
375-
}
372+
// Manual refresh token calls should always work, regardless of disableAutoRefresh
373+
// disableAutoRefresh only blocks automatic refresh operations
376374

377375
// Check network quality before attempting refresh token
378376
if (!isNetworkGoodSync()) {
@@ -874,8 +872,9 @@ class FronteggAuthService(
874872
* @throws Exception if an error occurs during the process.
875873
*/
876874
@Throws(IllegalArgumentException::class, IOException::class)
877-
fun sendRefreshToken(): Boolean {
878-
if (disableAutoRefresh) {
875+
fun sendRefreshToken(isManualCall: Boolean = false): Boolean {
876+
// Only block automatic refresh operations, not manual calls
877+
if (!isManualCall && disableAutoRefresh) {
879878
return false
880879
}
881880

android/src/test/java/com/frontegg/android/services/FronteggAuthServiceTest.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,18 @@ class FronteggAuthServiceTest {
211211
}
212212

213213
@Test
214-
fun `refreshTokenIfNeeded should return false if Exception`() {
215-
every { apiMock.refreshToken(any()) }.throws(Exception())
216-
// Mock network check to return true so it doesn't queue the request
214+
fun `refreshTokenIfNeeded should work even when auto refresh is disabled`() {
215+
every { apiMock.refreshToken(any()) }.returns(authResponse)
217216
every { NetworkGate.isNetworkLikelyGood(any()) }.returns(true)
217+
every { credentialManagerMock.save(any(), any()) }.returns(true)
218+
every { apiMock.me() }.returns(mockk<User>())
218219

220+
// Test that refreshTokenIfNeeded works regardless of disableAutoRefresh setting
221+
// because manual calls should always work
219222
val result = auth.refreshTokenIfNeeded()
220223

221-
verify(exactly = 0) { credentialManagerMock.save(any(), any()) }
222-
assert(result) // Should return true even if exception occurs, as it launches coroutine
224+
// Should return true because manual calls should work even when auto refresh is disabled
225+
assert(result)
223226
}
224227

225228
@Test

0 commit comments

Comments
 (0)