SailsJS + PassportJS(sails-generate-auth) – 无法login用户

我目前在使用PassportJS在我的SailsJS应用程序中validation用户的问题。

根据本教程,我已经使用sails-generate-auth生成了所有需要进行身份validation的内容。

看起来像POST请求按照routes.js文件中的定义正确路由:

'post /auth/local': 'AuthController.callback', 'post /auth/local/:action': 'AuthController.callback', 

在AuthController中我有以下代码:

 callback: function (req, res) { function tryAgain (err) { // Only certain error messages are returned via req.flash('error', someError) // because we shouldn't expose internal authorization errors to the user. // We do return a generic error and the original request body. var flashError = req.flash('error')[0]; if (err && !flashError ) { req.flash('error', 'Error.Passport.Generic'); } else if (flashError) { req.flash('error', flashError); } req.flash('form', req.body); // If an error was thrown, redirect the user to the // login, register or disconnect action initiator view. // These views should take care of rendering the error messages. var action = req.param('action'); switch (action) { case 'register': res.redirect('/register'); break; case 'disconnect': res.redirect('back'); break; default: res.redirect('/login'); } } passport.callback(req, res, function (err, user, challenges, statuses) { if (err || !user) { return tryAgain(challenges); } req.login(user, function (err) { if (err) { return tryAgain(err); } // Mark the session as authenticated to work with default Sails sessionAuth.js policy req.session.authenticated = true // Upon successful login, send the user to the homepage were req.user // will be available. res.redirect('/user/show/' + user.id); }); }); }, 

我在Jade模板中提交我的login表单:

 form(role='form', action='/auth/local', method='post') h2.form-signin-heading Please sign in .form-group label(for='username') Username input.form-control(name='username', id='username', type='username', placeholder='Username', required='', autofocus='') .form-group label(for='password') Password input.form-control(name='password', id='password', type='password', placeholder='Password', required='') .form-group button.btn.btn-lg.btn-primary.btn-block(type='submit') Sign in input(type='hidden', name='_csrf', value='#{_csrf}') 

我已经检查了传递给为password.callback(..)调用指定的callback函数的值:

 passport.callback(req, res, function (err, user, challenges, statuses) { 

用户variables设置为“false”。 我想,这是错误的来源。

有趣的是,当用户注册后调用callback()函数时, 用户variables被设置为包含用户名,电子邮件等所有值的用户对象。

如果你想检查其他的源文件,我的项目可以在这个仓库的 github上find。

任何帮助表示赞赏。

西蒙

您使用identifier作为LocalStrategyusernameField名字段(即默认设置),并在login视图中使用username ,这意味着身份validation框架不接收用户名,并触发Missing Credentials错误。

将login视图中的字段名称更改为identifier或者通过护照configuration文件( config/passport.js )设置相应的usernameField

  local: { strategy: require('passport-local').Strategy, options: { usernameField: 'username' } }