简体   繁体   English

如何将用于身份验证的中间件功能从主服务器模块传递到路由器文件

[英]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.

相关问题 JS:如何将值从模块文件传递到主文件 - JS : How to pass value from module file to main file 如何将数据从中间件传递到路由器? - How can i pass data from middleware to router? react-router如何从客户端向服务器中间件发送请求? - How react-router send request to server middleware from client? Koa,Node.js,服务器-如何从Server对象获得Koa的路由器级中间件功能? - Koa, Node.js, Server - How can I get Koa's Router-level middleware function from the Server object? 如何在中间件中将参数传递给函数 - How to pass parameter to function in middleware 如何从我的快速中间件函数传递值? - How do I pass values from my express middleware function? 如何将文件名从multer传递到另一个中间件? - how to pass file names from multer to another middleware? 使用angular-ui-router在angular.js中将子状态从子模块传递到主模块 - Pass sub-state from sub-module to the main module in angular.js with angular-ui-router 您可以在没有回调的情况下从模块文件中的主node.js服务器文件中调用函数吗? - Can you call a function in your main node.js server file from within a module file without a callBack? 使用 module.exports= router 但仍获得 Router.use() 需要中间件 function 但获得 Object - Using module.exports= router but still getting Router.use() requires a middleware function but got a Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM