gpt4 book ai didi

amazon-web-services - AWS Amplify 身份验证错误

转载 作者:行者123 更新时间:2023-12-03 16:47:14 26 4
gpt4 key购买 nike

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

最佳答案

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

 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}`);
}
}

关于amazon-web-services - AWS Amplify 身份验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65160061/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com