Skip to content

Commit bd27f88

Browse files
authored
Merge pull request #748 from Iterable/MOB-7857-Add-user-logged-in-check-for-jwt
[MOB-7857] - Add user logged in check
2 parents 27f2321 + 89b3419 commit bd27f88

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

swift-sdk/Internal/AuthManager.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ class AuthManager: IterableAuthManagerProtocol {
142142
ITBInfo()
143143

144144
expirationRefreshTimer = Timer.scheduledTimer(withTimeInterval: interval, repeats: false) { [weak self] _ in
145-
self?.requestNewAuthToken(hasFailedPriorAuth: false)
145+
if self?.localStorage.email != nil || self?.localStorage.userId != nil {
146+
self?.requestNewAuthToken(hasFailedPriorAuth: false)
147+
} else {
148+
ITBDebug("Email or userId is not available. Skipping token refresh")
149+
}
146150
}
147151
}
148152

tests/unit-tests/AuthTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,35 @@ class AuthTests: XCTestCase {
470470
wait(for: [condition1], timeout: testExpectationTimeout)
471471
}
472472

473+
func testAuthTokenRefreshSkippedIfUserLoggedOutAfterReschedule() {
474+
let callbackNotCalledExpectation = expectation(description: "\(#function) - Callback got called. Which it shouldn't when there is no userId or emailId in memory")
475+
callbackNotCalledExpectation.isInverted = true
476+
477+
let authDelegate = createAuthDelegate({
478+
callbackNotCalledExpectation.fulfill()
479+
return nil
480+
})
481+
482+
let expirationRefreshPeriod: TimeInterval = 0
483+
let waitTime: TimeInterval = 1.0
484+
let expirationTimeSinceEpoch = Date(timeIntervalSinceNow: expirationRefreshPeriod + waitTime).timeIntervalSince1970
485+
let mockEncodedPayload = createMockEncodedPayload(exp: Int(expirationTimeSinceEpoch))
486+
487+
let mockLocalStorage = MockLocalStorage()
488+
mockLocalStorage.authToken = mockEncodedPayload
489+
mockLocalStorage.email = nil
490+
mockLocalStorage.userId = nil
491+
492+
let authManager = AuthManager(delegate: authDelegate,
493+
expirationRefreshPeriod: expirationRefreshPeriod,
494+
localStorage: mockLocalStorage,
495+
dateProvider: MockDateProvider())
496+
497+
let _ = authManager
498+
499+
wait(for: [callbackNotCalledExpectation], timeout: 2.0)
500+
}
501+
473502
func testAuthTokenCallbackOnSetEmail() {
474503
let condition1 = expectation(description: "\(#function) - callback didn't get called after setEmail")
475504

0 commit comments

Comments
 (0)