From 3b4da32e490217c0ecd4eb98c3109378e33eaf35 Mon Sep 17 00:00:00 2001 From: Harshdeep Singh <6162866+harsh62@users.noreply.github.com> Date: Thu, 11 Jan 2024 12:21:41 -0500 Subject: [PATCH] fix(Auth): Fix multiple continuation resumes in hostedUI --- .../HostedUIASWebAuthenticationSession.swift | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/HostedUI/HostedUIASWebAuthenticationSession.swift b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/HostedUI/HostedUIASWebAuthenticationSession.swift index 45cc3124c2..9908da1df5 100644 --- a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/HostedUI/HostedUIASWebAuthenticationSession.swift +++ b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/HostedUI/HostedUIASWebAuthenticationSession.swift @@ -27,6 +27,11 @@ class HostedUIASWebAuthenticationSession: NSObject, HostedUISessionBehavior { return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<[URLQueryItem], Error>) in + // This is a workaround multiple calls to continuations in the ASWebAuthenticationSession + // which is happening due to a possible bug in the iOS platform. The idea is to null out the + // continuation once resumed. + var nullableContinuation: CheckedContinuation<[URLQueryItem], Error>? = continuation + let aswebAuthenticationSession = createAuthenticationSession( url: url, callbackURLScheme: callbackScheme, @@ -42,18 +47,20 @@ class HostedUIASWebAuthenticationSession: NSObject, HostedUISessionBehavior { where: { $0.name == "error_description" } )?.value?.trim() ?? "" let message = "\(error) \(errorDescription)" - return continuation.resume( + nullableContinuation?.resume( throwing: HostedUIError.serviceMessage(message)) + } else { + nullableContinuation?.resume( + returning: queryItems) } - return continuation.resume( - returning: queryItems) } else if let error = error { - return continuation.resume( + nullableContinuation?.resume( throwing: self.convertHostedUIError(error)) } else { - return continuation.resume( + nullableContinuation?.resume( throwing: HostedUIError.unknown) } + nullableContinuation = nil }) aswebAuthenticationSession.presentationContextProvider = self aswebAuthenticationSession.prefersEphemeralWebBrowserSession = inPrivate