diff --git a/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/LoginWizard.swift b/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/LoginWizard.swift index a8e985cc3..10df2b276 100644 --- a/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/LoginWizard.swift +++ b/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/LoginWizard.swift @@ -54,7 +54,7 @@ class LoginWizard { /// - initialDeviceName: The initial device name. /// - deviceID: The device ID, optional. If not provided or nil, the server will generate one. /// - Returns: An `MXSession` if the login is successful. - func login(login: String, password: String, initialDeviceName: String, deviceID: String? = nil) async throws -> MXSession { + func login(login: String, password: String, initialDeviceName: String, deviceID: String? = nil, resetOthers: Bool = false) async throws -> MXSession { let parameters: LoginPasswordParameters if MXTools.isEmailAddress(login) { @@ -70,16 +70,16 @@ class LoginWizard { } let credentials = try await client.login(parameters: parameters) - return sessionCreator.createSession(credentials: credentials, client: client) + return sessionCreator.createSession(credentials: credentials, client: client, resetOthers: resetOthers) } /// Exchange a login token to an access token. /// - Parameter loginToken: A login token, obtained when login has happened in a WebView, using SSO. /// - Returns: An `MXSession` if the login is successful. - func login(with token: String) async throws -> MXSession { + func login(with token: String, resetOthers: Bool = false) async throws -> MXSession { let parameters = LoginTokenParameters(token: token) let credentials = try await client.login(parameters: parameters) - return sessionCreator.createSession(credentials: credentials, client: client) + return sessionCreator.createSession(credentials: credentials, client: client, resetOthers: resetOthers) } // /// Login to the homeserver by sending a custom JsonDict. diff --git a/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/RegistrationWizard.swift b/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/RegistrationWizard.swift index 4f4df671a..c273672b7 100644 --- a/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/RegistrationWizard.swift +++ b/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/RegistrationWizard.swift @@ -255,7 +255,7 @@ class RegistrationWizard { do { let response = try await client.register(parameters: parameters) let credentials = MXCredentials(loginResponse: response, andDefaultCredentials: client.credentials) - return .success(sessionCreator.createSession(credentials: credentials, client: client)) + return .success(sessionCreator.createSession(credentials: credentials, client: client, resetOthers: false)) } catch { let nsError = error as NSError diff --git a/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/SessionCreator.swift b/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/SessionCreator.swift index 77add2d69..86c8efd77 100644 --- a/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/SessionCreator.swift +++ b/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/SessionCreator.swift @@ -21,8 +21,9 @@ protocol SessionCreatorProtocol { /// - Parameters: /// - credentials: The `MXCredentials` for the account. /// - client: The client that completed the authentication. + /// - resetOthers: Flag to reset other accounts. /// - Returns: A new `MXSession` for the account. - func createSession(credentials: MXCredentials, client: AuthenticationRestClient) -> MXSession + func createSession(credentials: MXCredentials, client: AuthenticationRestClient, resetOthers: Bool) -> MXSession } /// A struct that provides common functionality to create a new session. @@ -34,19 +35,29 @@ struct SessionCreator: SessionCreatorProtocol { self.accountManager = accountManager } - func createSession(credentials: MXCredentials, client: AuthenticationRestClient) -> MXSession { - // Report the new account in account manager + func createSession(credentials: MXCredentials, client: AuthenticationRestClient, resetOthers: Bool) -> MXSession { + // Use identity server provided in the client if credentials.identityServer == nil { credentials.identityServer = client.identityServer } - - let account = MXKAccount(credentials: credentials) - - if let identityServer = credentials.identityServer { - account.identityServerURL = identityServer + + if let account = accountManager.account(forUserId: credentials.userId) { + accountManager.hydrateAccount(account, with: credentials) + return account.mxSession + } else { + if resetOthers { + accountManager.logout(completion: nil) + } + + let account = MXKAccount(credentials: credentials) + + // set identity server of the new account + if let identityServer = credentials.identityServer { + account.identityServerURL = identityServer + } + + accountManager.addAccount(account, andOpenSession: true) + return account.mxSession } - - accountManager.addAccount(account, andOpenSession: true) - return account.mxSession } } diff --git a/RiotSwiftUI/Modules/Authentication/SoftLogout/Coordinator/AuthenticationSoftLogoutCoordinator.swift b/RiotSwiftUI/Modules/Authentication/SoftLogout/Coordinator/AuthenticationSoftLogoutCoordinator.swift index 56764d68f..412685f96 100644 --- a/RiotSwiftUI/Modules/Authentication/SoftLogout/Coordinator/AuthenticationSoftLogoutCoordinator.swift +++ b/RiotSwiftUI/Modules/Authentication/SoftLogout/Coordinator/AuthenticationSoftLogoutCoordinator.swift @@ -178,7 +178,8 @@ final class AuthenticationSoftLogoutCoordinator: Coordinator, Presentable { let session = try await loginWizard.login(login: userId, password: password, initialDeviceName: UIDevice.current.initialDisplayName, - deviceID: deviceId) + deviceID: deviceId, + resetOthers: true) guard !Task.isCancelled else { return } callback?(.success(session: session, password: password)) diff --git a/RiotTests/Modules/Authentication/Mocks/MockSessionCreator.swift b/RiotTests/Modules/Authentication/Mocks/MockSessionCreator.swift index ada8513eb..16f0bd2aa 100644 --- a/RiotTests/Modules/Authentication/Mocks/MockSessionCreator.swift +++ b/RiotTests/Modules/Authentication/Mocks/MockSessionCreator.swift @@ -20,7 +20,7 @@ import Foundation struct MockSessionCreator: SessionCreatorProtocol { /// Returns a basic session created from the supplied credentials. This prevents the app from setting up the account during tests. - func createSession(credentials: MXCredentials, client: AuthenticationRestClient) -> MXSession { + func createSession(credentials: MXCredentials, client: AuthenticationRestClient, resetOthers: Bool) -> MXSession { let client = MXRestClient(credentials: credentials, unauthenticatedHandler: { _,_,_,_ in }) // The handler is expected if credentials are set. return MXSession(matrixRestClient: client) diff --git a/RiotTests/SessionCreatorTests.swift b/RiotTests/SessionCreatorTests.swift index b2a118f65..ec86e6e81 100644 --- a/RiotTests/SessionCreatorTests.swift +++ b/RiotTests/SessionCreatorTests.swift @@ -29,7 +29,7 @@ class SessionCreatorTests: XCTestCase { accessToken: "mock_access_token") let client = MXRestClient(credentials: credentials) client.identityServer = mockIS - let session = sessionCreator.createSession(credentials: credentials, client: client) + let session = sessionCreator.createSession(credentials: credentials, client: client, resetOthers: false) XCTAssertEqual(credentials.identityServer, mockIS) XCTAssertEqual(session.credentials.identityServer, mockIS)