简体   繁体   English

需要帮助来了解NodeJS / PassportJS的工作方式

[英]Need help understanding how NodeJS / PassportJS works

I have the following 3 fragments of code taken from a working project I downloaded from internet: 我有以下3个代码片段,这些片段取自从Internet下载的一个工作项目:

file: ./routes.js 文件: ./ routes.js

 // ... var passport = require('passport'); var requireLoginLocal = passport.authenticate('local', { session: false }); var authController = require('./controllers/authController'); // ... module.exports = function(app) { // ... authRoutes.post('/login/local', requireLoginLocal, authController.login); // ... } 

file: ./config/passport.js 文件: ./ config / passport.js

 // ... var LocalStrategy = require('passport-local').Strategy; // ... module.exports = function(passport) { passport.use(new LocalStrategy({ usernameField: 'email' /* changing default */ }, function(email, password, done) { email = email.toLowerCase(); User.findOne({ 'email': email }, function(err, user) { if (err) { return done(err); } if (!user) { return done(null, false, { error: 'Login failed. Please try again.' }); } user.comparePassword(password, function(err, isMatch) { if (err) { return done(err); } if (!isMatch) { return done(null, false, { error: 'Login failed. Please try again.' }); } return done(null, user); }); } ); } )); }; 

file: ./controllers/authController.js 文件: ./ controllers / authController.js

 // ... exports.login = function(req, res, next) { var userInfo = getUserInfo(req.user); res.status(200).json({ token: 'JWT ' + generateToken(userInfo), user: userInfo }); } // ... 

I have the following questions: 我有以下问题:

1- On the file: ./routes.js , on the line: 1-在文件: ./routes.js ,在以下行:

authRoutes.post('/login/local', requireLoginLocal, authController.login);

how the data is passed from the 2nd to the 3rd argument? 数据如何从第二个参数传递到第三个参数?

2- On the file: ./config/passport.js , how affect those return values to the values passed from the 2nd to the 3rd argument on the line above? 2-在文件./config/passport.js ,这些返回值对上一行从第二个参数传递到第三个参数的值有何影响?

3- On file: ./controllers/authController.js , on function: exports.login , nothing is returned there, so, how that affects the values passed from the 3rd argument and a possible 4th argument on an hypothetical line like the one above? 3-在文件: ./controllers/authController.js ,在功能: exports.login ,返回什么都没有,所以,如何影响从第三个参数和可能的第四个参数传递类似上面的假想线的值?

Your 1st and 2nd question are linked. 您的第一个和第二个问题已链接。 This is the middleware pattern used by node specifically express in this case. 这是节点在这种情况下专门表达的中间件模式。 This arcticle explains this very nicely IMO. 这个细节很好地解释了IMO。

In my understanding and simple terms, they are functions which pass around the req, res objects ( info here ), by adding to them with their own changes. 以我的理解和简单的术语来说,它们是通过添加自己的更改而传递给req,res对象( 此处为info )的函数。

在此处输入图片说明

In your code you requireLoginLocal will add to the response object whatever it needs and calls next, in which case node will pass the response object to authController.login and so on. 在您的代码中,requireLoginLocal会根据需要添加到响应对象,然后调用它,在这种情况下,节点会将响应对象传递给authController.login,依此类推。 When all the middleware in the chain have done their necessary changes then the final response is send as http response 当链中的所有中间件都进行了必要的更改后,最终响应将作为http响应发送

As for your 2nd question, it's probably used by passport for configuration. 至于第二个问题,护照可能会将其用于配置。 The actual middleware for passport middleware is provide by this line 此行提供护照中间件的实际中间件

passport.authenticate('local', { session: false });

As this a deep and underlying base topic for node, you can google for node middleware and can find tons of articles out there about this 由于这是节点的基础知识,因此您可以在Google上搜索节点中间件,并且可以找到大量有关此的文章。

Answers to your questions: 您的问题的答案:

  1. Refer to the official express documentation . 请参阅官方快递文件
  2. This is the main functionality of passportjs, it acts as an authentication middleware, refer to the passport-local documentation . 这是passportjs的主要功能,它充当身份验证中间件,请参阅本地护照文档
  3. There can be multiple callbacks for a single route, but only when it is dependent on the previous callback. 单个路由可以有多个回调,但是仅当它依赖于先前的回调时才可以。 Also, any of the route callback can terminate the whole transferring the control to the next callback flow by sending the response with a status to the client. 同样,任何路由回调都可以通过将带有状态的响应发送给客户端来终止将控件转移到下一个回调流的整个过程。

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

相关问题 需要帮助了解为什么这样有效 - Need help understanding why this works 我正在分组 <li> 基于解决方案的类的元素,但需要帮助以了解其工作原理 - I'm grouping <li> elements by class, based on a solution, but need help understanding how it works 如果可能的话,我需要帮助理解这个jQuery过滤器函数如何逐行工作 - I need help understanding how this jQuery filter function works, line-by-line, if possible 我的第一个jQuery插件。需要帮助格式化并了解其工作原理 - My first jQuery plugin. Need help formatting and understanding how it works DOMContentLoaded事件似乎无法正常工作/我需要帮助来了解其工作原理 - DOMContentLoaded event doesn't seem to be working/I need help understanding how it works 需要一些帮助理解nodejs和socket.io - Need some help understanding nodejs and socket.io 需要帮助理解为什么我的代码会以它的方式工作(JS) - Need help understanding why my code works the way it does (JS) 需要帮助了解这些 TypeScript 声明有何不同 - Need help understanding how these TypeScript declarations are different 需要帮助来了解如何使用命名空间:( - need help understanding how to work with Name Space :( 需要帮助了解如何ajaxify一个网站 - Need help understanding how to ajaxify a web site
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM