如何使用Cognito在android中指定自定义错误消息

时间:2018-09-04 10:36:00

标签: android amazon-web-services amazon-cognito aws-cognito

我目前有一个使用amazon-cognito-sdk进行用户登录的android应用。当用户不存在或输入错误密码时显示的消息不是很好,因此我想对此进行自定义,但是我可以看不到有什么办法吗?

2 个答案:

答案 0 :(得分:3)

我在工作中做了类似的事情。基本上,我们将SDK异常映射到我们的内部AuthenticationException类中,每种类型都有自己的消息。在下面的kotlin示例代码下面找到。

private fun toAuthError(exception: Exception): AuthenticationError {
    return when(exception) {
        is UserNotFoundException -> AuthenticationError.UserNotFound()
        is InvalidParameterException -> AuthenticationError.InvalidParameter()
        is NotAuthorizedException -> AuthenticationError.UserNotFound()
        is InvalidPasswordException -> AuthenticationError.InvalidPassword()
        is InvalidLambdaResponseException -> AuthenticationError.InvalidResponse()
        is LimitExceededException -> AuthenticationError.LimitExceeded()
        is UsernameExistsException -> AuthenticationError.UsernameExists()
        is UserNotConfirmedException -> AuthenticationError.UserNotConfirmed()
        is CodeMismatchException -> AuthenticationError.VerificationCodeMismatch()
        is ExpiredCodeException -> AuthenticationError.VerificationCodeExpired()
        else -> AuthenticationError.UnknownError()
    }
}

请注意,上述方法是在onFailure认知AuthenticationHandler回调期间调用的。

        val authenticationHandler = object : AuthenticationHandler {
            ...
            override fun onFailure(exception: Exception) {
                Timber.e("login Failure $exception")
                subscriber.onError(toAuthError(exception))
            }
        }

答案 1 :(得分:0)

这是android kotlin中的一个简单示例,该类看起来像我发现这篇文章很有帮助。

class CognitoExceptionHelper(val context: Context) {

    /////////////////////////////////////////////////////////////////
    // STATIC MEMBERS
    /////////////////////////////////////////////////////////////////
    companion object {
        private var instance: CognitoExceptionHelper? = null

        @Synchronized
        fun getInstance(context: Context): CognitoExceptionHelper {
            if(instance == null){
                instance = CognitoExceptionHelper(context)
            }

            return instance!!
        }
    }

    /////////////////////////////////////////////////////////////////
    // METHODS
    /////////////////////////////////////////////////////////////////
    fun toAuthError(ex: Exception?): String {
        return when (ex) {
            is UserNotFoundException -> context.getString(R.string.user_not_found)
            is InvalidParameterException -> context.getString(R.string.invalid_parameter)
            is NotAuthorizedException -> context.getString(R.string.user_not_found)
            is InvalidPasswordException -> context.getString(R.string.invalid_password)
            is InvalidLambdaResponseException -> context.getString(R.string.invalid_lambda_response)
            is LimitExceededException -> context.getString(R.string.limit_exceeded)
            is UsernameExistsException -> context.getString(R.string.email_exists)
            is UserNotConfirmedException -> context.getString(R.string.account_not_confirmed)
            is CodeMismatchException -> context.getString(R.string.incorrect_verification_code)
            is ExpiredCodeException -> context.getString(R.string.verification_code_expired)
            is PasswordResetRequiredException -> context.getString(R.string.password_reset_required)
            else -> context.getString(R.string.error_occurred)
        }
    }
}