重定向基于MongoDB的护照登录查找
重定向基于MongoDB的护照登录查找
我已经登录路线router.post('/login', authController.login).
我的控制器使用的护照,看起来像:
exports.login = passport.authenticate('local', {
failureRedirect: '/login',
failureFlash: 'Failed Login',
successRedirect: '/',
successFlash: 'You are now logged in'
});
这工作得很好,但我想重定向到/store
如果用户不拥有设备和/device
如果他们这样做,基于查询:
const devices = await Device.find({ owner: req.user._id });
做这个的最好方式是什么?我试过了:
router.post('/login', authController.login, deviceController.hasDevice);
exports.login = (req, res, next) => {
passport.authenticate('local', {
failureRedirect: '/login',
failureFlash: 'Failed Login'
});
next();
};
exports.hasDevice = async (req, res) => {
const devices = await Device.find({ owner: req.user._id });
if (!devices.length) {
res.render('store', { title: 'Store' });
} else {
res.render('devices', { title: 'My Devices', devices });
}
};
但对REQ没有用户对象,当我到达hasDevice。即我得到“无法找到的不确定_id。”
回答如下:您可以通过使用passport.authenticate()
定制的回调实现这一目标。
router.post('/login', async function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err || !user) {
return res.redirect('/login');
}
const devices = await Device.find({ owner: user._id });
if (!devices.length) {
res.render('store', { title: 'Store' });
} else {
res.render('devices', { title: 'My Devices', devices });
}
})(req, res, next);
});
最新文章
- 物权法全文内容有哪些呢
- “脱离应用开发者的数据库,不会成功”,黄东旭万字长文剖析数据库发展新趋势...
- 【时间复杂度】时间复杂度
- Vue中的component
- 关于platform
- Linux中mysql的重启
- 聊聊职场
- 贝叶斯网络综合应用
- 数字芯片设计流程
- Vss2005 的使用方法
- SAR成像系列:【5】合成孔径雷达(SAR)成像算法
- postgresql 命令行操作
- lssvr matlab,lssvm(最小二乘支持向量机)matlab
- itoa函数和atoi函数的实现和用法
- Linux C编程下没有 itoa()函数的问题
- 磁盘分区形式:主启动记录(MBR)和全局唯一标识分区表(GPT)
- shiro漏洞原理以及检测key值原理
- shiro框架基础
- ST
- Qt中SQL QSqlQuery 对象中prepare() 函数的用法