简体   繁体   中英

aws-amplify client side password validation

I am using aws-amplify package for integration with Cognito for authentication. I am creating custom forms for Sign-up. For that, I wanted to add a client side validation. Is there any way in aws-amplify for a client side password validation? I wanted to show an error message to user when he leaves focus from password field.

You can create a client side validation completely independent from Amplify. Since you know the challenges you configured for creating a password, you can simply put that in your custom form.

For example, you could use html input attributes, like minLength and pattern , or use javascript.

<input 
  autoComplete="new-password"
  type="password"
  minLength="6"
  pattern="^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$"
/>

There isn't currently a way to do this via Amplify, it seems. You can see this issue where they briefly address it, and bring up a really good point: while Amplify doesn't do validation, Cognito does :

If this is a problem you would like to tackle yourself, you could start by looking at the Amazon Cognito API reference, specifically the DescribeUserPool operation. It gives you access to the Password policy for your user pool. maybe you could call this operation at build time, save the policy as a json that you can include in your front-end code and verify at runtime.

This is all assuming you really don't want to use the Cognito hosted UI for sign-in/up, which will handle validation according to these settings for you (and require very little work).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM