使用护照node.js连续重定向到登录页面 [英] Continuous redirecting to Login page using passport node.js

查看:199
本文介绍了使用护照node.js连续重定向到登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用node.js,express和passport,并且不断重定向到登录页面.奇怪的是,应用程序正在验证用户/护照,但随后会再次呈现登录页面.

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

这是在我的login.js文件中:

This is in my login.js file:

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

推荐答案

我不太了解Passport,但是在我看来,passport.authenticate在传递POST请求时对GET请求失败.检查以确保您在GET请求中传递了正确的参数.

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.

这篇关于使用护照node.js连续重定向到登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆