如何解决"error": "ACCESS DENIED" in postman 当我运行我使用 MERN 堆栈的代码时
Picture of error in postman //这是路线
router.get("/all/posts",isSignedIn,isAuthenticated,getAllPosts)
//这是控制器
exports.getAllPosts = (req,res) => {
Post.find()
.populate("postedBy","_id username")
.exec((err,posts) => {
if(err || !posts){
return res.status(400).json({
error:"No Post found"
})
}
res.json(posts)
})
}
//这是 Auth 控制器,它是我收到错误的中间件
exports.isAuthenticated = (req,res,next) => {
let checker = req.profile && req.auth && req.profile._id == req.auth._id;
if (!checker) {
console.log(checker);
return res.status(403).json({
error: "ACCESS DENIED",});
}
console.log(checker);
next();
};
//这是 isSignedIn 控制器
//protected routes
exports.isSignedIn = expressJwt({
secret: process.env.SECRET,userProperty: "auth",});
//这是登录控制器
exports.signin = (req,res) => {
const errors = validationResult(req);
const { username,password } = req.body;
if (!errors.isEmpty()) {
return res.status(422).json({
error: errors.array()[0].msg,});
}
User.findOne({ username },(err,user) => {
if (err || !user) {
return res.status(400).json({
error: "USER does not exists",});
}
if (!user.autheticate(password)) {
return res.status(401).json({
error: "Username and password do not match",});
}
//create token
const token = jwt.sign({ _id: user._id },process.env.SECRET);
//put token in cookie
res.cookie("token",token,{ expire: new Date() + 9999 });
//send response to front end
const { _id,username,role } = user;
return res.json({ token,user: { _id,role } });
});
};
//只有所有/有问题的帖子都可以正常工作,即使 isAuthentication 也在与其他路由一起工作,但在这种情况下它会出错
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。