简体   繁体   English

为什么我的护照中间件 function 不能正常工作?

[英]Why isn't my passport middleware function working correctly?

I am new to web development, and am working on setting up user authentication with Passport.js for a Node.js app.我是 web 开发的新手,并且正在使用 Passport.js 为 Node.js 应用程序设置用户身份验证。 Everything about the user authentication works perfectly, except for the image I am trying to load on the login form.除了我试图在登录表单上加载的图像之外,有关用户身份验证的所有内容都运行良好。 I know that the image's failure to load is somehow tied to my middleware function, because when I remove the middleware function by commenting out the line: app.use(checkAuthenticated);我知道图像加载失败在某种程度上与我的中间件 function 相关,因为当我通过注释掉以下行删除中间件 function 时: app.use(checkAuthenticated); , the image loads correctly, and everything is good. ,图像加载正确,一切都很好。 This is my middleware function:这是我的中间件 function:

    function checkAuthenticated(req, res, next) {
      if (req.isAuthenticated() || req.path.includes('login')) {
        return next()
      }
        res.redirect('/login')
        return;
    }

I also assumed that this type of middleware function to check Authentication with Passport.js would be quite common, so I would appreciate any feedback whatsoever about what the best thing to do for such a middleware function is.我还假设这种类型的中间件 function 来检查 Passport.js 的身份验证会很常见,所以我将不胜感激任何关于如何为这样的中间件 function 做最好的事情的反馈。 I feel like my implementation was somewhat improper, the "or path.includes('login')" bit feels very inelegant, but it is the only thing I could think of that made the function work, without it getting trapped in an infinite loop(because upon first visit, the user wouldn't be authenticated, and each redirect would continue to get redirected).我觉得我的实现有点不合适,“or path.includes('login')”位感觉很不雅,但这是我能想到的唯一让 function 工作的东西,而不会陷入无限循环(因为在第一次访问时,用户不会被认证,并且每个重定向都会继续被重定向)。 Thank you for any help you may provide, and please let me know if there is anything I can do to clarify anything.感谢您提供的任何帮助,如果有什么我可以做的澄清任何事情,请告诉我。

Edit: I also wanted to add, in case this is of any help, how the login file is being served, and how the image is being loaded into the login file.编辑:我还想添加,以防万一这有任何帮助,如何提供登录文件,以及如何将图像加载到登录文件中。 For the command to serve the login file, I have:对于提供登录文件的命令,我有:

app.get('/login', function(req, res) {
  res.sendFile(path.join(__dirname, 'public', 'login-page.html'));
});

And for the login file's link to the image, I have just the classic: <img src="path to the image">对于登录文件的图像链接,我只有经典: <img src="path to the image">

I found out what mistake I made, just going to leave the question and answer here in case anyone had a similar issue.我发现我犯了什么错误,只是将问题和答案留在这里,以防有人遇到类似问题。 So the middleware function is called every time anything gets served, including the images/css that are tied to the html file.因此,每次提供任何内容时都会调用中间件 function,包括与 html 文件相关的图像/css。 When my login.html file was served, it had to have the css and image go through the middleware function before also being served. When my login.html file was served, it had to have the css and image go through the middleware function before also being served. The somewhat jank solution I had meant that any request that included the word login would go through, and my css file was named login.css, so it went through, but my image was named something that didn't include the word login, so it was blocked for non-authenticated users(all users on the login page would not be authenticated).有点卡顿的解决方案我的意思是任何包含单词 login 的请求都将通过 go,而我的 css 文件名为 login.css,所以它包含了这个词,但我的图像没有命名它被未经身份验证的用户阻止(登录页面上的所有用户都不会经过身份验证)。 I am still open to any opinions from web developers about more elegant solutions though.不过,我仍然愿意接受 web 开发人员关于更优雅解决方案的任何意见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM