1

我是节点新手,正在尝试使用 passport.js 启动和运行简单的本地登录。在我看来,当调用身份验证函数时,我设置的策略没有运行。

       module.exports = function(app) {
var mongo = require('./mongoose-db.js');
var brands = require('./app/brands.js');

// brand admin page //

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

passport.use(new LocalStrategy( {//this is a strategy but why is it not being run?
    usernameField: 'admin_username',
    passwordField: 'admin_password'
  }, 
  function(username, password, done) {console.log('here 7');
    process.nextTick(function () {
      console.log('here 8');
      // Find the user by username.  If there is no user with the given
      // username, or the password is not correct, set the user to `false` to
      // indicate failure and set a flash message.  Otherwise, return the
      // authenticated `user`.
      mongo.findAdminByUsername(username, function(err, user) { console.log('here 9');
        if (err) { return done(err); }
        if (!user) { return done(null, false, { message: 'Unknown Admin ' + username }); }
        if (user.password != password) { return done(null, false, { message: 'Invalid password' }); }
        return done(null, user);
      })
    });
  }
));


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

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

app.get('/brand_admin', function(req, res){
    console.log('here 1');
  res.render('admin_login', { user: req.user, message: req.session.messages });
});


app.post('/brand_admin', function(req, res, next) {
    console.log('USER: ');
    console.log(req.user);
    res.send('ok');
    passport.authenticate('local', function(err, user, info) {
        console.log(user);
    if (err) { return next(err) }
    if (!user) {console.log(req.session.messages);
      req.session.messages =  [info.message];
    console.log('here 4');
    console.log(req.session.messages);
      return res.redirect('/brand_admin')
    }
    console.log('here 5');
    req.logIn(user, function(err) {
      if (err) { return next(err); console.log('here 6');}
      return res.redirect('/');
    });
  })(req, res, next);
});

ejs是:

<body>

<head> Admin Login </head>

<form id="brand_edit" action="/brand_admin" method="post" >
        <fieldset>

        Login: <input type="text" name="admin_login" > <br>

        Password: <input type="password" name="admin_password" >

        <input type="submit" name="action" value="Login"/>

        </fieldset>

<a href="">Forgotten your password?</a>

</form>

</body>

我从表单提交中得到的输出是:

Steve
agrgegerfe
{ id: 1,
  username: 'Steve',
  password: 'agrgegerfe',
  email: 'bob@example.com' }
false
undefined
here 4
[ 'Missing credentials' ]
here 1

该策略没有运行,但我不知道为什么?任何人都可以帮忙吗?可能会影响这一点的一件事是我在应用程序的另一部分使用护照登录 Facebook。

4

1 回答 1

4

我认为您没有正确调用护照。它需要在路由链中。尝试这个:

// 使用正确的字段配置护照
护照.使用(新的LocalStrategy({
  用户名字段:'admin_login',
  密码字段:'admin_password'
},功能(用户名,密码,完成){
  ...
});


app.post('/brand_admin', passport.authenticate('local'), function (req, res) {
 // req.user 现在已定义
 控制台.log(req.user);
 res.send('ok');
});
于 2013-07-19T15:38:12.700 回答