6

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'
                });
            }
        });
    };
};
Mariya James
  • 935
  • 2
  • 9
  • 27
  • you are trying to create article after log in? req.isAuthenticated() returning false for you right? are you using mean.js signin function to login? – Mariya James Nov 26 '15 at 04:38
  • i have create after signin only.an i have passed credentials after signin.yeah i am using meanjs functionality. i am new to this technology –  Nov 26 '15 at 04:41
  • If your are using the mean.js functionality then they are using passport-local to authenticate. No idea why you are getting these error? You clear or drop your db and if you haven't changed the html page to create article remove the credentials you declared in the client controller.js. i will update your code – Mariya James Nov 26 '15 at 05:00
  • i am getting error coz of two servers front end running one server and menjs running different server .i don know what passport is expecting to authenticate –  Nov 26 '15 at 05:04
  • Is ```req.isAuthenticated() ``` returning false or null? Did you actually install Passport or Express Session? Or did you just install the mean stack? Can you console.log ```req.isAuthenticated() ``` and tell us what the value is? – Max Baldwin Jan 21 '16 at 18:12
  • i have installed meanjs(meanjs already having passport) .i am using two servers forbackend i am using meanjs ,front end i am using different server .here meanjs already given signup,signin and article.if i use meanjs front end as well as backend its working fine but when i signup and signin using different server its working but when i try to create article i am getting error not auterised –  Jan 21 '16 at 18:22
  • i am getting error resource: the server responded with a status of 401 (Unauthorized) –  Jan 22 '16 at 19:42
  • I think the problem may be what you are sending in the request. What does a `console.log(req.user)` look like in your `exports.requiresLogin` function? Also.. what exactly is credentials in your `$http.post(..)`? – Brian Baker Jan 26 '16 at 05:40
  • @komal you may want to update your question and make it more readable: spelling, and surround code with `. http://stackoverflow.com/help/how-to-ask – Mehdi Jan 28 '16 at 15:45
  • yeah okey @mef but i need solution for this –  Jan 29 '16 at 04:23

1 Answers1

0

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.

Luke Kroon
  • 1,016
  • 12
  • 19