2016-11-03 50 views
-2

我正在使用Jared Hanson的connect-ensure-login方法,並且文檔表明它通過在登錄路徑上安裝authenticate()中間件與護照集成。如何保護除一個之外的所有路線?

我想知道這是否意味着只有這條路線受到保護?或者所有的路線是?

另外,我目前在我的應用程序中有重試計數/鎖定邏輯,但是這個模塊看起來相當簡單,因爲它只在失敗時重定向。是否可以使用更復雜的邏輯?

+0

會更好,如果你提供一些代碼或鏈接到你使用的模塊。 –

回答

0

您需要自行保護路線。由於路線定義的順序很重要,所以你會做例如。

// Obviously cannot be protected 
app.get('/login', function(req, res) { 
    res.send(...); 
}); 

// Authentication 
app.post('/login', passport.authenticate('strategy', { 
    successReturnToOrRedirect: '/', 
    failureRedirect: '/login' 
})); 

// A random unprotected route 
app.get('/unprotected', function(req, res) {}); 

// From now on all routes require authentication 
app.all('/*', ensureLoggedIn('/login')); 

// Root of domain for authenticated users only 
app.get('/', function(req, res) { 
    res.send(...); 
}); 

connect-ensure-login模塊只是定義了一個簡單的中間件,你可以很容易地定義你自己的,將只是你需要什麼做的。我相信很多或者甚至大多數使用更簡單的中間件檢測認證的用戶是這樣的:

var isAuthenticated = function(req, res, next) { 
    if (req.isAuthenticated()) { 
    return next(); 
    } else { 
    return res.redirect('/login'); 
    } 
}; 
相關問題