我正在将项目移至Vue.js,但我无法获得任何中间件来检查用户是否已登录或检查用户对工作的所有权。经过无休止的搜索后,我相信问题是我从客户端发送到服务器的标头不包含护照序列化的用户或其他内容?我该如何进行这项工作?
这是我在后端的登录路线:
router.post("/login", function (req, res, next) {
if (!req.body.username || !req.body.password) {
res.send("Error");
} else if(req.body.username.length > 40 || req.body.password.length > 40){
res.send("Error");
} else if (req.body.username) {
req.body.username = req.body.username.toLowerCase();
next();
}
}, passport.authenticate('local', {
failureRedirect: '/login'
}), function(req, res){
User.findById(req.user.id, function(err, user){
if(err){
res.send("User not found");
} else {
res.send(user.toJSON());
}
})
});
Run Code Online (Sandbox Code Playgroud)
这是我在客户端的登录页面:
async login () {
const response = await AuthenticationService.login({
username: this.username,
password: this.password,
})
if(response.data == "Error"){
this.$router.push({
name: 'login'
})
} else …Run Code Online (Sandbox Code Playgroud)