From c44d4ea8c27b8660a361b915f66dbe1c295a88ce Mon Sep 17 00:00:00 2001 From: Harshdeep Singh <6162866+harsh62@users.noreply.github.com> Date: Wed, 21 Aug 2024 20:40:25 -0400 Subject: [PATCH] fix session errors --- .../FetchAuthSessionOperationHelper.swift | 90 +++++-------------- 1 file changed, 23 insertions(+), 67 deletions(-) diff --git a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Operations/Helpers/FetchAuthSessionOperationHelper.swift b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Operations/Helpers/FetchAuthSessionOperationHelper.swift index b4a7aa9d4a..98504afe89 100644 --- a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Operations/Helpers/FetchAuthSessionOperationHelper.swift +++ b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Operations/Helpers/FetchAuthSessionOperationHelper.swift @@ -8,7 +8,7 @@ import Foundation import Amplify -class FetchAuthSessionOperationHelper: DefaultLogger { +class FetchAuthSessionOperationHelper { typealias FetchAuthSessionCompletion = (Result) -> Void @@ -108,85 +108,41 @@ class FetchAuthSessionOperationHelper: DefaultLogger { "Auth plugin is in an invalid state") } - func sessionResultWithError(_ error: AuthorizationError, - authenticationState: AuthenticationState) - throws -> AuthSession { - log.verbose("Received error - \(error)") + func sessionResultWithError( + _ error: AuthorizationError, + authenticationState: AuthenticationState + ) throws -> AuthSession { + log.verbose("Received fetch auth session error - \(error)") var isSignedIn = false - if case .signedIn = authenticationState { - isSignedIn = true - } - switch error { - case .sessionError(let fetchError, let credentials): - return try sessionResultWithFetchError(fetchError, - authenticationState: authenticationState, - existingCredentials: credentials) - case .sessionExpired(let error): - let session = AuthCognitoSignedInSessionHelper.makeExpiredSignedInSession( - underlyingError: error) - return session - default: - let message = "Unknown error occurred" - let error = AuthError.unknown(message) - let session = AWSAuthCognitoSession(isSignedIn: isSignedIn, - identityIdResult: .failure(error), - awsCredentialsResult: .failure(error), - cognitoTokensResult: .failure(error)) - return session - } - } - - func sessionResultWithFetchError(_ error: FetchSessionError, - authenticationState: AuthenticationState, - existingCredentials: AmplifyCredentials) - throws -> AuthSession { + var authError: AuthError = error.authError - var isSignedIn = false if case .signedIn = authenticationState { isSignedIn = true } switch error { - - case .notAuthorized, .noCredentialsToRefresh: - if !isSignedIn { + case .sessionError(let fetchError, _): + if (fetchError == .notAuthorized || fetchError == .noCredentialsToRefresh) && !isSignedIn { return AuthCognitoSignedOutSessionHelper.makeSessionWithNoGuestAccess() - } - - case .service(let error): - var authError: AuthError - if let convertedAuthError = (error as? AuthErrorConvertible)?.authError { - authError = convertedAuthError } else { - authError = AuthError.service( - "Unknown service error occurred", - "See the attached error for more details", - error) + authError = fetchError.authError } - let session = AWSAuthCognitoSession( - isSignedIn: isSignedIn, - identityIdResult: .failure(authError), - awsCredentialsResult: .failure(authError), - cognitoTokensResult: .failure(authError)) + case .sessionExpired(let error): + let session = AuthCognitoSignedInSessionHelper.makeExpiredSignedInSession( + underlyingError: error) return session - default: break - + default: + break } - let message = "Unknown error occurred" - let error = AuthError.unknown(message) - let session = AWSAuthCognitoSession(isSignedIn: isSignedIn, - identityIdResult: .failure(error), - awsCredentialsResult: .failure(error), - cognitoTokensResult: .failure(error)) - return session - } - public static var log: Logger { - Amplify.Logging.logger(forCategory: CategoryType.auth.displayName, forNamespace: String(describing: self)) - } - - public var log: Logger { - Self.log + let session = AWSAuthCognitoSession( + isSignedIn: isSignedIn, + identityIdResult: .failure(authError), + awsCredentialsResult: .failure(authError), + cognitoTokensResult: .failure(authError)) + return session } } + +extension FetchAuthSessionOperationHelper: DefaultLogger { }