From a961c646d76bb25a8d68472f4d9baba0b627b76e Mon Sep 17 00:00:00 2001 From: Harsh <6162866+harsh62@users.noreply.github.com> Date: Mon, 15 Jan 2024 11:19:53 -0500 Subject: [PATCH] fix(Auth): Fix multiple continuation resumes in hostedUI (#3466) * fix(Auth): Fix multiple continuation resumes in hostedUI * use weak self. * updated with review comments * adding back return statements. * worked on review comment * fixing availability on iOS --- .../HostedUIASWebAuthenticationSession.swift | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/HostedUI/HostedUIASWebAuthenticationSession.swift b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/HostedUI/HostedUIASWebAuthenticationSession.swift index 45cc3124c2..20f21d5262 100644 --- a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/HostedUI/HostedUIASWebAuthenticationSession.swift +++ b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/HostedUI/HostedUIASWebAuthenticationSession.swift @@ -24,14 +24,15 @@ class HostedUIASWebAuthenticationSession: NSObject, HostedUISessionBehavior { #if os(iOS) || os(macOS) self.webPresentation = presentationAnchor - return try await withCheckedThrowingContinuation { + return try await withCheckedThrowingContinuation { [weak self] (continuation: CheckedContinuation<[URLQueryItem], Error>) in + guard let self else { return } let aswebAuthenticationSession = createAuthenticationSession( url: url, callbackURLScheme: callbackScheme, - completionHandler: { url, error in - + completionHandler: { [weak self] url, error in + guard let self else { return } if let url = url { let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) let queryItems = urlComponents?.queryItems ?? [] @@ -44,9 +45,10 @@ class HostedUIASWebAuthenticationSession: NSObject, HostedUISessionBehavior { let message = "\(error) \(errorDescription)" return continuation.resume( throwing: HostedUIError.serviceMessage(message)) + } else { + return continuation.resume( + returning: queryItems) } - return continuation.resume( - returning: queryItems) } else if let error = error { return continuation.resume( throwing: self.convertHostedUIError(error)) @@ -59,7 +61,13 @@ class HostedUIASWebAuthenticationSession: NSObject, HostedUISessionBehavior { aswebAuthenticationSession.prefersEphemeralWebBrowserSession = inPrivate DispatchQueue.main.async { - aswebAuthenticationSession.start() + var canStart = true + if #available(macOS 10.15.4, iOS 13.4, *) { + canStart = aswebAuthenticationSession.canStart + } + if canStart { + aswebAuthenticationSession.start() + } } }