Accounts - Forgot Password

I am trying to figure out the flow for users who forget their password. I am NOT using the accounts-ui package.

Referring to Meteor Docs:

var email = tmpl.find('#email').value;

Accounts.forgotPassword(email, function(err){
	if (err) {
	} else {
		alert('Great success!');

At this point I am lost to what I need to do. The docs say Accounts.sendResetPasswordEmail is triggered after forgotPassword is called. That’s fine, but I haven’t written this function, and neither do I believe I can, because the first argument for sendResetPasswordEmail is userId. I can not know the userId, because the user is not logged in (they forgot their password).

Maybe the email is already sent after forgotPassword is called? I’m not sure. And how am I suppose to redirect users to create a new password? Accounts.onResetPasswordLink(callback) needs a token passed to it? Where is this token? I don’t know …

Help needed, thanks!

Well, I couldn’t speak on the details of the other stuff, but this one is easy to solve:

var user = Meteor.users.findOne({'emails.address':email});

… and now you have your userId. Logging the user in now would obviously be bad and not what you want because anyone could have entered that email address. But sending an email now using the Accounts.sendResetPasswordEmail method is quite appropriate.

The token is contained in the url that is generated for the reset password link. Check the docs for Accounts.emailTemplates for that. They say, among other things:

resetPassword.text: A Function that takes a user object and a url, and returns the body text for a reset password email.

Ha! Thank you @seeekr . That was quite simple. Although, when I call Accounts.sendResetPasswordEmail() I’m told the function doesn’t exist and when I wrap it in Meteor.isServer nothing happens at all, including no email being sent.

Any input?


Well, but that’s already better than getting an error message about the function not existing :smiley: (Man, I wish for more subtle / well-designed emoticons on here!)
So then I’d check a couple more things:

  1. Have you set up your basic email stuff using Accounts.emailTemplates? (Though only the from attribute might really matter.)
  2. Can you send any email at all through Meteor? Try just an Email.send call and see whether that does anything. If not email comes then you know it’s either a) messages going to spam or b) messed up outgoing email configuration somewhere, though from what I know Meteor uses Mailgun to send email by default, if nothing else is configured, and that usually works fine.