2

I am getting

"Uncaught (in promise) Error: Request failed with status code 401"

while my other API like GET, PUT, and POST with JWT is working fine. I am wondering where could I go wrong? Many thanks in advance and greatly appreciate any helps. Thanks again.

//JWT
function JWTAuthenticatToken(req, res, next) {

  const token = req.cookies.authcookie
  jwt.verify(token, process.env.JWT_TOKEN_SECRET, (err, userData) => {
    if (err) return res.sendStatus(401).json({ error: "You have to be logged in!" })
    req.user = userData
    next()

  })
}

//client
const deletePost = async (id) => {
  const response = await Axios.delete('http://localhost:5000/api/posts/delpost', { postId: id }, { withCredentials: true })
  const deleteData = dbdata.map(item => {
    if (item._id == response.data._id) {
      return response.data
    }
    else {
      return item
    }

  })
  setDBData(deleteData)

}
  
//express
router.delete('/delpost', JWTAuthenticatToken, async (request, response) => {
  console.log(request.body)
  try {
    const post = await Post.findOne({ _id: request.body.postId }).populate("postedby", "_id")
    if (post.postedby._id === request.user.id) {
      post.remove({ _id: request.body.postId })
      return response.json({ message: "deleted succesfully" })

    }
  } catch (error) {
    return response.json({ message: error })
  }

})
kgangadhar
  • 4,886
  • 5
  • 36
  • 54
Nat
  • 679
  • 1
  • 9
  • 24
  • You should add th token the headers in your Axios.delete() call, where is your token ? – phoenixstudio Nov 29 '20 at 09:34
  • function JWTAuthenticatToken(req, res, next) { const token = req.cookies.authcookie jwt.verify(token,process.env.JWT_TOKEN_SECRET,(err,userData) =>{ if(err) return res.sendStatus(401).json({error:"You have to be logged in!"}) req.user = userData next() }) } – Nat Nov 29 '20 at 09:35
  • the JWT token is working fine for put post and get, but not delete – Nat Nov 29 '20 at 09:37
  • can you plase add that to your qustion ? it is very important part of your issue – phoenixstudio Nov 29 '20 at 09:37
  • Just add it to your delete in the headers of Axios.delete() – phoenixstudio Nov 29 '20 at 09:37
  • the code you pasted is from your axios – phoenixstudio Nov 29 '20 at 09:39
  • sorry, I don't get your question? I have added the JWT code in the question section. – Nat Nov 29 '20 at 09:40
  • you shouldn't send JWT via cookies, chech this answer please : https://stackoverflow.com/questions/39176237/how-do-i-store-jwt-and-send-them-with-every-request-using-react/60875373 – phoenixstudio Nov 29 '20 at 09:43
  • this code i have is working fine setting the withCredential:true. const response = await Axios.put('http://localhost:5000/api/posts/comment',{text:text.index, postId:id}, {withCredentials:true}) – Nat Nov 29 '20 at 09:45
  • cookies aren't secure since browser will send the autmaticaly – phoenixstudio Nov 29 '20 at 09:46
  • I have set the cookie to secure at the nodeJS. const accessToken = jwt.sign({id:user._id}, process.env.JWT_TOKEN_SECRET) res.cookie('authcookie',accessToken, {httpOnly:true}) – Nat Nov 29 '20 at 09:47
  • does not mattr how you set it, your app should b able to controle 100% when the token is sent – phoenixstudio Nov 29 '20 at 09:49

2 Answers2

1

Remove Router.delete. It'll work then.

kgangadhar
  • 4,886
  • 5
  • 36
  • 54
Dingus45191
  • 532
  • 1
  • 7
  • 19
0

in the end, I found out that Axios does not support delete. I switch to post instead and it worked.

Nat
  • 679
  • 1
  • 9
  • 24