Cognito Login preferred_username whitespace


Hello, in my application, users are signing up with cognito and i'm using the hosted UI. unfortunately it is not possible for the users to choose a username with a whitespace-character at signup. however, my users are able to change their username in the application afterwards, and i'm setting the preferred_username then. However, when they logout and login again, users can't use their preferred_username for login if they changed their username inside my application to something containing a whitespace as their is a validation error returned when trying to login on the hosted ui. Is there a way how i could allow whitespace-characters for login in the hosted ui?

Kind regards Simon

1 réponse

the AWS Cognito hosted UI does not seem to offer built-in support for usernames with whitespace characters. The hosted UI has limited customization options, and it doesn't seem to provide a way to alter the validation rules for usernames​ In Cognito, the username value is a separate attribute that users can't change, but developers can use the preferred_username attribute to give users usernames that they can change. If you mark an attribute as an alias, users can sign in with that attribute in place of the username

One possible workaround to your problem might be to use AWS Lambda triggers to perform custom validation of usernames. AWS Cognito lets you customize much of your authentication workflow with AWS Lambda triggers. To add username validation, you can implement a pre-sign-up Lambda trigger, which lets you perform custom validation and accept or deny the registration request. If you want to declare a sign-up as invalid, all you have to do is return an error from the Lambda function. You can also validate other attributes in the same way or add other custom validation by adding additional checks and raising an exception, with a custom message if it fails​. In your case, you could potentially create a Lambda function that replaces whitespace characters with another acceptable character (like an underscore) before the username is stored in Cognito. You would need to implement similar logic on the client side to ensure that when users enter their username to log in, any whitespace characters are also replaced with the same character.

profile picture
répondu il y a un an

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions