0
  • in this project i am testing each route after coding it.when i was testing my authantication ,i got my jsonwebtoken(from postman after applying post request).Now when i set my header key as:x-auth-token and its value as jwtwebtoken(that i got from postman),instead of getting same data stored in mongodb data base with an id,it shows me just a boolean value "false".no error showing in console.i am not surewhere i am going wrong.please help*

middleware auth.js file

const jwt=require("jsonwebtoken");
const config=require("config");

module.exports=function(req,res,next){
    //get token from header
    const token=req.header("x-auth-token");

    //check if not token
    if(!token){
       return res.status(401).json({msg:"No token authorization"});

    }
    //verify token
    try{
const decoded=jwt.verify(token,config.get("jwtSecret"));
req.user=decoded.user;
next();
    }catch(err){
res.status(401).json({msg:"Token is not valid"});
    }
} ;

route api auth.js file

const express=require("express");
const router=express.Router();
const auth =require('../../middleware/auth');

const User = require('../../models/User');


//@route  Get api/auth
//does    test route
//@acess Public
router.get("/",auth,async (req,res) =>{
    try{
        const user=await User.findById(req.user.id).selected("-password");
        res.json(user);

    }catch(err){
        console.error(err.message);
        res.status(500).send("server error");

    }
});

module.exports=router;

this is the url on which i am getting "false" on sending get request from postman

1

1 Answer 1

0

No one answered this question. After doing a lot of brainstorming, I finally got the answer. Inside my json file add another field for the password like shown below

password:{
          select:false
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.