NodeJS&Express身份validation中间件无法正常运行
我试图运行函数isUserAuthenticated
每个请求到服务器通过要求它在app.js
和'使用'它是这样的: app.use(authenticate.isUserAuthenticated)
。
我有一个/authenticate
callback路由,由我们的SAML身份提供商POSTED,其中包含validation用户和会话所需的信息。 这是我的authenticate.js
文件中的内容:
module.exports = router; module.exports.isUserAuthenticated = function(req, res, next) { console.log(req.cookies.subject); if (req.cookies.subject) { console.log(req.cookies.subject) return next(); } res.redirect("LINK TO IDP FOR VERIFICATION, callback func. is then ran to get value of user and session"); }
正如所引用的,这个authentication函数在app.js
需要和使用: authenticate = require('./routes/authenticate')
和app.use(authenticate.isUserAuthenticated)
。
问题:无论if
语句的哪个变体validation请求中是否存在主题cookie,身份validation检查都不会被触发,redirect到IDP身份validation路由也不会被redirect。 上面的代码中的console.log
检查正在返回:
undefined
和{}
。
当我使用isUserAuthenticated
函数像这样: router.use('/', isUserAuthenticated, function(req, res, next) {...
,但我试图全局使用这个函数所以我不必在每条path上手动合并这个中间件。
任何意见/build议将不胜感激。 谢谢。
正如评论所build议的,
您可以将isUserAuthenticated
函数移动到app.js. 它看起来像这样
app.use(function(req, res, next) { if (req.cookies.subject) { next(); } else res.redirect("LINK TO IDP FOR VERIFICATION, callback func. is then ran to get value of user and session"); })
这将处理所有请求,然后再转发到路由。
如果您使用express js,则需要在路由器对象上设置中间件
router.use(isUserAuthenticated)
不要忘记把这个放在你的路线文件的顶部。
在这里查看应用程序级别和路由器级中间件的区别
- 我如何使用Passport.js返回到当前散列位置?
- 检查用户是否使用PassportJS / ExpressJSlogin
- nodejs HTTP摘要authentication不起作用
- 会话身份validationtypes错误:对象#属性“types”<ServerResponse>不是函数
- 在Nodejs中使用Google API进行服务validation
- Sdk的获取身份validation令牌张贴在facebook / twitter /链接,然后发布使用nodejs
- 如何识别Socket.IO(通过socketio-auth)成功的身份validation?
- 路由器中的Hapijs会话
- 使用NodeJS服务器devise移动身份validation