In my react project, I have been using axios to help get data from MongoDB.
My axios get requests return a 401 error when I add my auth middleware as a param. This middleware requires the user to have a valid JWT token before any data is returned.
When making the request with Insomnia/Postman, as long as long as the user token is added to the request headers, I always get the desired data returned. However, when I try to get the data within the react application, I get a "Failed to load resource: the server responded with a status of 401 (Unauthorized)" error log, along with my custom json error response, "No Auth token, authorisation denied!"
I built a simple boolean function that returns true if the current user has a valid JWT token. Despite returning true, I am still receiving the 401 error as a response, which leads me to suspect there is a syntax or formatting error somewhere inside my react code.
Backend (with express):
router.get("/allauth", auth, async (req, res) => {
const players = await Player.find();
res.json(players);
});
Auth:
const jwt = require("jsonwebtoken");
const auth = (req, res, next) => {
try {
const token = req.header("x-auth-token");
if (!token)
return res.status(401)
.json({ msg: "No Auth token, authorisation denied!" });
//match token against .env password
const verified = jwt.verify(token, process.env.JWT_SECRET);
if (!verified)
return res.status(401)
.json({ msg: "Token verification failed, authorisation denied!" });
req.user = verified.id;
next();
} catch (err) {
res.status(500).json({ error: err.message });
}
};
React frontend:
export default function DisplayTeam() {
const [players, setPlayers] = useState([]);
const {userData} = useContext(UserContext);
useEffect(() => {
let token = localStorage.getItem("auth-token");
const url = "http://localhost:5000/players";
console.log(token);
axios.get(`${url}/allauth`, {
headers: {
"Authorization": `x-auth-token ${token}`
}
})
.then( response => setPlayers(response.data))
.catch( error => console.log(error));
}, []);
return (//display from DB);
provigil 200 mg – modafinil side effects order modafinil
cheap accutane online – buy accutane cream accutane 20mg
buy amoxicilina 500 mg online – amoxil USA generic amoxicillin cost
online vardenafil – cheap levitra vardenafil vardenafil
stromectol pharmacy – ivermectin for humans for sale stromectol australia
tadalafil tablets 20 mg buy – best price generic tadalafil silkroad online pharmacy
ivermectin 0.5% lotion – ivermectin us stromectol tab price
accutane uk cost – isotretinoin 10mg isotretinoin medication
lyrica prescription coupon – canadian vet pharmacy buying drugs from canada
buying amoxicillin over the counter – over the counter amoxicillin kroger amoxicillin without a doctor
price for viagra – best price for sildenafil generic viagra 100mg pills
buy tadalafil us – tadalafil india 10 mg best generic tadalafil
ivermectin 6mg – ivermectin 1% cream generic ivermectin buy canada
prednisone 30 mg coupon – 54899 prednisone cost of prednisone 10mg tablets
modafinil 100 mg – provigil a stimulant modafinil and alcohol
zithromax z-pak – can you buy zithromax zitromax
lasix tablets uk – buy furosemide 20 mg online canadian pharmacy lasix
clomid online 25mg – clomid for sale clomid online
price of stromectol – ivermecti.com ivermectin lotion 0.5
natural ed pills – ed pills gnc ed medication online
deltasone otc – buy prednisone cheap prednisone 50 mg
cheapest generic accutane – accutaneple where can i get accutane in australia
generic viagra 100mg – cialis 10mg over the counter order cialis 5mg generic
sildenafil 200mg en ligne – vrai cialis 10mg prix cialis 20mg sans ordonnance
ivermectin brand – order stromectol 3mg online ivermectin 12 mg for humans for sale
prednisone usa – safest canadian online pharmacy prednisone 5mg uk
cialis without a prescription tadalafil liquid
pregabalin 150mg cheap – azithromycin 250mg price zithromax 500mg usa
cheapest tadalafil cost tadalafil online with out prescription
order orlistat online cheap – hydroxychloroquine 400mg pills hydroxychloroquine 200mg usa
order hydroxyzine 10mg sale – hydroxyzine 25mg without prescription order crestor 20mg pill
tetracycline 250mg for sale – order cyclobenzaprine sale celexa pill
https://cialismat.com/ generic tadalafil from uk
purchase chloroquine online – buy baricitinib for sale sildenafil canada
https://cialisvet.com/ cost of cialis
cheap cialis pills for sale https://cialisbusd.com/
tadalafil online tadalafil blood pressure
581982 916308Some truly quality posts on this web site , saved to favorites . 215085
where to buy generic cialis online safely https://nextadalafil.com/
https://cialisvet.com/ cialis tadalafil
https://cialisicp.com/ generic tadalafil from uk
http://ivermectinuni.com/ ivermectin pills stromectol
generic ivermectin 6mg buy 12 mg ivermectin
https://stromectolusdt.com/ where to buy ivermectina 12mg
https://www.ivermectinusd.com/ ivermectin stromectol tablets