Skip to content

Commit 13fef07

Browse files
committed
fix(Auth) Mapping correct error when Device Operations fail due to user not signed in.
1 parent b16aad5 commit 13fef07

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Dependency/AuthDeviceServiceAdapter.swift

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
//
77

88
import Amplify
9+
#if COCOAPODS
10+
import AWSMobileClient
11+
#else
12+
import AWSMobileClientXCF
13+
#endif
914

1015
struct AuthDeviceServiceAdapter: AuthDeviceServiceBehavior {
1116

@@ -19,8 +24,7 @@ struct AuthDeviceServiceAdapter: AuthDeviceServiceBehavior {
1924
completionHandler: @escaping (Result<[AuthDevice], AuthError>) -> Void) {
2025

2126
awsMobileClient.listDevices { result, error in
22-
if let error = error {
23-
let authError = AuthErrorHelper.toAuthError(error)
27+
if let authError = mapToAuthError(error) {
2428
completionHandler(.failure(authError))
2529
return
2630
}
@@ -42,8 +46,7 @@ struct AuthDeviceServiceAdapter: AuthDeviceServiceBehavior {
4246

4347
guard let device = request.device else {
4448
awsMobileClient.forgetCurrentDevice { error in
45-
if let error = error {
46-
let authError = AuthErrorHelper.toAuthError(error)
49+
if let authError = mapToAuthError(error) {
4750
completionHandler(.failure(authError))
4851
return
4952
}
@@ -52,8 +55,7 @@ struct AuthDeviceServiceAdapter: AuthDeviceServiceBehavior {
5255
return
5356
}
5457
awsMobileClient.forgetDevice(deviceId: device.id) { error in
55-
if let error = error {
56-
let authError = AuthErrorHelper.toAuthError(error)
58+
if let authError = mapToAuthError(error) {
5759
completionHandler(.failure(authError))
5860
return
5961
}
@@ -66,12 +68,29 @@ struct AuthDeviceServiceAdapter: AuthDeviceServiceBehavior {
6668
func rememberDevice(request: AuthRememberDeviceRequest,
6769
completionHandler: @escaping (Result<Void, AuthError>) -> Void) {
6870
awsMobileClient.updateDeviceStatus(remembered: true) { _, error in
69-
if let error = error {
70-
let authError = AuthErrorHelper.toAuthError(error)
71+
if let authError = mapToAuthError(error) {
7172
completionHandler(.failure(authError))
7273
return
7374
}
7475
completionHandler(.success(()))
7576
}
7677
}
78+
79+
private func mapToAuthError(_ error: Error?) -> AuthError? {
80+
guard let error = error else {
81+
return nil
82+
}
83+
84+
// `.notSignedIn` should be handled explicitly and not as part of AuthErrorHelper
85+
if let awsMobileClientError = error as? AWSMobileClientError,
86+
case .notSignedIn = awsMobileClientError {
87+
return AuthError.signedOut(
88+
AuthPluginErrorConstants.userSignedOutError.errorDescription,
89+
AuthPluginErrorConstants.userSignedOutError.recoverySuggestion,
90+
error
91+
)
92+
} else {
93+
return AuthErrorHelper.toAuthError(error)
94+
}
95+
}
7796
}

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Support/Constants/AuthPluginErrorConstants.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ struct AuthPluginErrorConstants {
143143
static let changePasswordUnableToSignInError: AuthPluginErrorString = (
144144
"Could not change password, the user session is expired",
145145
"Re-authenticate the user by using one of the signIn apis")
146+
147+
static let userSignedOutError: AuthPluginErrorString = (
148+
"There is no user signed in to the Auth category",
149+
"SignIn to Auth category by using one of the sign in methods and then try again")
146150
}
147151

148152
// Field validation errors

0 commit comments

Comments
 (0)