【问题标题】:passport.authenticate - username and password are nullpassport.authenticate - 用户名和密码为空
【发布时间】:2015-08-10 10:48:39
【问题描述】:

这可能是一些基本错误,但我正在观看教程,即使我认为在提交登录表单后我所做的一切都完全符合我的要求,但我被重定向到“failureRedirect”页面。当我查看护照模块中的源代码时,我发现了一些东西。

之后:

Strategy.prototype.authenticate = function(req, options) {

  options = options || {};

  var username = lookup(req.body, this._usernameField) || lookup(req.query, this._usernameField);

  var password = lookup(req.body, this._passwordField) || lookup(req.query, this._passwordField);

  //I added:
  console.log("U-> " + username);
  console.log("P-> " + password);

控制台说

U-> null

P-> null

然后在此之后,不会执行rest。

 if (!username || !password) {
     return this.fail({ message: options.badRequestMessage || 'Missing credentials' }, 400);
   }

我不确定我应该在此处发布哪些代码部分。也许这会有所帮助

passport.use(new LocalStrategy(
  function(username, password, done){
    console.log("passport.use new LocalStrategy");  //never gets executed

//永远不会被执行

    User.getUserByUsername(username, function(err, user){
      if (err) throw err;
      if(!user) {
        console.log("Unknown user");
        return done(null, false, {message: "Uknkown User"});
    }
    User.comparePassword(password, user.password, function(err, isMatch){
      if (err) throw err;
      if (isMatch) {
        return done(null, user);
      } else { 
        console.log("invalid Pass");
        return done(null, false, {message: "Invalid Password"});
   }
  });
 });
}));

router.post("/login", passport.authenticate("local", {failureRedirect:"/users/login/err", failureFlash:"invalid username or pass"}), function(req, res){
  console.log("Authenticated OK");
  req.flash("success", "You are logged in");
  res.redirect("/xx");
});

【问题讨论】:

标签: javascript node.js express passport.js passport-local


【解决方案1】:

我不确定您正在执行的确切实施。您可能正在使用原型模式覆盖身份验证功能。 但是,使用 Passportjs 进行身份验证很简单。我最近在我的业余项目中做了一个。请根据我自己实施 Passportjs 的经验通过以下链接 I have a well documented artcile that i wrote on my tech blog. Hope this helps you

// complete code for the exmaple node rest api authentication using passport
var express = require('express');
var passport = require('passport');
var passportHttp = require('passport-http');
var basicStrategy = passportHttp.BasicStrategy; // using the basic     authentication

var app = express();

app.get('/',function(req,res){
res.send("There you go");
});

app.use(passport.initialize()); // initialize and use it in express

passport.use(new passportHttp.BasicStrategy(function(username,password,done)        {
    if(username === password){
    done(null,username); //null means no error and return is the username
}
else{
    return done(null,'there is no entry for you!'); // null means nothing to say,
    //no error. 2nd is the custom statement business rule
    }
}));
// this function hits first when there is an API call. 
function ensureAuthenticated(req,res,next){
if(req.isAuthenticated()){
        next(); 
    // next redirects the user to the next api function waiting to be executed in the express framework
}else{
    res.sendStatus(403); //forbidden || unauthorized
}
};
// this means all the API calls that hit via mydomain.com/api/ uses this  authentication.
//session is false, because its a HTTP API call.
// setting this helps passport to skip the check if its an API call or a session web call
app.use('/api',passport.authenticate('basic',{session:false})); 
// this is served the user once the authentication is a susccess
app.get('/api/data',ensureAuthenticated,function(req,res){
var somevalue = [{name: 'foo'},
    {name: 'bar'},
    {name: 'baz'}];
    res.send(somevalue);
});

app.listen(3250);
console.log('listening to port on ' + 3250);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 2011-10-29
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多