[英]How to pass a middleware function for authentication from the main server module to a router file
Somewhat new, please bear with me.有点新,请多多包涵。 Trying to use passport to authenticate only specific routes in a website.尝试使用通行证仅验证网站中的特定路线。 The routes are in a separate file called blog_a.js
.这些路由位于一个名为blog_a.js
的单独文件中。 I have the following functions created in the main server.js
file:我在主server.js
文件中创建了以下函数:
function checkAuthenticated(req, res, next) {
if (req.isAuthenticated()){
return next()
}
res.redirect('/login')
}
function checkNotAuthenticated(req, res, next) {
if (req.isAuthenticated()){
return res.redirect('/')
}
next()
}
However, I'm trying to pass the above functions into the blog_a.js
module, so I can use them as middleware to protect the routes within that module.但是,我正在尝试将上述函数传递到blog_a.js
模块中,因此我可以将它们用作中间件来保护该模块中的路由。
I have tried to use module.exports = {checkAuthenticated, checkNotAuthenticated}
at the bottom of the main 'server.js' file, and then use a let server = require('../server.js')
line to import these functions to the module that contains the routes I want to protect.我尝试在主“server.js”文件的底部使用module.exports = {checkAuthenticated, checkNotAuthenticated}
,然后使用let server = require('../server.js')
行来导入这些函数到包含我要保护的路由的模块。
However, the above server
variable comes back as undefined, and I've tried several permutations / destructuring methods to try to import it.但是,上面的server
变量返回未定义,我尝试了几种排列/解构方法来尝试导入它。 All to no avail, I keep getting the routes failing due to the "undefined" object-- Error: Route.get() requires a callback function but got a [object Undefined]
.一切都无济于事,由于“未定义”对象,我不断使路由失败 - Error: Route.get() requires a callback function but got a [object Undefined]
。
How can I set up the authentication in the server.js
file but then pass its authentication functions to be used as middleware within an individual route file?如何在server.js
文件中设置身份验证,然后传递其身份验证函数以用作单个路由文件中的中间件?
I looked at this solution , but it doesn't clearly explain how to get the middleware functions from one module-- server.js
--to another module-- blog_a.js
.我看着这个解决方案,但它并没有解释清楚如何从一个module--获得中间件功能server.js
--to另一个module-- blog_a.js
。
I got the following response on Patreon from Kyle of Web Dev Simplified, and it worked like a charm!我从 Web Dev Simplified 的 Kyle 那里得到了关于 Patreon 的以下回复,它的效果非常棒!
"You should be able to just create another file that is called authMiddleware.js or something like that. Then in that file define and export the functions at the end (module.exports = { function1, function2 }). Now in the places you need those functions you should be able to import them like so (const { function1, function2 } = require('./authMiddleware'))." “您应该能够创建另一个名为 authMiddleware.js 或类似文件的文件。然后在该文件中定义并导出最后的函数 (module.exports = { function1, function2 })。现在在您的位置需要那些函数,您应该能够像这样导入它们(const { function1, function2 } = require('./authMiddleware'))。”
So I followed Kyle's advice and created the following separate file authMiddleware.js
:所以我按照 Kyle 的建议创建了以下单独的文件authMiddleware.js
:
function checkAuthenticated(req, res, next) {
if (req.isAuthenticated()){
return next()
}
res.redirect('/login')
}
function checkNotAuthenticated(req, res, next) {
if (req.isAuthenticated()){
return res.redirect('/')
}
next()
}
module.exports = { checkAuthenticated, checkNotAuthenticated }
... and then used the following require statements to get access to the functions: ...然后使用以下 require 语句来访问函数:
-in main server.js
--> const {checkAuthenticated, checkNotAuthenticated} = require('./authMiddleware.js')
-in main server.js
--> const {checkAuthenticated, checkNotAuthenticated} = require('./authMiddleware.js')
-in router file blog_a.js
--> const {checkAuthenticated, checkNotAuthenticated} = require('../authMiddleware.js')
-in 路由器文件blog_a.js
--> const {checkAuthenticated, checkNotAuthenticated} = require('../authMiddleware.js')
Thanks!谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.