簡體   English   中英

使用 MERN 堆棧的密碼重置身份驗證

[英]Password Reset Auth using MERN Stack

我在 nodemailer email 中實現了一個授權令牌,每當用戶重置密碼時就會發送該令牌。 我得到了生成的 url 以及附加的令牌設置和正確發送,我似乎無法讓我的路由正確處理所述令牌的身份驗證。

我的請求返回 404,表示未找到我的 GET (GET http://localhost:3000/reset/XXXXXXXXXXXX 404 (Not Found) )。 誰能幫我指出正確的方向?

這是我的服務器 api 路由

router.get('/reset', (req, res, next) => {
   User.findOne({ resetPasswordToken: req.query.resetPasswordToken }).then(user => {
      if (user == null) {
         res.json('Password link is invalid or has expired');
      } else {
         res.status(200).send({
           username: user.email,
           message: 'Password link accepted',
         })
     }
   })
});

還有我處理請求的代碼

componentDidMount() {

  console.log(this.props.match.params.token);

  axios.get('/reset' + this.props.match.params.token)
    .then(response => {
      // Conditional logic here based on response returned
    })
    .catch(function (error) {
      console.log(error);
    })
}

您需要像這樣告訴 express 期望 url 中的參數:

router.get('/reset/:token', (req, res, next) => {
   // token is inside req.params.token
   User.findOne({ resetPasswordToken: req.params.token }).then(user => {
      if (user == null) {
         res.json('Password link is invalid or has expired');
      } else {
         res.status(200).send({
           username: user.email,
           message: 'Password link accepted',
         })
     }
   })
});

然后確保在發出請求時 url 中有一個斜線:

  axios.get('/reset/' + this.props.match.params.token)
    .then(response => {
      // Conditional logic here based on response returned
    })
    .catch(function (error) {
      console.log(error);
    })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM