Skip to content

Instantly share code, notes, and snippets.

@LeCoupa
Last active July 30, 2022 19:34
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save LeCoupa/9879066 to your computer and use it in GitHub Desktop.
Save LeCoupa/9879066 to your computer and use it in GitHub Desktop.
Reset a password with Meteor --> https://github.com/LeCoupa/awesome-cheatsheets
<template name="ForgotPassword">
<form action="/forgot" id="forgotPasswordForm" method="post">
<input id="forgotPasswordEmail" type="text" name="email" placeholder="Email Address">
<input class="btn-submit" type="submit" value="Send">
</form>
<!-- end #forgot-password-form -->
</template>
<template name="ResetPassword">
{{#if resetPassword}}
<form action="/reset-password" id="resetPasswordForm" method="post">
<input id="resetPasswordPassword" name="password" placeholder="New Password" type="password" >
<input id="resetPasswordPasswordConfirm" name="password-confirm" placeholder="Confirm" type="password" >
<input class="btn-submit" type="submit" value="Reset">
</form>
<!-- end #reset-password-form -->
{{/if}}
</template>
// Do not forget to add the email package: $ meteor add email
// and to configure the SMTP: https://gist.github.com/LeCoupa/9879221
Template.ForgotPassword.events({
'submit #forgotPasswordForm': function(e, t) {
e.preventDefault();
var forgotPasswordForm = $(e.currentTarget),
email = trimInput(forgotPasswordForm.find('#forgotPasswordEmail').val().toLowerCase());
if (isNotEmpty(email) && isEmail(email)) {
Accounts.forgotPassword({email: email}, function(err) {
if (err) {
if (err.message === 'User not found [403]') {
console.log('This email does not exist.');
} else {
console.log('We are sorry but something went wrong.');
}
} else {
console.log('Email Sent. Check your mailbox.');
}
});
}
return false;
},
});
if (Accounts._resetPasswordToken) {
Session.set('resetPassword', Accounts._resetPasswordToken);
}
Template.ResetPassword.helpers({
resetPassword: function(){
return Session.get('resetPassword');
}
});
Template.ResetPassword.events({
'submit #resetPasswordForm': function(e, t) {
e.preventDefault();
var resetPasswordForm = $(e.currentTarget),
password = resetPasswordForm.find('#resetPasswordPassword').val(),
passwordConfirm = resetPasswordForm.find('#resetPasswordPasswordConfirm').val();
if (isNotEmpty(password) && areValidPasswords(password, passwordConfirm)) {
Accounts.resetPassword(Session.get('resetPassword'), password, function(err) {
if (err) {
console.log('We are sorry but something went wrong.');
} else {
console.log('Your password has been changed. Welcome back!');
Session.set('resetPassword', null);
}
});
}
return false;
}
});
@jesusgtz
Copy link

How can I know what's happening if I always reach the else case of line 17?

@weeger
Copy link

weeger commented Nov 30, 2015

Try to add console.log(err) to see details about the error.

@midhun-toobler
Copy link

Could you add the router file also?

@alaingoldman
Copy link

Yeah the router file is the real question lol

@manusharma1
Copy link

Hi @midhun-toobler,

Below are the routes for iron router. I hope this will help

Router.route('/forgot-password', {
name: 'forgotpassword',
template: 'ForgotPassword'

});

Router.route('/#/reset-password/:token', {
name: 'resetpassword',
template: 'ResetPassword',
});

Thanks and Best Regards,
Manu

@hafizali05
Copy link

I need help with setting up my router for the same code!

@rashmimhatre100
Copy link

When I am clicking the reset link which is in mail it is diverting it to myapp with reset token and within second it is again diverting to '/' that is home. Please help me

@Marcelrama
Copy link

@rashmimhatre100 I'm getting the following error message when testing out the reset password

errorClass
errorType
:
"Match.Error"
message
:
"Match error: Expected string, got undefined"
path
:
""

You managed to reset reset password,I work until I send the mail I receive the url with the token when I make the change of the two passwords I receive the error

@rashmimhatre100
Copy link

Your flowrouter should be like this:
FlowRouter.route('/reset-password/:token', {
name: 'resetpassword',
action: function() {
BlazeLayout.render("resetPassword");
}
});

Set up below @serverside
Meteor.startup(() => {
process.env.MAIL_URL="smtp://YOUR_EMAIL_ADDRESS:YOUR_PASSWORD@smtp.gmail.com:587";

Accounts.urls.resetPassword = function(token) {
return Meteor.absoluteUrl('reset-password/' + token);
}

});

On click event of submit email below event will occure:
Template.ForgotPassword.events({
'click .UMloginbutton': function(e, t) {
e.preventDefault();

// var forgotPasswordForm = $(e.currentTarget);
// console.log(forgotPasswordForm);
var email , trimInput ;

// var emailVar = e.target.email.value;
var emailVar = $("input[name=email]").val();
console.log("emailVar : " + emailVar);

trimInput = function(val) {
  return val.replace(/^\s*|\s*$/g, "");
}

    emailtrim = trimInput(emailVar);
    email = emailtrim.toLowerCase();


  Accounts.forgotPassword({email: email}, function(err) {
    if (err) {
      if (err.message === 'User not found [403]') {
        console.log('This email does not exist.');
        alert('This email does not exist.');
      } else {
        console.log('We are sorry but something went wrong.');
        alert('We are sorry but something went wrong.');
      }
    } else {
      console.log('Email Sent. Check your mailbox.');
      alert('Email Sent. Check your mailbox.');
    }
  });

    
  Bert.alert( "Instructions sent! We've sent an email with instructions on how to reset your password.If you don't receive an email within a few minutes, check your spam and junk folders.", 'success', 'growl-top-right' );
return false;

},
});

After this you will get password reset link in your email address click that link and you will be forwarded to your defined template(resetPassword):

Template.ResetPassword.onCreated(function() {
if (Accounts._resetPasswordToken) {
// var resetPassword = FlowRouter.getParam('token');
Session.set('resetPassword', Accounts._resetPasswordToken);
console.log('ResetPasswordtemplate : ' + resetPassword);
}
});

Template.ResetPassword.helpers({
resetPassword: function(){
// console.log('ResetPassword : ' + resetPassword);
var resetPassword = FlowRouter.getParam('token');
// console.log('ResetPassword : ' + resetPassword);
return resetPassword;
// return Session.get('resetPassword');

},
});

ON submit event of password and confirm password :
Template.ResetPassword.events({
'submit #resetPasswordForm': function(e, t) {
e.preventDefault();
var resetPassword = FlowRouter.getParam('token');
// console.log('ResetPassword : ' + resetPassword);
var resetPasswordForm = $(e.currentTarget),
password = resetPasswordForm.find('#resetPasswordPassword').val(),
passwordConfirm = resetPasswordForm.find('#resetPasswordPasswordConfirm').val();

//Check password is at least 6 chars long
var isValidPassword = function(password, passwordConfirm) {
   if (password === passwordConfirm) {
    console.log('passwordVar.length'+ password.length >= 6 ? true : false);
     return password.length >= 6 ? true : false;
   } else {
     return swal({
        title: 'Passwords dont match',
        text: 'Please try again',
        showConfirmButton: true,
        type: 'error'
     }); //End of error swal
   } //End of else
 }

if (isValidPassword(password, passwordConfirm)) {
// if (isNotEmpty(password) && areValidPasswords(password, passwordConfirm)) {
Accounts.resetPassword(resetPassword, password, function(err) {
if (err) {
console.log('We are sorry but something went wrong.');
} else {
console.log('Your password has been changed. Welcome back!');
Session.set('resetPassword', null);
FlowRouter.go('/');
}
});
}else{
return swal({
title: "password should be at least 6 characters long",
text: "Please try again",
timer: 1700,
showConfirmButton: false,
type: "error"
});

}
// }
return false;

}
});

@GauravPanwar100
Copy link

where is the code for send button of id btn-submit (line 5 of reset-password.html)

@devanghire
Copy link

@55
Copy link

55 commented Dec 12, 2020

@devanghire

You can override URL this way:

Accounts.urls.resetPassword = function (token) {
  return Meteor.absoluteUrl('reset-password/' + token);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment