在meteor中为自定义路由设置重置密码令牌

时间:2015-09-21 03:31:14

标签: javascript meteor iron-router

我正在使用meteor中的帐户密码构建自定义身份验证系统。我的问题是为什么当我删除meteor为重置密码链接提供的默认哈希时,我也会丢失我的resetPasswors令牌会话?到目前为止,我有这个代码,但我无法访问我的模板中的条件来更改我的密码。设置自定义重置密码路由时似乎有问题。

router.js

Router.route('/reset-password', {name: 'resetPassword'});

Router.route('/reset-password/:token', function () {
  this.render('resetPassword');
});

reset_password.html

<template name="resetPassword">
  <div class="reset-container">
    {{#if resetPassword}}   
      <form class="reset-password">
        <div class="form-group">
          <label for="password" class="control-label">New password</label>
          <input type="password" name="password" class="form-control"
            title="Please enter a new password" id="password" placeholder="Password">
          <span class="help-block small"> Your strong password</span> 
        </div>
        <input type="submit" class="btn btn-success btn-block" value="Reset password">   
      </form>               
    {{else}}
      <form class="forgot-password">
        <div class="form-group">
          <label for="email" class="control-label">Email</label>
          <input type="text" name="email" class="form-control"
            title="Please enter your email" id="email" placeholder="example@gmail.com">
          <span class="help-block small"> Your unique email address</span>   
        </div>
        <input type="submit" class="btn btn-success btn-block" value="Send instructions!">   
      </form>           
    {{/if}}
  </div>
</template>

reset_password.js

if (Accounts._resetPasswordToken) {  
  Session.set('resetPasswordToken', Accounts._resetPasswordToken);
}

Template.resetPassword.helpers({  
  resetPassword: function() {
    return Session.get('resetPasswordToken');
  }
});

Template.resetPassword.events({  
  "submit .forgot-password": function(event) {
    // Prevent default browser form submit
    event.preventDefault();

    // Get value from form element
    email = event.target.email.value;

    if (email) {
      Accounts.forgotPassword({email: email}, function (error) {
        if (error) {
          if (error.message === 'User not found [403]') {
            throwAlert('This email address does not exist.', 'danger');
          } else {
          throwAlert('We are sorry but something went wrong.', 'danger');
          }
        } else {
          throwAlert('We have sent you an email with basic instructions to reset your password.', 'success');
        }
      });
    } else {
      throwAlert('Your email address cannot be empty.', 'danger');
    }
  },
  "submit .reset-password": function (event) {
    // Prevent default browser form submit
    event.preventDefault();

    // Get value from form element
    password = event.target.password.value;

    // If the password is valid, we can reset it.
    if (password) {
      Accounts.resetPassword(Session.get('resetPasswordToken'), password, function (error) {
        if (error) {
          throwAlert('We are sorry but something went wrong.', 'danger');
        } else {
          throwAlert('Your password has been changed. Welcome back!', 'success');
          Session.set('resetPasswordToken', null);
          Router.go('postsList');
        }
      });
    } else {
      throwAlert('Your password cannot be empty. Create a good one!', 'danger');
    }
  }
});

服务器/ config.js

Meteor.startup(function() {
  Accounts.emailTemplates.resetPassword.text = function(user, url) {
    var token = url.substring(url.lastIndexOf('/') + 1, url.length);
    var newUrl = Meteor.absoluteUrl('reset-password/' + token);
    var str = 'Hello, \n';
        str+= 'Click on the following link to reset your password \n';
        str+= newUrl;
    return str;
  };
});

1 个答案:

答案 0 :(得分:3)

同样,我这样做:

  this.route('password.reset', {
     path: '/password/reset/:token',
     onBeforeAction: function() {
        Accounts._resetPasswordToken = this.params.token;
        this.next();
     },
     template: 'resetPassword'
  });

并在onCreated中移动if(Accounts._resetPasswordToken)

Template.resetPassword.onCreated(function() {
   if(Accounts._resetPasswordToken) {
    Session.set(RESET_PASSWORD, Accounts._resetPasswordToken);
   }
  ...