Cognito Login preferred_username whitespace

0

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 Answer
0

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​ https://cloudcompiled.com/tutorials/aws-cognito-hosted-ui/ 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 https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-attributes.html

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​. https://aws.amazon.com/blogs/aws/safely-validating-usernames-with-amazon-cognito/ 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
EXPERT
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions