node 后台重定向,使用护照node.js连续重定向到登录页面

I am using node.js, express and passport and am having a continuous redirect to the login page. The strange thing is that the application is validating the user/passport, but then it renders the login page again.

How can I get this to render the index page upon authentication? Is there something wrong with the session, such that it doesn't carry the authentication over from login to index?

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('/');

}

);

This is in my login.js file:

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

res.render('login', { title: 'login' });

});

It follows the following path:

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

解决方案

I don't know Passport very well but it appears to me that passport.authenticate is failing on GET requests whilst passing on POST requests. Check to ensure you are passing the correct arguments in the GET request.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值