Last active
July 30, 2022 19:34
-
-
Save LeCoupa/9879066 to your computer and use it in GitHub Desktop.
Reset a password with Meteor --> https://github.com/LeCoupa/awesome-cheatsheets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} | |
}); |
where is the code for send button of id btn-submit (line 5 of reset-password.html)
link is http://localhost:3000/#/reset-password/3nhpVCHKhkcG_ZyPflg2CasaFXvidU05l480C6af3pX
how can i remove # from link
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
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:[email protected]: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();
},
});
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();
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"
});
}
});