2016-11-11 37 views
0

我正在使用護照和OAuth對我的網站上的用戶進行身份驗證。我製作了中間件來檢查用戶是否使用OAuth登錄,以及他們是否在允許的用戶列表中。中間件工作並防止用戶在未經授權的情況下訪問使用它的數據路由。問題是,在Sequelize catch處理程序未能在列表中找到用戶之後,永遠不會調用該響應,因此如果它失敗並且它只顯示沒有數據的頁面,它們就不會重定向。在Sequelize catch處理程序中未執行的快速響應

這是我的中間件。我知道catch已經到達,因爲我可以看到console.log,但是絕不會發生這種情況。讓我知道是否有代碼的其他部分有助於查看。在此先感謝您的幫助!

'use strict'; 
 
const clc = require(`cli-color`); 
 
const models = require('../models'); 
 
const User = models.User; 
 

 
function isLoggedIn(req, res, next) { 
 
    console.log(clc.red.bgBlue(` ::: `), req.user); 
 
    res.header('Access-Control-Allow-Credentials', true); 
 
    models.sql.sync() 
 
    .then(() => { 
 
     User.findOne({ 
 
     where: { 
 
      email: req.user.unique_name 
 
     } 
 
     }); 
 
    }) 
 
    .then((user) => { 
 
     if (req.isAuthenticated() && user !== null && user.email === req.user.unique_name) { // if user is authenticated in the session and the user email matches the user found in the db, carry on 
 
     return next(); 
 
     } else { 
 
     // if they aren't send not authorized 
 
     res.status('401').redirect(`/admin/login`); 
 

 
     } 
 
    }) 
 
    .catch((err) => { 
 
     console.log(clc.red.bgWhite(`ERR :::: )`), err); 
 
     res.status(`401`).redirect(`/admin/login`); 
 
    }); 
 

 
} 
 

 
module.exports = isLoggedIn;

回答

相關問題