Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 帆船+;PassportJS(sails生成身份验证)-无法登录用户_Javascript_Node.js_Authentication_Sails.js - Fatal编程技术网

Javascript 帆船+;PassportJS(sails生成身份验证)-无法登录用户

Javascript 帆船+;PassportJS(sails生成身份验证)-无法登录用户,javascript,node.js,authentication,sails.js,Javascript,Node.js,Authentication,Sails.js,我目前在使用PassportJS验证我的SailsJS应用程序中的用户时遇到问题 我已经根据生成了使用sails生成auth进行身份验证所需的所有内容 POST请求似乎已按照routes.js文件中的定义正确路由: 'post /auth/local': 'AuthController.callback', 'post /auth/local/:action': 'AuthController.callback', 在AuthController中,我有以下代码: callback: func

我目前在使用PassportJS验证我的SailsJS应用程序中的用户时遇到问题

我已经根据生成了使用sails生成auth进行身份验证所需的所有内容

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模板中的登录表单:

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}')
我已检查传递给为密码指定的回调函数的值。回调(..)调用:

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

有趣的是,当用户注册后调用callback()函数时,用户变量被设置为user object,其中包含username、email等所有值

如果您想检查其他源文件,我的项目可以在中的github上找到

感谢您的帮助


Shimon

您使用
标识符
作为
本地策略
用户名字段
(即默认设置),并且在登录视图中具有
用户名
,这意味着身份验证框架没有收到用户名,并引发
缺少凭据的错误

将登录视图中的字段名更改为
identifier
,或通过passport配置文件(
config/passport.js
)设置相应的
usernameField
):


您应该从passport配置文件中删除您的Google OAuth凭据。始终将敏感配置信息放在
local.js
中。感谢您的提示@galactocalypse。我会修好的。非常感谢!我不知道这个选项。我已经在
config/passport.js
中更改了配置,登录正常工作
passport.callback(req, res, function (err, user, challenges, statuses) {
  local: {
    strategy: require('passport-local').Strategy,
    options: {
      usernameField: 'username'
    }
  }