钩子(`http`)无法加载! SailsJs

时间:2017-02-07 14:33:04

标签: sails.js passport.js

Iam学习Sails和最后几天,我可以实现Passport Js身份验证,但今天出现了一个错误。这很奇怪,因为我做了同样的步骤,但现在,都失败了。我忘了安装一些依赖吗?

我不知道错误可能在哪里。 这是错误:

Express midleware for passport
error: A hook (`http`) failed to load!
error: ReferenceError: express is not defined
    at Object.module.exports.http.customMiddleware (/home/tatico/estudiomate/config/passport.js:46:12)
    at /home/tatico/.npm-global/lib/node_modules/sails/lib/hooks/http/initialize.js:237:33
    at arrayEach (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/@sailshq/lodash/lib/index.js:1439:13)
    at Function.<anonymous> (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/@sailshq/lodash/lib/index.js:3500:13)
    at _afterLoadingSessionHookIfApplicable (/home/tatico/.npm-global/lib/node_modules/sails/lib/hooks/http/initialize.js:224:11)
    at /home/tatico/.npm-global/lib/node_modules/sails/lib/hooks/http/initialize.js:31:18
    at /home/tatico/.npm-global/lib/node_modules/sails/lib/app/private/after.js:91:14
    at /home/tatico/.npm-global/lib/node_modules/sails/node_modules/async/lib/async.js:721:13
    at /home/tatico/.npm-global/lib/node_modules/sails/node_modules/async/lib/async.js:52:16
    at async.forEachOf.async.eachOf (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/async/lib/async.js:236:30)
    at _parallel (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/async/lib/async.js:712:9)
    at Object.async.parallel (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/async/lib/async.js:726:9)
    at Sails.emitter.after (/home/tatico/.npm-global/lib/node_modules/sails/lib/app/private/after.js:89:11)
    at _waitForSessionHookIfApplicable (/home/tatico/.npm-global/lib/node_modules/sails/lib/hooks/http/initialize.js:30:15)
    at Hook.initialize (/home/tatico/.npm-global/lib/node_modules/sails/lib/hooks/http/initialize.js:42:7)
    at Hook.wrapper [as initialize] (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/@sailshq/lodash/lib/index.js:3250:19)
    at /home/tatico/.npm-global/lib/node_modules/sails/lib/hooks/index.js:88:16
    at /home/tatico/.npm-global/lib/node_modules/sails/node_modules/async/lib/async.js:52:16
    at /home/tatico/.npm-global/lib/node_modules/sails/node_modules/async/lib/async.js:548:17
    at /home/tatico/.npm-global/lib/node_modules/sails/node_modules/async/lib/async.js:542:17
    at _arrayEach (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/async/lib/async.js:85:13)
    at Immediate.taskComplete (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/async/lib/async.js:541:13)

这些是我的文件:

的package.json:

{
  "name": "estudioMate",
  "private": true,
  "version": "0.0.0",
  "description": "a Sails application",
  "keywords": [],
  "dependencies": {
    "bcrypt": "^1.0.2",
    "ejs": "2.3.4",
    "grunt": "1.0.1",
    "grunt-contrib-clean": "1.0.0",
    "grunt-contrib-coffee": "1.0.0",
    "grunt-contrib-concat": "1.0.1",
    "grunt-contrib-copy": "1.0.0",
    "grunt-contrib-cssmin": "1.0.1",
    "grunt-contrib-jst": "1.0.0",
    "grunt-contrib-less": "1.3.0",
    "grunt-contrib-uglify": "1.0.1",
    "grunt-contrib-watch": "1.0.0",
    "grunt-sails-linker": "~0.10.1",
    "grunt-sync": "0.5.2",
    "include-all": "^1.0.0",
    "passport": "^0.3.2",
    "passport-local": "^1.0.0",
    "rc": "1.0.1",
    "sails": "~0.12.11",
    "sails-disk": "~0.10.9"
  },
  "scripts": {
    "debug": "node debug app.js",
    "start": "node app.js"
  },
  "main": "app.js",
  "repository": {
    "type": "git",
    "url": "git://github.com/tatico/estudioMate.git"
  },

  "author": "tatico",
  "license": ""
}

配置/ passport.js:

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


passport.use(new LocalStrategy(
  function(email, password, done) {
    User.findOne({ email: email }, 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 = {
    providers: {
        'github': {},
        'facebook': {},
        'twitter': {}
    },
    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
                // to get it from the view
                res.locals.loggedUser = req.user;
                next();
            });
        }
    }
};

模型/ user.js的:

module.exports = {

    attributes: {
        email: {
            type: 'email',
            unique: true
        },

        admin: {
            type: 'boolean',
            defaultsTo: false
        },

        password: {
            type: 'string',
            required: true
        },

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

        // Check a password with the saved one.
        validPassword: function(password, callback) {
            var obj = this.toObject();
            // If there are a callback, compare async.
            if (callback) {
            //callback (err, res)
            return bcrypt.compare(password, obj.password, callback);
            }
            // Otherwise, compare sync.
            return bcrypt.compareSync(password, obj.password);
        },

        hasPassword: function(){
            return !!this.password;
        }

    },

    // Lifecycle Callbacks
    beforeCreate: function(values, next) {
        hashPassword(values, next);
    },

    beforeValidation: function(values, next) {
        if( values.new_password && values.confirm_password ) {
            var newPasswordMatch = values.new_password === values.confirm_password;
            if( newPasswordMatch ) {
                User.findOne(values.id).done(function(err, user) {
                    if (err) return next(err);
                    if( !values.password || user.validPassword(values.password) ){
                        // If old password is valid.
                        // Ovewrite password with the new password.
                        values.password = values.new_password;
                        // delete new password and confirmation.
                        delete values.confirm_password;
                        delete values.new_password;
                        // Hash the password.
                        hashPassword(values, next);
                    }
                });
            }
        } else if (values.id) {
            // If we are updating the data but the password is not submited.
            User.findOne(values.id).done(function(err, user) {
                if (err) {
                    return next(err);
                } else {
                    // Take the same password user had.
                    values.password = user.password;
                    next();
                }
            });
        } else {
            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();
    });
};

1 个答案:

答案 0 :(得分:0)

在passport.js

的顶部添加var express = require('express');