From b08ad642605b22948d85965cc3bb43221532c918 Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Tue, 3 Oct 2023 13:18:16 +0300 Subject: [PATCH] Fixes #7687 - Wait for MXSession state update before proceeding with the login process --- .../Service/MatrixSDK/QRLoginService.swift | 36 ++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/RiotSwiftUI/Modules/Authentication/QRLogin/Common/Service/MatrixSDK/QRLoginService.swift b/RiotSwiftUI/Modules/Authentication/QRLogin/Common/Service/MatrixSDK/QRLoginService.swift index 9cf127bb7d..daf2e93f5f 100644 --- a/RiotSwiftUI/Modules/Authentication/QRLogin/Common/Service/MatrixSDK/QRLoginService.swift +++ b/RiotSwiftUI/Modules/Authentication/QRLogin/Common/Service/MatrixSDK/QRLoginService.swift @@ -265,19 +265,7 @@ class QRLoginService: NSObject, QRLoginServiceProtocol { MXLog.debug("[QRLoginService] Got acess token") - let session = sessionCreator.createSession(credentials: credentials, client: client, removeOtherAccounts: false) - - let cryptoResult = await withCheckedContinuation { continuation in - session.enableCrypto(true) { response in - continuation.resume(returning: response) - } - } - - guard case .success = cryptoResult else { - MXLog.error("[QRLoginService] Failed enabling crypto") - await teardownRendezvous(state: .failed(error: .rendezvousFailed)) - return - } + let session = await createSession(credentials: credentials, client: client) MXLog.debug("[QRLoginService] Session created, sending device details") let successPayload = flow == .SETUP_ADDITIONAL_DEVICE_V1 @@ -363,6 +351,28 @@ class QRLoginService: NSObject, QRLoginServiceProtocol { state = .completed(session: session, securityCompleted: true) } + private func createSession(credentials: MXCredentials, client: AuthenticationRestClient) async -> MXSession { + let session = await sessionCreator.createSession(credentials: credentials, client: client, removeOtherAccounts: false) + + if session.state == .storeDataReady { + return session + } + + await withCheckedContinuation { continuation in + NotificationCenter.default.addObserver(forName: NSNotification.Name.mxSessionStateDidChange, object: session, queue: nil) { notification in + guard let session = notification.object as? MXSession else { + fatalError() + } + + if session.state == .storeDataReady { + continuation.resume() + } + } + } + + return session + } + private func declineRendezvous() async { guard let requestData = try? JSONEncoder().encode(QRLoginRendezvousPayload(type: .loginFinish, outcome: .declined)) else { return