Francisco Possetto
Francisco Possetto

Reputation: 443

I have an error with passport.js implementation in Sails.js

I have a problem with Sails Passport implementation. This error appears in my terminal:

/home/tatico/sinGualichoPassport/config/passport.js:19


if (!user.validPassword(password)) {
            ^

    TypeError: user.validPassword is not a function
        at /home/tatico/sinGualichoPassport/config/passport.js:19:17

    at returnResults (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/basic.js:180:9)
    at /home/tatico/.npm-global/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/basic.js:86:16
    at /home/tatico/.npm-global/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/operations.js:83:7
    at /home/tatico/.npm-global/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:52:16
    at Object.async.forEachOf.async.eachOf (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:236:30)
    at Object.async.forEach.async.each (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:209:22)
    at /home/tatico/.npm-global/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/operations.js:436:11
    at /home/tatico/.npm-global/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/operations.js:574:5
    at /home/tatico/.npm-global/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:52:16
    at Object.async.forEachOf.async.eachOf (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:236:30)
    at Object.async.forEach.async.each (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:209:22)
    at _buildChildOpts (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/operations.js:464:9)
    at _execChildOpts (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/operations.js:432:8)
    at /home/tatico/.npm-global/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/operations.js:81:10
    at wrapper (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/waterline/node_modules/lodash/index.js:3592:19)
    at applyInOriginalCtx (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:421:80)
    at wrappedCallback (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:324:18)
    at callback.success (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/normalize.js:33:31)
    at _switch (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/factory.js:58:28)
    at /home/tatico/.npm-global/lib/node_modules/sails/node_modules/waterline/lib/waterline/adapter/dql.js:166:7
    at wrapper (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/waterline/node_modules/lodash/index.js:3592:19)

This is my Files:

config/passport.js:

var passport      = require('passport'),
    LocalStrategy = require('passport-local').Strategy
    bcrypt        = require('bcrypt');

passport.use(new LocalStrategy(
  function(username, password, done) {
    User.findOne({ username: username }, function(err, user) {
      if (err) { return done(err); }
      if (!user) {
        return done(null, false, { message: 'Incorrect username.' });
      }
      if (!user.validPassword(password)) {
        return done(null, false, { message: 'Incorrect password.' });
      }
      return done(null, user);
    });
  }
));

passport.serializeUser(function(user, done) {
  done(null, user.id);
});

passport.deserializeUser(function(id, done) {
  User.findById(id, function(err, user) {
    done(err, user);
  });
});

module.exports = {
    http: {
        customMiddleware: function(app){
            console.log('Express midleware for passport');
            app.use(express.static('public'));
            app.use(express.cookieParser());
            app.use(express.bodyParser());
            app.use(express.session({ secret: 'keyboard cat' }));
            app.use(passport.initialize());
            app.use(passport.session());
            app.use(app.router);
            app.use(function(req,res,next){
                // Set the loggedUser in locals *does it work?
                // to get it from the view
                res.locals.loggedUser = req.user;
                next();
            });
        }
    }
};

UserController.js

module.exports = {

    auth: function(req, res) {
        return res.view();
    },



    create: function(req, res, next) {
        User.create( req.params.all(), function createdUser(err, user){
            if (err) {
                return res.negotiate(err);
            }
            req.session.authenticated = true;
            req.session.user = user;
            return res.json(user);
        });
    },

    login: function(req, res, next) {
        // Use Passport LocalStrategy
        require('passport').authenticate('local', function(err, user, info){
            if ((err) || (!user)) next(err);
            req.login(user, function(err){
                if (err) return res.redirect('/user/auth');
                // Redirect to the user page.
                return res.redirect('/user/' + user.id);
            });
        })(req, res);
    },

    logout: function(req, res){
        // Call Passport method to destroy the session.
        req.logout();
        // Redirect to home page.
        return res.redirect('/');
    }
};

User.js:

module.exports = {

    attributes: {
        email: {
            type: 'string',
            required: true,
            email: true,
            unique: true
        },
        password: {
            type: 'string',
            required: true
        },

        username: {
            type: 'string',
            required: true,
            unique: true
        },

        toJSON: function() {
            var obj = this.toObject();
            delete obj.password;
            return obj;
        }
    },

    // Lifecycle Callbacks
    beforeCreate: function(values, next) {
        hashPassword(values, next);
    },
    beforeUpdate: function(values, next) {
        if(values.password) hashPassword(values, next);
        else next();
    }
}

var bcrypt = require('bcrypt');

function hashPassword(values, next) {
    bcrypt.hash(values.password, 10, function(err, hash) {
        if (err) return next(err);

        values.password = hash;
        next();
    });
}

Im new in Sails, and I suppose this is a not good implementantion, Anyone can Help me and give a little feed back to improve my understanding of Sails and Passport? Thanks.

Upvotes: 0

Views: 123

Answers (1)

Sapna Jindal
Sapna Jindal

Reputation: 422

ValidPassword is not defined anywhere which is leading to this error.

Upvotes: 1

Related Questions