Passport因模块化Express应用程序失败

时间:2015-04-14 08:27:17

标签: javascript node.js authentication express passport.js

因此,我尝试使用模块化目录结构设置半RESTful Express应用程序。

我开始观看TJ关于Vimeo(https://vimeo.com/56166857)的视频,了解如何设置模块化快递应用。我非常喜欢他这样做的方式,并希望扩展它的应用程序。

问题是,我认为Passport.js会因为他正在进行的模块化设置而弄乱它如何处理身份验证。因为如果我使用另一个不太模块化的目录结构,Express应用程序可以正常工作。

如果我按照他的步骤,并尝试集成Passport.js,视图渲染正常,但如果我尝试注册一个新用户,当POST请求进入/users/register我做的用户控制器,我得到一个非常奇怪的错误:

crypto.js:599
    return binding.PBKDF2(password, salt, iterations, keylen, digest, callback
                   ^
TypeError: Not a buffer
    at TypeError (native)
    at pbkdf2 (crypto.js:599:20)
    at Object.exports.pbkdf2 (crypto.js:585:10)
    at Object.ondone (/home/karysto/Desktop/modular-multiblog/node_modules/passport-local-mongoose/lib/passport-local-mongoose.js:71:20)

我真的很困惑为什么会这样,但我有充分的理由相信它是造成它的模块化设置。

这是我的users controller.js文件:

const
    User = require('./model'); // including the user model

exports.index = function(req, res) {
  User.find(function(err, users) {
    if (err) throw err;
    return res.status(200).json(users);
  });
};

exports.show = function(req, res) {
  User.find(req.param.id, function(err, user) {
    if (err) throw err;
    return res.status(200).json(user);
  });
};

exports.create = function(req, res) {
  User.register( new User({ username: req.body.username, password: req.body.password}), function(err, user) {
    if (err) {
      return res.render('users/register', { info: "Sorry. That username already exists. Try again." });
    };

    // successful account creation, redirect to profile route.
    passport.authenticate('local')(function (req, res) {
      return res.render('users/show', {
        user: req.user
      });
    });
  });
};

exports.new = function(req, res) {
  return res.render('users/register');
};

exports.login = function(req, res) {
  if (req.user) {
    res.render('users/show', {
      user: req.user
    });
  }
  else {
    res.render('users/login');
  }
};

exports.logout = function(req, res) {
  req.logout();
  return res.render('static_pages/index');
};

这是我的users route.js文件:

const
    controller = require('./controller')
  , express    = require('express')
  , router     = express.Router()
  , passport = require('passport');

router.post('/register', controller.create);
router.get('/register', controller.new);

router.post('/login', passport.authenticate('local'), controller.login);
router.get('/login', controller.login);
router.get('/logout', controller.logout);

router.get('/:id', controller.show);
router.get('/', controller.index);

module.exports = router;

users模型也没什么特别之处:

const
   mongoose = require('mongoose')
 , passportLocalMongoose = require('passport-local-mongoose');

const User = new mongoose.Schema({
    username: String,
    password: String
});

// Adds the User model to the mongodb schema and passport model.
// Allows the methods .register to be added for this schema.
User.plugin(passportLocalMongoose);

module.exports = mongoose.model('User', User);

如果它也有帮助,我的app目录结构如下:

api/ app.js bin/ makefile package.json public/ README routes/ test/ views/

应用的其余部分,每个目录的文件,我都省略了公共目录,因为目前没有使用它:

./api: post/ static_pages/ user/

./api/post: controller.js model.js routes.js

./api/static_pages: controller.js routes.js

./api/user: controller.js model.js routes.js

./bin: www

./routes: index.js users.js

./test: test.user.js

./views: error.jade index.jade layout.jade posts/ static_pages/ users/

./views/static_pages: about.jade contact.jade index.jade

./views/users: login.jade register.jade show.jade

0 个答案:

没有答案