忘记了aws cognito的密码链接



我开始为我的虚拟ios应用程序探索AWS cognito,尽管在新用户注册期间,我在电子邮件中收到了一个确认链接,点击它可以验证电子邮件是否正确

对于忘记的密码,我们有相同的功能吗?即获得一个链接而不是代码,并将其重定向到我的虚拟网站,用户只需要输入新密码。

提前谢谢。

我可能已经在我的项目中实现了这一点。

它是通过aws cognito中的触发器完成的。

在自定义消息触发器中,设置要触发的lambda函数。

const AWS = require('aws-sdk');
exports.handler = (event, context, callback) => {
var CustomMessage_ForgotPassword = `<style>
p {
display: block;
margin-block-start: 1em;
margin-block-end: 1em;
margin-inline-start: 0px;
margin-inline-end: 0px;
}
</style>
<div id=":x9" class="a3s aXjCH " role="gridcell" tabindex="-1"><p>Hello,</p>
<p>Follow this link to reset your Password. </p>
<p><a href="https://your-website.com/reset-password?confirmation_code=${event.request.codeParameter}&user_name=${event.userName}"> Reset Password </a></p>
<p>If you didn’t ask to change password, you can ignore this email.</p>
<p>Thanks,</p>
<p>Your website team</p>
</div>`

if (event.triggerSource === "CustomMessage_ForgotPassword") {
event.response.emailMessage = CustomMessage_ForgotPassword;
}
callback(null, event);
};

然后在你的网站上制作一条路线来处理这个代码。

是。您可以拨打ForgotPassword端点的电话:

{
"AnalyticsMetadata": { 
"AnalyticsEndpointId": "string"
},
"ClientId": "string",
"SecretHash": "string",
"Username": "string"
}

然后,您需要(从您的网站代码)致电ConfirmForgotPassword端点以重置密码:

{
"AnalyticsMetadata": { 
"AnalyticsEndpointId": "string"
},
"ClientId": "string",
"ConfirmationCode": "string",
"Password": "string",
"SecretHash": "string",
"Username": "string"
}

我忘记了几个月前问的这个问题,想用答案更新它。所以根据AWS文档:
"调用此API会向最终用户发送一条消息,其中包含更改用户密码所需的确认代码。对于用户名参数,您可以使用用户名或用户别名。如果用户存在已验证的电话号码,则将确认代码发送到该电话号码。否则,如果存在已验证电子邮件,则将向该电子邮件发送确认代码。如果已验证的电话号码或已验证的电子邮件都不存在,将引发InvalidParameterException。">
这是AWS文档的链接。
因此,可能有一些变通方法可以实现,但AWS Cognito目前不支持发送忘记密码的自我验证链接。

我知道这个问题已经得到了回答和接受,虽然Cognito确实没有开箱即用,但我想找到一种无缝工作的方法。

以下是我的想法:

  1. 在您的网站上创建一个带有电子邮件输入框的页面。当用户提交时,使用用户池和电子邮件创建一个CognitoUser实例,并对用户调用forgotPassword函数
  2. 创建一个电子邮件拦截器Lambda,如Mayur Shingare的回答所述
  3. 将此Lambda连接到自定义消息触发器。用户现在应该会收到一封带有自定义电子邮件的邮件,其中包含查询参数中的验证码和他的电子邮件,而不是标准验证码电子邮件
  4. 当用户单击链接时,浏览器窗口应打开到您的网站。然后从URL中提取这些查询参数。在这个页面上有两个框,用户可以键入并确认他的密码
  5. 提交新密码后,使用用户池、电子邮件和验证码(来自查询参数)获取CognitoUser实例并调用confirmPassword函数
  6. 成功后,使用新密码以程序方式登录用户,或重定向用户手动登录

对此有什么想法吗?我使用了同样的机制来让用户注册以无缝的方式工作,尽管这需要更多的工作。

您必须触发Lambda函数,并将其附加到用户池(AWS)中的常规设置->触发器->自定义消息。

这是一个例子。

exports.handler = (event, context, callback) => {
// https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-custom-message.html
// dev
if(event.userPoolId === "YOUR USER POOL ID") {
// Identify why was this function invoked
if(event.triggerSource === "CustomMessage_ForgotPassword") {           
event.response.smsMessage = "Your confirmation code is: " + event.request.codeParameter;
event.response.emailSubject = "Confirmation Code";
event.response.emailMessage = "Your confirmation code: " + event.request.codeParameter + "<br/><br/>Please visit this url and provide the requested information: ~your url~";   
}        
}

// Return to Amazon Cognito
callback(null, event);
};

如果您使用的是更新的Node.js版本>=10,下面的版本可能有助于

const AWS = require('aws-sdk');
exports.handler = async (event) => {

var CustomMessage_ForgotPassword = `<style>
p {
display: block;
font-size: 14px;
margin-block-start: 1em;
margin-block-end: 1em;
margin-inline-start: 0px;
margin-inline-end: 0px;
}
</style>
<div>
<p>
<img align="center"
alt="logo"
src=""
width="248"
style="max-width:400px;padding-bottom:0px;vertical-align:bottom;display:inline!important;border:1px none;border-radius:0%;height:auto;outline:none;text-decoration:none"
>
<br/>
<br/>
<p style="font-size=28px;font-weight:bold;">You have submitted a password change request</p>
<p>If this was you click the URL below to change your password</p>
<p><a href="http://localhost:3000/auth/forgot-password?confirmation_code=${event.request.codeParameter}&user_name=${event.userName}"> Reset Password </a></p>

</div>`
if (event.triggerSource === "CustomMessage_ForgotPassword") {
event.response.emailMessage = CustomMessage_ForgotPassword;
}

return event;
};

关键是您发送回了相同的事件对象,而不是构造一些新的响应对象。仅受投票最多的答案启发。谢谢

最新更新