Overdrivr
Overdrivr

Reputation: 6576

recommended way to implement request-password-reset in Loopback

To implement password reset request in loopback (send email to the user with reset link), we need to handle the event 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.

Upvotes: 0

Views: 750

Answers (1)

Serg
Serg

Reputation: 2427

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.

Upvotes: 1

Related Questions