2016-11-11 36 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;

回答

相关问题