简体   繁体   English

当我在Node.js + Express中使用护照进行用户身份验证时会发生奇怪的事情

[英]Strange things happen when I use passport for user authentication in Nodejs+Express

In the beginning, I used this code to define LocalStrategy 开始时,我使用此代码定义LocalStrategy

    passport.use(
        'local-login',
        new LocalStrategy({
            usernameField:'username',
            passwordField: 'password',
            passReqtoCallback: true
        },
            function(req,username,password,done){
            ....

Then, the passport couldn't work well.It read password as username and couldn't read username from the HTML form. 然后,护照无法正常工作,它以用户名的身份读取密码,并且无法从HTML表单中读取用户名。

I was so confused and then I downloaded other people's source code and applied it to my code. 我很困惑,然后下载了其他人的源代码并将其应用于我的代码。 In fact, I only changed above part to this: 实际上,我只改变了一部分:

 passport.use(
  'local-login',
  new LocalStrategy({
   usernameField: 'username',
   passwordField: 'password',
   passReqToCallback: true
  },
 function(req,username,password,done){
            ....

And it just worked!!!! 它才有效!!!

Actually, I think those two codes are only different in indentation.So what is my original mistake?Could anyone tell me, thanks! 实际上,我认为这两个代码只是缩进不同,所以我最初的错误是什么?有人可以告诉我,谢谢!

(To make it more clear , I upload the screenshot from my IDE) (为使内容更清楚,我从IDE上传了屏幕截图)

This is buggy Code. 这是错误的代码。 这是儿童车代码

This is the code that works well. 这是运行良好的代码。 好代码

In your original code you have 在原始代码中

passport.use(
  'local-login',
  new LocalStrategy({
    ...
    passReqtoCallback: true // misspelled!
  }),
  ...
)

instead of 代替

passport.use(
  'local-login',
  new LocalStrategy({
    ...
    passReqToCallback: true
  }),
  ...
)

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

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