2014-09-27 67 views
0

所以我有點問題在這裏。我有我的登錄路由:無法模塊化passport.js與節點

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]);   

    }); 

})); 

}; 

我真的不能明白爲什麼護照配置似乎沒有得到處理。當我打電話給護照。驗證它看起來什麼都不做

+0

這裏有很多錯誤。曾閱讀[文檔](http://passportjs.org/guide/authenticate/)? – vesse 2014-09-29 07:24:17

回答

2

所以,我做了幾件事情來完成這項工作。首先是我將以下內容移至passport.js在我的路由中有與require()護照相關的任何內容是瘋狂的。我一開始沒有這樣的混亂,但試圖修補已經發展到了這一點。此外,我的反序列化函數錯誤地有一個MySQL引用,而不是我的池。

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

然後我剛剛成立的護照像普通的配置,並沒有

module.exports = passport; 

然後只是做這app.js:

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

然後我改變了我的路由器,基本上刪除了帖子功能上的所有東西,並且確實:

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

我瞭解到我不能只調用passport.authenticate。它意味着被放置在一個req處理程序中。

對不起,完全瘋狂和模棱兩可的問題傢伙。爲了萬一任何可憐的靈魂尋找這條線,我想我至少需要有禮貌地談論它。