推荐的方法在Loopback中实现request-password-reset

时间:2022-11-15 13:13:56

To implement password reset request in loopback (send email to the user with reset link), we need to handle the event resetPasswordRequest.

要在环回中实现密码重置请求(使用重置链接向用户发送电子邮件),我们需要处理事件resetPasswordRequest。

This is a possible implementation below

这是下面的可能实现

Client.on('resetPasswordRequest', function(info) {
  var options = {
    type: 'email',
    to: info.email,
    from: '....',
    ...
  };

  Client.email.send(options, function(err, res) {
    if (err) console.log(err);
  });
});

With this approach, if an error occurs it is simply logged to the console. Throwing an error that won't be handled doesn't feel like a better solution either.

使用此方法,如果发生错误,则只需将其记录到控制台。抛出一个无法处理的错误也不是一个更好的解决方案。

Why is it not mentioned in the docs to use an afterRemoteHook to add this logic or even create a new custom endpoint ? Both solutions seem better at handling errors.

为什么文档中没有提到使用afterRemoteHook来添加此逻辑甚至创建新的自定义端点?两种解决方案似乎都更好地处理错误

1 个解决方案

#1


1  

I think your code is based on example application, isn't it? If so, this approach is used by developer of example application but is not required implementation. You may use any other appropriate solution and one is that what you've mentioned in your question.

我认为你的代码是基于示例应用程序的,不是吗?如果是这样,这种方法由示例应用程序的开发人员使用,但不是必需的实现。您可以使用任何其他适当的解决方案,其中一个就是您在问题中提到的内容。

As for emitting event - it has it's advantage. You emit event and immediately send response to request. So client app will not wait until email sending part will send email - this can take from seconds to tens of seconds.

至于发射事件 - 它有它的优点。您发出事件并立即发送响应请求。所以客户端应用程序不会等到电子邮件发送部分将发送电子邮件 - 这可能需要几秒到几十秒。

You may implement email sending log and make another request to it while user is waiting for password reset email thus notify him if any error will occur.

您可以实现电子邮件发送日志,并在用户等待密码重置电子邮件时向其发出另一个请求,从而在发生任何错误时通知他。

From the other hand this is only example but not required implementation for using in production.

另一方面,这仅是示例,但不是生产中使用的必需实现。

#1


1  

I think your code is based on example application, isn't it? If so, this approach is used by developer of example application but is not required implementation. You may use any other appropriate solution and one is that what you've mentioned in your question.

我认为你的代码是基于示例应用程序的,不是吗?如果是这样,这种方法由示例应用程序的开发人员使用,但不是必需的实现。您可以使用任何其他适当的解决方案,其中一个就是您在问题中提到的内容。

As for emitting event - it has it's advantage. You emit event and immediately send response to request. So client app will not wait until email sending part will send email - this can take from seconds to tens of seconds.

至于发射事件 - 它有它的优点。您发出事件并立即发送响应请求。所以客户端应用程序不会等到电子邮件发送部分将发送电子邮件 - 这可能需要几秒到几十秒。

You may implement email sending log and make another request to it while user is waiting for password reset email thus notify him if any error will occur.

您可以实现电子邮件发送日志,并在用户等待密码重置电子邮件时向其发出另一个请求,从而在发生任何错误时通知他。

From the other hand this is only example but not required implementation for using in production.

另一方面,这仅是示例,但不是生产中使用的必需实现。