Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #7687 - Wait for MXSession state update before proceeding with … #7690

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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
Expand Down
Loading