【问题标题】:How to specify a custom error message in android using cognito如何使用 cognito 在 android 中指定自定义错误消息
【发布时间】:2019-02-09 08:54:00
【问题描述】:

我目前有一个使用 amazon-cognito-sdk 进行用户登录的 android 应用程序。当用户不存在或输入错误密码时显示的消息不是很好,所以我想自定义它,但我可以没有办法做到这一点?

【问题讨论】:

  • 好问题。我需要 AuthenticationError 类吗?

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


【解决方案1】:

我在工作中也做过类似的事情。基本上,我们将 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))
            }
        }

【讨论】:

  • 我需要 AuthenticationError 类?
【解决方案2】:

这是一个在 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)
        }
    }
}

【讨论】:

    猜你喜欢
    • 2021-03-16
    • 2015-09-22
    • 2014-07-10
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 2011-08-14
    相关资源
    最近更新 更多