Passport authenticate is stuck and does not return any value









up vote
0
down vote

favorite












This authenticate works fine



app.post('/login', passport.authenticate('local-login', 
successRedirect: '/home',
failureRedirect: '/login',
failureFlash: true
)
);


but I'm trying to validate the fields of the form before the authentication using express-validator.



I came with that



app.post('/login', function(req, res)
req.checkBody('email', 'Email is required').notEmpty();
req.checkBody('email', 'Email is not valid').isEmail();
req.checkBody('password', 'Password is required').notEmpty();
var validationErr = req.validationErrors();

if (validationErr)
res.render('login',
errors: validationErr,
failureFlash: true
);
else
// authenticate once fields have been validated
passport.authenticate('local-login',
successRedirect: '/home',
failureRedirect: '/login',
failureFlash: true // allow flash messages
)

);


Using this second code, nothing happens when I submit the form and the client gives the error message localhost didn't send any data after a while. The first part works fine, I can see all the errors when I submit an empty form and the authenticate method is reached. I suspect this question might partially answer mine or is kind of related, but I can't understand it.



The passport.js documentation provides an example with a function but the function gets called only when the authentication is successful, so after. I'd like to perform the field validation before the authentication.



Let me know if you need the passport.authenticate code.










share|improve this question

























    up vote
    0
    down vote

    favorite












    This authenticate works fine



    app.post('/login', passport.authenticate('local-login', 
    successRedirect: '/home',
    failureRedirect: '/login',
    failureFlash: true
    )
    );


    but I'm trying to validate the fields of the form before the authentication using express-validator.



    I came with that



    app.post('/login', function(req, res)
    req.checkBody('email', 'Email is required').notEmpty();
    req.checkBody('email', 'Email is not valid').isEmail();
    req.checkBody('password', 'Password is required').notEmpty();
    var validationErr = req.validationErrors();

    if (validationErr)
    res.render('login',
    errors: validationErr,
    failureFlash: true
    );
    else
    // authenticate once fields have been validated
    passport.authenticate('local-login',
    successRedirect: '/home',
    failureRedirect: '/login',
    failureFlash: true // allow flash messages
    )

    );


    Using this second code, nothing happens when I submit the form and the client gives the error message localhost didn't send any data after a while. The first part works fine, I can see all the errors when I submit an empty form and the authenticate method is reached. I suspect this question might partially answer mine or is kind of related, but I can't understand it.



    The passport.js documentation provides an example with a function but the function gets called only when the authentication is successful, so after. I'd like to perform the field validation before the authentication.



    Let me know if you need the passport.authenticate code.










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      This authenticate works fine



      app.post('/login', passport.authenticate('local-login', 
      successRedirect: '/home',
      failureRedirect: '/login',
      failureFlash: true
      )
      );


      but I'm trying to validate the fields of the form before the authentication using express-validator.



      I came with that



      app.post('/login', function(req, res)
      req.checkBody('email', 'Email is required').notEmpty();
      req.checkBody('email', 'Email is not valid').isEmail();
      req.checkBody('password', 'Password is required').notEmpty();
      var validationErr = req.validationErrors();

      if (validationErr)
      res.render('login',
      errors: validationErr,
      failureFlash: true
      );
      else
      // authenticate once fields have been validated
      passport.authenticate('local-login',
      successRedirect: '/home',
      failureRedirect: '/login',
      failureFlash: true // allow flash messages
      )

      );


      Using this second code, nothing happens when I submit the form and the client gives the error message localhost didn't send any data after a while. The first part works fine, I can see all the errors when I submit an empty form and the authenticate method is reached. I suspect this question might partially answer mine or is kind of related, but I can't understand it.



      The passport.js documentation provides an example with a function but the function gets called only when the authentication is successful, so after. I'd like to perform the field validation before the authentication.



      Let me know if you need the passport.authenticate code.










      share|improve this question













      This authenticate works fine



      app.post('/login', passport.authenticate('local-login', 
      successRedirect: '/home',
      failureRedirect: '/login',
      failureFlash: true
      )
      );


      but I'm trying to validate the fields of the form before the authentication using express-validator.



      I came with that



      app.post('/login', function(req, res)
      req.checkBody('email', 'Email is required').notEmpty();
      req.checkBody('email', 'Email is not valid').isEmail();
      req.checkBody('password', 'Password is required').notEmpty();
      var validationErr = req.validationErrors();

      if (validationErr)
      res.render('login',
      errors: validationErr,
      failureFlash: true
      );
      else
      // authenticate once fields have been validated
      passport.authenticate('local-login',
      successRedirect: '/home',
      failureRedirect: '/login',
      failureFlash: true // allow flash messages
      )

      );


      Using this second code, nothing happens when I submit the form and the client gives the error message localhost didn't send any data after a while. The first part works fine, I can see all the errors when I submit an empty form and the authenticate method is reached. I suspect this question might partially answer mine or is kind of related, but I can't understand it.



      The passport.js documentation provides an example with a function but the function gets called only when the authentication is successful, so after. I'd like to perform the field validation before the authentication.



      Let me know if you need the passport.authenticate code.







      javascript node.js express express-validator






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 23:26









      Long Halloween

      32




      32






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          passport.authenticate is a function. In your first (working) code, you're calling it as middleware, where it gets passed objects for (req, res, next) as parameters.



          With your second code, you're trying to call it directly and without parameters, and the client is timing out because it's not getting a response.



          If I'm not missing something, you may be able to make this work by passing (req, res) to it, like this:



           if (validationErr)
          res.render('login',
          errors: validationErr,
          failureFlash: true
          );
          else
          // authenticate once fields have been validated
          passport.authenticate('local-login',
          successRedirect: '/home',
          failureRedirect: '/login',
          failureFlash: true // allow flash messages
          )(req, res, next);






          share|improve this answer




















          • As pointed in Jim’s answer the key here is the final (req,req,next) since passport.authenticate is a middleware, you need to call the req,res,next) and pass back a res for the function
            – Pari Baker
            Nov 12 at 1:42

















          up vote
          0
          down vote













          I had exactly the same code and issue whilst trying to introduce express-validator.



          I can confirm that adding the additional "(req, res, next);" to the end of the passport.authenticate function does allow the authentication process to complete and /login to process.



          However there seems to be no additional user added to the database after passport has authenticated with this method alone. I think a custom callback for passport is required e.g.:



          http://www.passportjs.org/docs/authenticate/



          Example of use: https://gist.github.com/Xeoncross/bae6f2c5be40bf0c6993089d4de2175e






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254274%2fpassport-authenticate-is-stuck-and-does-not-return-any-value%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            passport.authenticate is a function. In your first (working) code, you're calling it as middleware, where it gets passed objects for (req, res, next) as parameters.



            With your second code, you're trying to call it directly and without parameters, and the client is timing out because it's not getting a response.



            If I'm not missing something, you may be able to make this work by passing (req, res) to it, like this:



             if (validationErr)
            res.render('login',
            errors: validationErr,
            failureFlash: true
            );
            else
            // authenticate once fields have been validated
            passport.authenticate('local-login',
            successRedirect: '/home',
            failureRedirect: '/login',
            failureFlash: true // allow flash messages
            )(req, res, next);






            share|improve this answer




















            • As pointed in Jim’s answer the key here is the final (req,req,next) since passport.authenticate is a middleware, you need to call the req,res,next) and pass back a res for the function
              – Pari Baker
              Nov 12 at 1:42














            up vote
            0
            down vote



            accepted










            passport.authenticate is a function. In your first (working) code, you're calling it as middleware, where it gets passed objects for (req, res, next) as parameters.



            With your second code, you're trying to call it directly and without parameters, and the client is timing out because it's not getting a response.



            If I'm not missing something, you may be able to make this work by passing (req, res) to it, like this:



             if (validationErr)
            res.render('login',
            errors: validationErr,
            failureFlash: true
            );
            else
            // authenticate once fields have been validated
            passport.authenticate('local-login',
            successRedirect: '/home',
            failureRedirect: '/login',
            failureFlash: true // allow flash messages
            )(req, res, next);






            share|improve this answer




















            • As pointed in Jim’s answer the key here is the final (req,req,next) since passport.authenticate is a middleware, you need to call the req,res,next) and pass back a res for the function
              – Pari Baker
              Nov 12 at 1:42












            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            passport.authenticate is a function. In your first (working) code, you're calling it as middleware, where it gets passed objects for (req, res, next) as parameters.



            With your second code, you're trying to call it directly and without parameters, and the client is timing out because it's not getting a response.



            If I'm not missing something, you may be able to make this work by passing (req, res) to it, like this:



             if (validationErr)
            res.render('login',
            errors: validationErr,
            failureFlash: true
            );
            else
            // authenticate once fields have been validated
            passport.authenticate('local-login',
            successRedirect: '/home',
            failureRedirect: '/login',
            failureFlash: true // allow flash messages
            )(req, res, next);






            share|improve this answer












            passport.authenticate is a function. In your first (working) code, you're calling it as middleware, where it gets passed objects for (req, res, next) as parameters.



            With your second code, you're trying to call it directly and without parameters, and the client is timing out because it's not getting a response.



            If I'm not missing something, you may be able to make this work by passing (req, res) to it, like this:



             if (validationErr)
            res.render('login',
            errors: validationErr,
            failureFlash: true
            );
            else
            // authenticate once fields have been validated
            passport.authenticate('local-login',
            successRedirect: '/home',
            failureRedirect: '/login',
            failureFlash: true // allow flash messages
            )(req, res, next);







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 23:44









            Jim B.

            2,6561928




            2,6561928











            • As pointed in Jim’s answer the key here is the final (req,req,next) since passport.authenticate is a middleware, you need to call the req,res,next) and pass back a res for the function
              – Pari Baker
              Nov 12 at 1:42
















            • As pointed in Jim’s answer the key here is the final (req,req,next) since passport.authenticate is a middleware, you need to call the req,res,next) and pass back a res for the function
              – Pari Baker
              Nov 12 at 1:42















            As pointed in Jim’s answer the key here is the final (req,req,next) since passport.authenticate is a middleware, you need to call the req,res,next) and pass back a res for the function
            – Pari Baker
            Nov 12 at 1:42




            As pointed in Jim’s answer the key here is the final (req,req,next) since passport.authenticate is a middleware, you need to call the req,res,next) and pass back a res for the function
            – Pari Baker
            Nov 12 at 1:42












            up vote
            0
            down vote













            I had exactly the same code and issue whilst trying to introduce express-validator.



            I can confirm that adding the additional "(req, res, next);" to the end of the passport.authenticate function does allow the authentication process to complete and /login to process.



            However there seems to be no additional user added to the database after passport has authenticated with this method alone. I think a custom callback for passport is required e.g.:



            http://www.passportjs.org/docs/authenticate/



            Example of use: https://gist.github.com/Xeoncross/bae6f2c5be40bf0c6993089d4de2175e






            share|improve this answer


























              up vote
              0
              down vote













              I had exactly the same code and issue whilst trying to introduce express-validator.



              I can confirm that adding the additional "(req, res, next);" to the end of the passport.authenticate function does allow the authentication process to complete and /login to process.



              However there seems to be no additional user added to the database after passport has authenticated with this method alone. I think a custom callback for passport is required e.g.:



              http://www.passportjs.org/docs/authenticate/



              Example of use: https://gist.github.com/Xeoncross/bae6f2c5be40bf0c6993089d4de2175e






              share|improve this answer
























                up vote
                0
                down vote










                up vote
                0
                down vote









                I had exactly the same code and issue whilst trying to introduce express-validator.



                I can confirm that adding the additional "(req, res, next);" to the end of the passport.authenticate function does allow the authentication process to complete and /login to process.



                However there seems to be no additional user added to the database after passport has authenticated with this method alone. I think a custom callback for passport is required e.g.:



                http://www.passportjs.org/docs/authenticate/



                Example of use: https://gist.github.com/Xeoncross/bae6f2c5be40bf0c6993089d4de2175e






                share|improve this answer














                I had exactly the same code and issue whilst trying to introduce express-validator.



                I can confirm that adding the additional "(req, res, next);" to the end of the passport.authenticate function does allow the authentication process to complete and /login to process.



                However there seems to be no additional user added to the database after passport has authenticated with this method alone. I think a custom callback for passport is required e.g.:



                http://www.passportjs.org/docs/authenticate/



                Example of use: https://gist.github.com/Xeoncross/bae6f2c5be40bf0c6993089d4de2175e







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 12 at 19:17

























                answered Nov 12 at 17:48









                SamG

                12




                12



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254274%2fpassport-authenticate-is-stuck-and-does-not-return-any-value%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                    Museum of Modern and Contemporary Art of Trento and Rovereto