0

次のルートのアプリがあります

/dothis/
    ...//dothis routes
/dothat
    ...//dothat routes
/doother
    ...//doother routes 

およびログインルート:

/login

/   //which currently actually isn't even used, would redirect to /login

実際に認証なしで/と/loginのみにアクセスできるようにルートを閉じることは可能ですか?または、他のすべてのルートにプレフィックスを適用する必要がありますか。ありがとう

4

3 に答える 3

0
   app.get('*', function(req, res, next) {
         //  console.log(everyauth);
        if (!req.session.auth) {
            res.redirect('/login');
        } else {
          next();
        }
    });

app.get('/login', function(req, res){
  res.render('login', {
  });
});

動作するようです

于 2012-08-27T22:10:11.890 に答える
0

まさにこれを行うミドルウェアがあります: https://github.com/jaredhanson/connect-ensure-login

app.get('/dothat',
  ensureLoggedIn('/login'),  // redirect to /login if not logged in
  function(req, res) {
    // render do that;
  });

スタンドアロンで使用できますが、Passportとシームレスに統合されるため、ログイン後、ユーザーは最初に要求した URL にリダイレクトされます。

于 2012-08-28T18:21:48.777 に答える
0
app.all('*', Authentication, function(req, res) {
});

function Authentication(req, res, next) {
   if (req is not user) {
       if (req.url === '/' || req.url === '/login')
           next()
   }
   else
      next();
}
于 2012-08-27T22:13:26.743 に答える