用节点模块化passport.js的麻烦

时间:2014-09-27 19:31:41

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

所以我有点问题。我有我的登录路线:

var express = require('express');
var router = express.Router();
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
require('../config/passport')(passport, LocalStrategy);


/* GET /login */
router.get('/', function(req, res) {
  //you'll probably write some 
res.render('login', { title: 'Please Log In' });
});

router.post('/', function(req, res) {
console.log('posting');
console.log(passport);
passport.initialize();
passport.session();
passport.authenticate('local', { successRedirect: '/',
                                 failureRedirect: '/login'
                                });
console.log('after');
});
module.exports = router;

我的app.js:

var express = require('express'),
  path = require('path'),
  favicon = require('serve-favicon'),
  logger = require('morgan'),
  cookieParser = require('cookie-parser'),
  bodyParser = require('body-parser'),
  session = require('express-session'), 
  RedisStore = require('connect-redis')(session),
  passport = require('passport'),

  //  LocalStrategy = require('passport-local').Strategy,
  pool = require('./config/database'),

  routes = require('./routes/index'),
  api = require('./routes/api'),
  login = require('./routes/login'),

  app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session(<<redis store info>>);
app.use(passport.initialize());
app.use(passport.session());


app.use('/', routes);
app.use('/api', api);
app.use('/login', login);
...etc

和我的./config/passport.js

var pool = require('./database');

module.exports = function(passport, LocalStrategy) {


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

// used to deserialize the user
passport.deserializeUser(function(id, done) {
    mysql.query("select * from users where id = "+id,function(err,rows){    
        done(err, rows[0]);
    });
});

passport.use(new LocalStrategy({
    usernameField : 'username',
    passwordField : 'password',
    passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, username, password, done) { // callback with user and password from our form
    console.log('hi');
     pool.query("SELECT * FROM `users` WHERE `user` = '" + username + "'",function(err,rows){
        if (err)
            return done(err);
         if (!rows.length) {
            return done(null, false, req.flash('loginMessage', 'No user found.')); // req.flash is the way to set flashdata using connect-flash
        } 

        // if the user is found but the password is wrong
        if (!( rows[0].password == password))
            return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata

        // all is well, return successful user
        return done(null, rows[0]);         

    });

}));

};

我无法理解为什么护照配置似乎根本没有得到处理。当我打电话给passport.authenticate时,它似乎无所事事

1 个答案:

答案 0 :(得分:2)

所以,我做了一些事情来让它发挥作用。首先是我将以下内容移入passport.js我的路线中有任何内部要求()护照是疯了。我开始没有这个混乱,但试图修补已经进展到那一点。此外,我的反序列化函数错误地有一个mysql引用而不是我的池。

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

然后我在配置中像普通一样设置护照,然后

module.exports = passport;

然后在app.js中执行此操作:

var passport = require('../config/passport');

然后我改变了我的路由器,基本上删除了post函数的全部内容,并且做了:

router.post('/', passport.authenticate('local', { successRedirect: '/',
                                                  failureRedirect: '/login'
                                                 }));

我知道我不能打电话给passport.authenticate。它本来是放在一个req处理程序中。

对不起那些完全疯狂和暧昧的问题。为了防止任何可怜的灵魂在寻找这个问题,我认为我至少需要礼貌地谈论它。