Passport.js仅重定向到失败页面,而不调用LocalStrategy

时间:2014-01-02 17:46:53

标签: javascript node.js express passport.js

我的Express.js小应用程序中的Passport.js存在持久性问题:无论我放在LocalStrategy中,我总是被重定向到failureRedirect,显然甚至没有通过LocalStrategy ... 我错了什么?如何打印/显示最大的错误日志以执行?

var express = require('express'),
  passport = require('passport'),
  LocalStrategy = require('passport-local').Strategy;
  http = require('http'),
  path = require('path'),
  mysql = require('mysql'),
  flash = require('connect-flash');
var app = express();

// SQL connexion
var sqlInfo = {
  host: 'localhost', 
  user: 'root',
  password: '', 
  database: 'mex'
};
global.client = mysql.createConnection(sqlInfo);
client.connect(function(err) {
    if(err)
        console.log(err);
});

app.configure(function(){
    app.set('views', __dirname+'/views');
    app.set('port', process.env.PORT || 85);
    //app.engine('ejs', engine);
    app.use(express.static('public'));
    app.use(express.cookieParser());
    app.use(express.bodyParser());
   // 1 - Session express
    app.use(express.session({ secret: 'secretpass' }));
    // 2 - Init et session Passeport
    app.use(passport.initialize());
    app.use(passport.session());

    app.use(flash());
    app.use(express.logger());

    app.use(app.router);

});

// Simple logger
app.use(function(req, res, next){
  console.log('%s %s', req.method, req.url);
  next();    

passport.use(new LocalStrategy(
    function(username, password, done) {
        console.log("localstrategy ---");
        var user = {username: 'w@j.fr', password: 'pwd'};
        return done(null, user);
}));

passport.serializeUser(function(user, done) { 
  // JUST 4 TEST
  done(null, user);
});

passport.deserializeUser(function(user, done) { 
  // JUST 4 TEST
  done(null, user);
});

// Home
app.get('/', function(req, res){
    //check user session value, is logged in 
    if(req.user)
        res.render('menu_log.ejs', function(err, html){

            var data = {
                title: 'Home',
                menu: html
                //req.user['Prenom'] -- à afficher
            };
            res.render('index.ejs', data);

            console.log("index logué");
        });
    else
        res.render('menu_nolog.ejs', function(err, html){

            var data = {
                title: 'Home',
                menu: html
            };
            res.render('index.ejs', data);

            console.log("index PAS logué");
        });
});

// Passport's authentication
app.post('/login',
    passport.authenticate('local', { successRedirect: '/',
                                   successFlash: 'Bienvenue !',
                                   failureRedirect: '/wrong',
                                   failureFlash: "Email ou mot de passe incorrect." })
);

app.get('/logout', function(req, res){

    req.logout();
    res.redirect('/');
})

.get('/inscription', function(req, res){

    res.render('menu_nolog.ejs', function(err, html){

        var data = {
            title: 'Register',
            menu: html
        };

        res.render('inscription.ejs', data);
    });
});

http.createServer(app).listen(app.get('port'), function () {
    console.log('Express server listening on port ' + app.get('port'));
});

1 个答案:

答案 0 :(得分:1)

LocalStrategy需要两个参数(作为POST数据或在查询字符串中传递)才能存在:usernamepassword。如果你没有通过这两个,Passport甚至都不愿意调用策略处理程序。

可以通过将对象作为第一个参数传递给LocalStrategy构造函数来配置字段名称:

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