问题
i have downloaded meanjs version@0.1.12.here i have used two servers for front end i hvae used angular with ionic its running in localhost:3000,for backend i have used meanjs.in that meanjs i have created signup,signin and articles.when ever i am using meansjs as a backend and front end it's working fine .but when i connect to another server(localhost:3000) signup and signin working fine but when ever i am creating articles i am getting 401 unauthorized bcoz of that req.isAuthenticated() function.when ever i create article module req.isAuthenticated() getting fail.req.isAuthenticated() i dono what should i pass for this function i have included my code anyone help me out
now i am passing data like this
$http.post('http://192.168.1.14:3000/articles', credentials).success(function(response,data,errorResponse) {
// If successful we assign the response to the global user model
//$scope.authentication.user =response;
console.log(response);
console.log(data);
// And redirect to the index page
$location.path('/tab/account');
}, function(response,data,errorResponse) {
$scope.error = errorResponse.data.message;
console.log($scope.error);
console.log(data);
});
routes:
app.route('/articles')
.get(users.requiresLogin,articles.list)
.post(users.requiresLogin,articles.create);
login checkup
/**
* Require login routing middleware
*/
exports.requiresLogin = function(req, res, next) {
//console.log(req.isAuthenticated());
console.log(req);
if (!req.isAuthenticated()) {
return res.status(401).send({
message: 'User is not logged in'
});
}
next();
};
/**
* User authorizations routing middleware
*/
exports.hasAuthorization = function(roles) {
var _this = this;
console.log('sss');
return function(req, res, next) {
_this.requiresLogin(req, res, function() {
if (_.intersection(req.user.roles, roles).length) {
return next();
} else {
return res.status(403).send({
message: 'User is not authorized'
});
}
});
};
};
回答1:
I think I had the same problem. Make sure to check your policies folder on the server side.
roles: ['user'],
allows: [{
resources: '/articles',
permissions: ['get', 'post']
}, {
resources: '/articles/:articlesId',
permissions: ['get']
}, {
resources: '/articles',
permissions: ['post']
}]
Add the resource path /articles
and permissions.
来源:https://stackoverflow.com/questions/33842707/mean-js-req-isauthenticated-is-showing-fail