2014-11-20 87 views
0

我正在使用node.js,express和passport,並持續重定向到登錄頁面。奇怪的是,應用程序正在驗證用戶/護照,但隨後再次呈現登錄頁面。使用護照連續重定向到登錄頁面node.js

我怎麼能得到這個呈現索引頁認證?會話是否有問題,從而不會從登錄到索引進行身份驗證?

var mongoose = require('mongoose/'); 
mongoose.connect('mongodb://localhost/testdb'); 
passport = require('passport') 
LocalStrategy = require('passport-local').Strategy; 

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

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

var Schema = mongoose.Schema; 
var UserDetail = new Schema({ 
     username: String, 
     password: String 
    }, { 
     collection: 'userInfo' 
    }); 
var UserDetails = mongoose.model('userInfo', UserDetail); 

passport.use(new LocalStrategy(function(username, password, done) { 

    process.nextTick(function() { 

    UserDetails.findOne({ 
     'username': username, 
    }, function(err, user) { 

     if (err) { 

     return done(err); 
     } 

     if (!user) { 
     console.log("no such user...") 
     return done(null, false); 
     } 

     if (user.password != password) { 
     console.log("password doesn't match..") 
     return done(null, false); 
     } 
     console.log('found user') 
     return done(null, user); 
    }); 
    }); 
})); 


router.get('/', passport.authenticate('local', { failureRedirect: '/login' }), function(req, res) { 
    res.render('index', { title: 'theTitle' }); 
}); 

router.post('/login', passport.authenticate('local', { 
    failureRedirect: '/login'}), 
    function(req, res) { 
     console.log('success, redirecting to index page...') 
     res.redirect('/'); 
    } 
); 

這是我login.js文件:

router.get('/', function(req, res) { 
    res.render('login', { title: 'login' }); 
}); 

它遵循以下路徑:

GET/302 17.735 ms - 68 
GET /login 304 241.586 ms - - 
GET /javascripts/jquery.min.js 200 25.971 ms - 84245 
found user // this is being logged to the console 
success, redirecting to index page... //also being logged to the console 
POST /login 302 31.477 ms - 58 
GET/302 0.847 ms - 68 
GET /login 200 21.153 ms - 1012 
GET /javascripts/jquery-ui.min.js 200 51.780 ms - 239564 
+0

你找出解決方案了嗎? – Shikhar 2015-07-30 14:40:40

+0

你有沒有找到任何解決方案? – 2016-05-23 06:58:02

回答

0

我不知道護照固然不錯,但在我看來,在傳遞POST請求時,passport.authenticate在GET請求上失敗。檢查以確保您在GET請求中傳遞正確的參數。