Skip to content

Commit

Permalink
Fixes #7687 - Wait for MXSession state update before proceeding with …
Browse files Browse the repository at this point in the history
…the login process
  • Loading branch information
stefanceriu committed Oct 3, 2023
1 parent f1b6f52 commit 6a22ae5
Showing 1 changed file with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -363,6 +351,24 @@ 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)

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
Expand Down

0 comments on commit 6a22ae5

Please sign in to comment.