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..8c1ed9c1e0 100644 --- a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/StateMachine/CodeGen/Errors/AuthorizationError.swift +++ b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/StateMachine/CodeGen/Errors/AuthorizationError.swift @@ -15,14 +15,17 @@ 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( + "Session expired", + "Invoke Auth.signIn to re-authenticate the user", + 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), diff --git a/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/AuthorizationTests/AWSAuthFederationToIdentityPoolTests.swift b/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/AuthorizationTests/AWSAuthFederationToIdentityPoolTests.swift index e3612eaf69..604c0b24d8 100644 --- a/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/AuthorizationTests/AWSAuthFederationToIdentityPoolTests.swift +++ b/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/AuthorizationTests/AWSAuthFederationToIdentityPoolTests.swift @@ -85,7 +85,8 @@ class AWSAuthFederationToIdentityPoolTests: BaseAuthorizationTests { AuthorizationState.configured), AuthState.configured( AuthenticationState.signedOut(.testData), - AuthorizationState.error(.sessionExpired)) + AuthorizationState.error(.sessionExpired( + error: NotAuthorizedException(message: "message")))) ] for initialState in statesToTest { diff --git a/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TestHarness/CodableStates/CodableStates.swift b/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TestHarness/CodableStates/CodableStates.swift index aa967a353e..0dc049110d 100644 --- a/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TestHarness/CodableStates/CodableStates.swift +++ b/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TestHarness/CodableStates/CodableStates.swift @@ -107,7 +107,7 @@ extension FetchAuthSessionState: Codable { extension AuthorizationError: Codable { public init(from decoder: Decoder) throws { - self = .sessionExpired + self = .sessionExpired(error: NotAuthorizedException(message: "message")) } public func encode(to encoder: Encoder) throws {