AWS Amplify 身份验证错误

chr*_*tel 5 amazon-web-services amazon-cognito aws-amplify aws-amplify-sdk-android

我正在使用 Android Amplify 库。我无法找出从Amplify.Auth.signIn()函数传回的错误类型。我没有在任何地方找到这方面的文档。现在我只是在猜测它会返回什么。我想要的是告诉用户如何从错误中恢复。用户名不存在,密码不正确,格式错误等。阅读源代码我给我的印象是 AmplifyException.recoveryMessage 是我想要的,但这仍然有问题,因为它不允许我自定义消息。

/**
 * Sign in the user to the back-end service and set the currentUser for this application
 * @param username User's username
 * @param password User's password
 */
override fun initiateSignin(username : String, password : String) {
    //Sign in the user to the AWS back-end
    Amplify.Auth.signIn(
        username,
        password,
        {result ->
            if (result.isSignInComplete) {
                Timber.tag(TAG).i("Sign in successful.")

                //Load the user if the sign in was successful
                loadUser()

            } else {
                Timber.tag(TAG).i("Sign in unsuccessful.")
                //TODO:  I think this will happen if the password is incorrect?

            }
        },
        {error ->
            Timber.tag(UserLogin.TAG).e(error.toString())
            authenticationRecoveryMessage.value = error.recoverySuggestion
        }
    )
}
Run Code Online (Sandbox Code Playgroud)

身份验证恢复消息是LiveData我想更新一个快餐栏,它会告诉用户他们需要做什么才能成功登录。我觉得必须有某种方法可以从中获取错误,而我还没有弄清楚。处理给用户的消息的理想方式是使用 XML 字符串进行翻译,所以我真的很想在小吃栏中使用我自己的字符串,但我需要知道注册时可能出现的问题以及正在传达的信息我通过error -> {}回调。

小智 6

我自己在文档中找不到它们,所以我决定记录可能的情况。

 try {
        
        const signInResult = await Auth.signIn({
          username: emailOrPhoneNumber,
          password
        });

        const userId = signInResult.attributes.sub;
        const token =  (await Auth.currentSession()).getAccessToken().getJwtToken();
        console.log(userId, 'token: ', token);
        resolve(new AuthSession(userId, token, false));
      } catch (e) {
        switch (e.message) {
          case 'Username should be either an email or a phone number.':
            reject(`${AuthError.usernameInvalid}:  ${e.message}`);
            break;
          case 'Password did not conform with policy: Password not long enough':
            reject(`${AuthError.passwordTooShort}:  ${e.message}`);
            break;
          case 'User is not confirmed.':
            reject(`${AuthError.userIsNotConfirmed}:  ${e.message}`);
            break;
          case 'Incorrect username or password.':
            reject(`${AuthError.incorrectUsernameOrPassword}:  ${e.message}`);
            break;
          case 'User does not exist.':
            reject(`${AuthError.userDoesNotExist}:  ${e.message}`);
            break;
          default:
            reject(`${AuthError.unknownError}:  ${e.message}`);
        }
      }
Run Code Online (Sandbox Code Playgroud)


Mar*_*oni 5

SignInInitiateAuth在底层使用 Cognito ,因此可以在此处找到错误代码:

https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_InitiateAuth.html#API_InitiateAuth_Errors

code它们在错误字段中可用。