简体   繁体   English

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

[英]Continuous redirecting to Login page using passport node.js

I am using node.js, express and passport and am having a continuous redirect to the login page. 我正在使用node.js,express和passport,并且不断重定向到登录页面。 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: 这是在我的login.js文件中:

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. 我不太了解Passport,但是在我看来,passport.authenticate在传递POST请求时对GET请求失败。 Check to ensure you are passing the correct arguments in the GET request. 检查以确保您在GET请求中传递正确的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM