简体   繁体   English

aws-amplify 客户端密码验证

[英]aws-amplify client side password validation

I am using aws-amplify package for integration with Cognito for authentication.我正在使用aws-amplify package 与 Cognito 集成以进行身份验证。 I am creating custom forms for Sign-up.我正在创建自定义 forms 以进行注册。 For that, I wanted to add a client side validation.为此,我想添加一个客户端验证。 Is there any way in aws-amplify for a client side password validation? aws-amplify中有什么方法可以进行客户端密码验证吗? 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.您可以创建完全独立于 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.例如,您可以使用 html 输入属性,例如minLengthpattern ,或者使用 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.目前似乎没有办法通过 Amplify 做到这一点。 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 :您可以在他们简要解决的地方看到这个问题,并提出一个非常好的观点:虽然 Amplify 不进行验证,但 Cognito可以

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.如果这是您想自己解决的问题,您可以先查看 Amazon Cognito API 参考,特别是 DescribeUserPool 操作。 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.也许您可以在构建时调用此操作,将策略保存为 json,您可以将其包含在前端代码中并在运行时验证。

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).这一切都假设您真的不想使用Cognito 托管的 UI进行登录/注册,它将根据这些设置为您处理验证(并且只需要很少的工作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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