I currently have an android app that uses amazon-cognito-sdk for user sign in. The message displayed when a user doesn't exist or types in the wrong password is not great so I would like to customise this but I can't see any way to do this?
I currently have an android app that uses amazon-cognito-sdk for user sign in. The message displayed when a user doesn't exist or types in the wrong password is not great so I would like to customise this but I can't see any way to do this?
I have done similar things at work. Basically, we mapped the SDK exception into our internal AuthenticationException class, each type will have its own message. Below find below the sample code in 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()
}
}
Kindly note that above method is called during onFailure in cognition AuthenticationHandler callback.
val authenticationHandler = object : AuthenticationHandler {
...
override fun onFailure(exception: Exception) {
Timber.e("login Failure $exception")
subscriber.onError(toAuthError(exception))
}
}
Here is a quick example in android kotlin of what that class could look like as I found this post helpful.
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)
}
}
}