一旦authentication,总是去索引页面,而不是login

我有一个护照的小问题,一旦我成功login总是默认去“/ index”而不是“/”<—这是login页面。

默认情况下,用户将进入“/”,如果他们已经有一个会话,他们将被自动redirect到“/ index”

我有一些地方,如果用户没有通过身份validation,他们不能访问“/索引”,但不断获得redirect循环与我试图防止这一切,我只用了护照的最后一天左右,并有这个问题的问题。

我有一些地方,如果用户没有通过身份validation,他们不能访问“/索引”

任何人有什么build议,缺less什么?

//Gets the login route and renders the page. app.get('/', function (req, res) { res.render('login'); }); //The index page, isLoggedIn function always called to check to see if user is authenticated or in a session, if they are they can access the index route, if they are not they will be redirected to the login route instead. app.get('/index', checkLoggedIn, function (req, res) { res.render('index.ejs', { isAuthenticated : req.isAuthenticated(), user : req.user }); }); //This is where the users log in details are posted to, if they are successfull then redirect to index otherwise keep them on the login route. app.post('/login', passport.authenticate('local', { successRedirect : '/index', failureRedirect : '/', failureFlash : 'Invalid username or password.' })); //When logout is clicked it gets the user, logs them out and deletes the session, session cookie included then redirects the user back to the login route. app.get('/logOut', function (req, res) { req.logOut(); req.session.destroy(); res.redirect('/') }); //Check to see if logged in, if so carry on otherwise go back to login. function checkLoggedIn(req, res, next) { // if user is authenticated in the session, carry on if (req.isAuthenticated()) return next(); // if they aren't redirect them to the index page res.redirect('/'); } 

亲切的问候

正如所评论的,你可以这样做一个中间件:

 app.get('/', redirectToIndexIfLoggedIn, function (req, res) { res.render('login'); }); function redirectToIndexIfLoggedIn(req, res, next) { if (req.isAuthenticated()) res.redirect('/index'); return next(); }