diff --git a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Actions/FetchAuthorizationSession/InformSessionError.swift b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Actions/FetchAuthorizationSession/InformSessionError.swift index c556bf89d4..d21f30363a 100644 --- a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Actions/FetchAuthorizationSession/InformSessionError.swift +++ b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Actions/FetchAuthorizationSession/InformSessionError.swift @@ -23,7 +23,8 @@ struct InformSessionError: Action { switch error { case .service(let serviceError): if isNotAuthorizedError(serviceError) { - event = .init(eventType: .throwError(.sessionExpired)) + event = .init(eventType: .throwError( + .sessionExpired(error: serviceError))) } else { event = .init(eventType: .receivedSessionError(error)) } diff --git a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Operations/Helpers/FetchAuthSessionOperationHelper.swift b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Operations/Helpers/FetchAuthSessionOperationHelper.swift index af3a5c87b5..b9fdbc1d88 100644 --- a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Operations/Helpers/FetchAuthSessionOperationHelper.swift +++ b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Operations/Helpers/FetchAuthSessionOperationHelper.swift @@ -41,9 +41,10 @@ class FetchAuthSessionOperationHelper: DefaultLogger { forceRefresh: forceRefresh) case .error(let error): - if case .sessionExpired = error { + if case .sessionExpired(let error) = error { log.verbose("Session is expired") - let session = AuthCognitoSignedInSessionHelper.makeExpiredSignedInSession() + let session = AuthCognitoSignedInSessionHelper.makeExpiredSignedInSession( + underlyingError: error) return session } else if case .sessionError(_, let credentials) = error { return try await refreshIfRequired( @@ -125,8 +126,9 @@ class FetchAuthSessionOperationHelper: DefaultLogger { return try sessionResultWithFetchError(fetchError, authenticationState: authenticationState, existingCredentials: credentials) - case .sessionExpired: - let session = AuthCognitoSignedInSessionHelper.makeExpiredSignedInSession() + case .sessionExpired(let error): + let session = AuthCognitoSignedInSessionHelper.makeExpiredSignedInSession( + underlyingError: error) return session default: let message = "Unknown error occurred" diff --git a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/StateMachine/CodeGen/Errors/AuthorizationError.swift b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/StateMachine/CodeGen/Errors/AuthorizationError.swift index 771c1aed5a..d107152094 100644 --- a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/StateMachine/CodeGen/Errors/AuthorizationError.swift +++ b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/StateMachine/CodeGen/Errors/AuthorizationError.swift @@ -15,14 +15,14 @@ enum AuthorizationError: Error { case service(error: Swift.Error) case invalidState(message: String) case sessionError(FetchSessionError, AmplifyCredentials) - case sessionExpired + case sessionExpired(error: Error) } extension AuthorizationError: AuthErrorConvertible { var authError: AuthError { switch self { - case .sessionExpired: - return .sessionExpired("", "", nil) + case .sessionExpired(let error): + return .sessionExpired("", "", error) case .configuration(let message): return .configuration(message, "") case .service(let error): diff --git a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/Helpers/AuthCognitoSignedInSessionHelper.swift b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/Helpers/AuthCognitoSignedInSessionHelper.swift index fbc1cf8d0d..2db23e0ce7 100644 --- a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/Helpers/AuthCognitoSignedInSessionHelper.swift +++ b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/Helpers/AuthCognitoSignedInSessionHelper.swift @@ -32,18 +32,21 @@ struct AuthCognitoSignedInSessionHelper { return authSession } - static func makeExpiredSignedInSession() -> AWSAuthCognitoSession { + static func makeExpiredSignedInSession(underlyingError: Error) -> AWSAuthCognitoSession { let identityIdError = AuthError.sessionExpired( AuthPluginErrorConstants.identityIdSessionExpiredError.errorDescription, - AuthPluginErrorConstants.identityIdSessionExpiredError.recoverySuggestion) + AuthPluginErrorConstants.identityIdSessionExpiredError.recoverySuggestion, + underlyingError) let awsCredentialsError = AuthError.sessionExpired( AuthPluginErrorConstants.awsCredentialsSessionExpiredError.errorDescription, - AuthPluginErrorConstants.awsCredentialsSessionExpiredError.recoverySuggestion) + AuthPluginErrorConstants.awsCredentialsSessionExpiredError.recoverySuggestion, + underlyingError) let tokensError = AuthError.sessionExpired( AuthPluginErrorConstants.cognitoTokensSessionExpiredError.errorDescription, - AuthPluginErrorConstants.cognitoTokensSessionExpiredError.recoverySuggestion) + AuthPluginErrorConstants.cognitoTokensSessionExpiredError.recoverySuggestion, + underlyingError) let authSession = AWSAuthCognitoSession(isSignedIn: true, identityIdResult: .failure(identityIdError),