Answer the question
In order to leave comments, you need to log in
Why is a 403 code displayed on page reload?
I have a middleware that checks if the administrator is logged in and if he exists:
import jwtDecode from 'jwt-decode';
const Admin = require("../models/Admin");
module.exports = async (req, res, next) => {
try {
const auth = req.headers.authorization;
if (auth) {
const token = auth.split(' ')[1];
if (token) {
const { _doc: { _id } } = jwtDecode(token);
const candidate = await Admin.findById(_id);
req.isAdmin = Boolean(candidate);
} else {
req.isAdmin = false;
}
} else {
req.isAdmin = false;
}
next();
} catch (err) {
console.log(err);
return res.status(500).json({ ok: false, message: "Произошла ошибка сервера" });
}
}
{
ok: false,
message: "У вас нет доступа"
}
router.get("/orders", isAdmin, async (req, res) => {
try {
if (req.isAdmin) {
const orders = await Order.find();
return res.status(200).json({ ok: true, orders });
} else {
return res.status(403).json({ ok: false, message: "У вас нет доступа" });
}
} catch (err) {
console.log(err);
return res.status(500).json({ ok: false, message: "Произошла ошибка сервера" });
}
});
Answer the question
In order to leave comments, you need to log in
Show the code that is responsible for successful authorization. It looks like your sessions are not being established.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question