0

我正在尝试以这种方式使用本地护照设置登录页面,但它不起作用:

节点服务器端:

// mongoose
var User = mongoose.model('User', userSchema);
User.find({}).exec(function(err, collection){
    if(collection.length === 0) {
        User.create({identifiant: '123', motDePasse: '123'});
    }
});

// passport-local
passport.use(new LocalStrategy(
    function(identifiant, motDePasse, done) {
        console.log(identifiant); // It's not logging
        User.findOne({identifiant:identifiant}).exec(function(err, user) {
            if(user) {
                return done(null, user);
            } else {
                return done(null, false);
            }
        })
    }
));
passport.serializeUser(function(user, done) {
    if(user) {
        done(null, user._id);
    }
});
passport.deserializeUser(function(id, done) {
    User.findOne({_id:id}).exec(function(err, user) {
        if(user) {
            return done(null, user);
        } else {
            return done(null, false);
        }
    })
});

// route
app.post('/connexion', function(req, res, next){
    var auth = passport.authenticate('local', function(err, user) {
        if(err) {return next(err);}
        if(!user) {res.send({success: false});}
        req.logIn(user, function(err) {
            if(err) {return next(err);}
            res.send({success: true, user: user});
        })
    });
    auth(req, res, next);
});

角度客户端:

app.controller('uAsideLoginCtrl', function($scope, $http, uIdentity, uNotifier){
    $scope.identity = uIdentity;
    $scope.signin = function(identifiant, motDePasse){
        $http.post('/connexion', {identifiant: identifiant, motDePasse: motDePasse}).then(function(res){
            if(res.data.success) {
                uIdentity.currentUser = res.data.user;
                uNotifier.success("Vous êtes maintenant connecté!");
            } else {
                uNotifier.error("L'identifiant ou le mot-de-passe est incorrecte.");
            }
        });
    };
});

这是 mongodb 的用户行:

{“_id”:ObjectId(“53df7b3b769827786b32dafe”),“identifiant”:“123”,“motDePasse”:“123”,“__v”:0}

我认为它来自LocalStrategy。我没有得到 console.log 的结果。

请问有什么绝妙的主意吗?

请问怎么了?

4

1 回答 1

0

问题不在您显示的代码中。我的猜测是,您的请求中使用的参数(我假设它是带有方法/connexion的常规参数)名称错误,因为您设置了自定义字段名称。formPOST

来自 Passport 的官方文档:

默认情况下,LocalStrategy 期望在名为 username 和 password 的参数中找到凭据。如果您的站点希望以不同的方式命名这些字段,则可以使用选项来更改默认值。

passport.use(new LocalStrategy({
    usernameField: 'email',
    passwordField: 'passwd'
  },
  function(username, password, done) {
    // ...
  }
));

但是你的名字响了,你昨天不是问了类似的问题吗,我们帮你了,然后你把你的帖子删了?

于 2014-08-08T00:11:51.577 回答