jlonganecker
jlonganecker

Reputation: 1198

Policy causing error: ignored invalid attempt to bind route to a non-function controller

Using SailsJS 0.10.5 on my Mac (installed using npm -g) and I am trying to apply a policy to a controllers action and I am getting this error:

error: In user.isloggedin, ignored invalid attempt to bind route to a non-function controller: { identity: 'isAuthenticated',
globalId: 'isauthenticated',
_middlewareType: 'POLICY: isAuthenticated' } for path:  /isLoggedIn

I have a controller with an action, a route in config/route.js, a policy in config/policies.js

Additional Details
If I don't use isAuthenticated in the config/policies.js file I do not get this error.
I am using passport with passport-local

Any ideas what I am doing wrong?

config/policies.js

module.exports.policies = {

    '*': true,
    UserController: {
        'signup': true,
        'isLoggedIn': 'isAuthenticated'
    },
    AuthController: {
        '*': true
    }
};


config/routes.js

module.exports.routes = {

    'post /signup' : {
        controller: 'UserController',
        action: 'signup',
    },
    '/isLoggedIn' : {
        controller : 'UserController',
        action : 'isLoggedIn'
    }
};


api/controller/UserController.js

var passport = require('passport');

module.exports = {
    signup: function(req, res) {
        User.create(req.params.all()).exec(function(err, user) {
            if(err) {
                return res.negotiate(err);
            }
            req.logIn(user, function(err) {
                if(err) {
                    return res.negotiate(err);
                }
                return res.redirect('/');
            });
        });
    },
    isLoggedIn: function(req, res) {
        User.findOne({id: req.session.passport.user}).exec(function(err, user) {
            if(err) {
                return;
            }
            res.json(user);
        });
    }
};


api/policies/isAuthenticated.js

module.export = function isAuthenticated(req, res, next) {
    if(!req.isAuthenticated()) {
        console.log('403!!!');
        return res.send(403, {message: 'Not Authorized' });
    }
    console.log('asdf');
    return next();
};

Upvotes: 2

Views: 2130

Answers (1)

Disruptive Art
Disruptive Art

Reputation: 308

Try changing module.export to module.exports <- with an s in your file isAuthenticated.js

Upvotes: 4

Related Questions