Skip to content

Commit 5b8cac2

Browse files
nuno-vieiratestableapple
authored andcommitted
Fix logout clearing token provider before removing the device (#3772)
1 parent 838f3ca commit 5b8cac2

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

Sources/StreamChat/ChatClient.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -508,19 +508,21 @@ public class ChatClient {
508508
completion: @escaping () -> Void
509509
) {
510510
let currentUserController = currentUserController()
511-
512-
// We need to call logout after creating the current user controller.
513-
// Otherwise we would not be able to create the controller without currentUserId.
514-
authenticationRepository.logOutUser()
515-
516511
if removeDevice, let currentUserDevice = currentUserController.currentUser?.currentDevice {
517-
currentUserController.removeDevice(id: currentUserDevice.id) { error in
512+
currentUserController.removeDevice(id: currentUserDevice.id) { [weak self] error in
518513
if let error {
519514
log.error(error)
520515
}
516+
self?.authenticationRepository.logOutUser()
521517
}
522518
}
523519

520+
authenticationRepository.clearCurrentUserId()
521+
522+
if removeDevice == false {
523+
authenticationRepository.logOutUser()
524+
}
525+
524526
// Stop tracking active components
525527
syncRepository.removeAllTracked()
526528

Sources/StreamChat/Repositories/AuthenticationRepository.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ class AuthenticationRepository {
211211
currentUserId = nil
212212
}
213213

214+
func clearCurrentUserId() {
215+
currentUserId = nil
216+
}
217+
214218
func refreshToken(completion: @escaping (Error?) -> Void) {
215219
guard let tokenProvider = tokenProvider else {
216220
let error = ClientError.MissingTokenProvider()

TestTools/StreamChatTestTools/Mocks/StreamChat/Repositories/AuthenticationRepository_Mock.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class AuthenticationRepository_Mock: AuthenticationRepository, Spy {
1313
static let refreshToken = "refreshToken(completion:)"
1414
static let clearTokenProvider = "clearTokenProvider()"
1515
static let logOut = "logOutUser()"
16+
static let clearCurrentUserId = "clearCurrentUserId()"
1617
static let completeTokenWaiters = "completeTokenWaiters(token:)"
1718
static let completeTokenCompletions = "completeTokenCompletions(error:)"
1819
static let setToken = "setToken(token:completeTokenWaiters:)"
@@ -95,6 +96,10 @@ class AuthenticationRepository_Mock: AuthenticationRepository, Spy {
9596
record()
9697
}
9798

99+
override func clearCurrentUserId() {
100+
record()
101+
}
102+
98103
var resetCallCount: Int = 0
99104
override func reset() {
100105
resetCallCount += 1

Tests/StreamChatTests/ChatClient_Tests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ final class ChatClient_Tests: XCTestCase {
331331
expectation.fulfill()
332332
}
333333
/// Erasing current user id should be called right after calling logout.
334-
XCTAssertCall(AuthenticationRepository_Mock.Signature.logOut, on: testEnv.authenticationRepository!)
334+
XCTAssertCall(AuthenticationRepository_Mock.Signature.clearCurrentUserId, on: testEnv.authenticationRepository!)
335335

336336
// THEN
337337
waitForExpectations(timeout: defaultTimeout)

0 commit comments

Comments
 (0)