2016-07-25 56 views
0

我正在使用useraccounts:unstyled,accounts-base,accounts-password並試圖實現密碼重置功能。密碼重置令牌返回空值,儘管已定義

我有我的路線定義爲這樣:

FlowRouter.route('/reset-password/:token', { 
    name: 'reset-password', 
    onBeforeAction: function() 
    Accounts._resetPasswordToken = this.params.token; 
    this.next(); 
    }, 
    action(params){ 
    Accounts._resetPasswordToken = params.token; 
    mount(MainLayout, { 
     content: (<ForgotPassword />) 
    }); 
    } 
}); 

我的模板定義爲這樣:

<template name="ForgotPasswordModal"> 
    {{#if $not currentUser}} 
    <div class="forgot-modal {{$.Session.get 'nav-toggle'}}" id="{{checkState}}"> 
     <i class="fa fa-close resetPwd"></i> 
     {{> atForm}} 
    </div> 
    {{/if}} 
</template> 

我的輔助函數定義爲:

if (Meteor.isClient) { 
    Template.ForgotPasswordModal.onCreated(function(){ 
    if(Accounts._resetPasswordToken){ 
     Session.set('resetPasswordToken', Accounts._resetPasswordToken); 
    }else{ 
     console.log("else"); 
    } 
    }); 

    Template.ForgotPasswordModal.helpers({ 
     checkState() { 
      return (AccountsTemplates.getState() == 'resetPwd') ? 'forgot-modal' : ''; 
     } 
    }); 

    Template.ForgotPasswordModal.events({ 
    "submit .at-btn": (event)=>{ 
     event.preventDefault(); 
     console.log(event); 
     password = document.getElementById('reset-password-new-password').value; 
     console.log("password", password); 
     if(password){ 
     Accounts.resetPassword(Session.get('resetPasswordToken'), password, (error)=>{ 
      if(error){ 
      console.log("error: ", error); 
      }else{ 
      console.log("success"); 
      Session.set('resetPasswordToken', null); 
      } 
     }); 
     } 
    } 
    }); 
} 

一旦點擊提交我獲得Error: Match error: Expected string, got null (unknown)

雖然如果我加載路由(使用有效令牌)並運行Session.get('resetPasswordToken')令牌有效地返回。

回答

0

請嘗試使用這種方式: FlowRouter.route('/#/ reset-password /:token'); 這是重置密碼的默認路由。

0

我得到這幾天,無法弄清楚......然後經過一些重新安排,它終於工作。

而且您不必使用Meteor的默認路線&表單重設密碼。

你剝奪妙蛙種子密切@Sleep,你的路線應該是這樣的:

FlowRouter.route('/reset-password/:token', { 
    name: 'reset-password', 
    action(params){ 
    Session.set('_resetPasswordToken', params.token); 
    mount(MainLayout, { 
     content: (<ForgotPassword />) 
    }); 
    } 
}); 

我並不需要onBeforeAction,Accounts._resetPasswordToken,或this.next(),它工作得很好並自動登錄。

你的模板應該是這樣的:

if (!validateForm(password,passwordAgain)) { 
    console.log('Your passwords dont match'); 
} else if (!isValidPassword(password, passwordAgain)) { 
    console.log('You do not have valid passwords'); 
} else { 
    let token = Session.get('_resetPasswordToken'); 
    Accounts.resetPassword(token, password, function(err) { 
    check(token, String); 
    if (err) { 
     console.log('We are sorry but something went wrong.'); 
    } else { 
     console.log('Your password has been changed. Welcome back!'); 
    } 
    }); 
}