2012-08-27 66 views
0

我們有以下途徑驗證區域的節點路由?

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

和登錄路由的應用程序:

/login 

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

是否有可能關閉的路線,這樣實際上只/和/ login可以在沒有身份驗證的情況或者我們是否需要爲所有其他路線應用前綴。由於

回答

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', { 
    }); 
}); 

似乎工作

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