我写了一个重置密码功能:
用户集合(services>password>reset
)设置良好
邮件发送良好
邮件中的链接将我路由到右侧URL(右侧令牌)
在此页面上,会话resetPasswordToken
设置为但有
undefined
值:我无法在此处自动编写令牌 - 如果我在这里手动编写令牌,一切正常,密码重置得很好,会话resetPasswordToken
就是在设置良好之后到null
。
那么如何在会话中编写令牌?感谢。
在我的代码下面。
**
获取新密码的链接客户端
<p class="info"><a href="#" class="send-reset-password-link">Resend verification link</a></p>
点击(客户端)上的事件
Template.UserProfile.events({
//resend verification link function
'click .send-reset-password-link' ( event, template ) {
Meteor.call( 'sendResetPasswordLink', ( error, response ) => {
if ( error ) {
Bert.alert({
title: 'Error',
message: error.reason,
type: 'danger'
});
} else {
let email = Meteor.user().emails[ 0 ].address;
Bert.alert({
title: 'Reset Password Link sended',
message: 'Please check your mails.',
type: 'info'
});
}
});
}
});
发送链接(服务器)的方法
Meteor.methods({
sendResetPasswordLink() {
let userId = Meteor.userId();
if ( userId ) {
return Accounts.sendResetPasswordEmail( userId );
}
}
});
重置密码电子邮件模板(服务器)
//resetPassword Template
Accounts.emailTemplates.siteName = "Me";
Accounts.emailTemplates.from = "Me <my@mail.com>";
Accounts.emailTemplates.resetPassword.subject = function (user) {
return "Reset Your Password";
};
// html template
Accounts.emailTemplates.resetPassword.html = function (user, url) {
SSR.compileTemplate( 'htmlEmail', Assets.getText( 'emailverification.html' ) );
let emailAddress = user.emails[0].address,
userAvatar = user.profile.avatar,
urlWithoutHash = url.replace( '#/', '' );
var emailData = {
urlWithoutHash: `${urlWithoutHash}`,
userAvatar: `${userAvatar}`,
};
return SSR.render( 'htmlEmail', emailData );
};
路线
FlowRouter.route( '/reset-password/:token', {
name: 'reset-password',
action( params ) {
BlazeLayout.render('ResetPassword', {content: 'body'});
Accounts.resetPassword( params.token, ( error ) =>{
if ( error ) {
Bert.alert({
title: 'Error',
message: error.reason,
type: 'danger'
});
} else {
FlowRouter.go( '/' );
}
});
}
});
最后,重置密码模板
//resetpassword.html
<template name="ResetPassword">
<form action="/reset-password" class="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" > -->
<button class="btn-submit" type="submit" value="Reset">Reset</button>
</form>
<!-- end #reset-password-form -->
</template>
//resetpassword.js
if (Accounts.resetPassword) {
Session.set('resetPasswordToken', Accounts.resetPassword);
}
Template.ResetPassword.helpers({
resetPassword: function(){
return Session.get('resetPasswordToken');
}
});
Template.ResetPassword.events({
"submit .reset-password": (event) => {
// Prevent default browser form submit
event.preventDefault();
//let token;
// Get value from form element
const target = event.target;
const password = event.target.password.value;
// If the password is valid, we can reset it.
if (password) {
//Accounts.resetPassword(token, password, (error) => {
Accounts.resetPassword(Session.get('resetPasswordToken'), password, (error) => {
if (error) {
Bert.alert({
title: 'Error',
message: error.reason,
type: 'danger'
});
} else {
Bert.alert({
title: 'Success',
message: 'Account successfully created.',
type: 'success'
});
Session.set('resetPasswordToken', null);
//Router.go('postsList');
}
});
} else {
Bert.alert({
title: 'Error',
message: 'The password cannot be empty.',
type: 'danger'
});
}
}