SQL Server and PassportJS












0















I am trying to create a form login with Nodejs, I am using mssql and passport.js, but it always redirect me to the failure login, I have checked the users created in the database but these are ok. I have removed bcrypt to test but it continue with the same issue.



Could somebody help me? Thanks in advance.



This is part of my passport.js file:



passport.serializeUser(function(usario, done) {
done(null, usuario.id);
});

// used to deserialize the user
passport.deserializeUser(function(Id, done) {
request.query("select * from tb_User where Id='"+Id+"'",function(err,rows){
done(err, rows[0]);
});
});

passport.use('local-login', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'usuario',
passwordField : 'Password',
passReqToCallback : true // allows us to pass back the entire request to the callback
}),

function(req, usuario, Password, done) { // callback with email and password from our form
request.query("select * from tb_User where usuario='"+usuario+"'",function(err,rows){
if (err){
return done(err);
}
if (!rows.length) {
return done(null, false, req.flash('loginMessage', 'No user found.')); // ash is the way to set flashdata using connect-flash
}
// if the user is found but the password is wrong
if (!(rows[0].Password == Password)){
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // the loginMessage and save it to session as flashdata
// all is well, return successful user
return done(null, rows[0]);
console.log('loged');
}
});
};


router.js



module.exports = function(app,passport) {
app.get('/index', function(req, res) {
res.render('/index.html',{ message: req.flash('loginMessage') });
});

// process the login form
app.post('/login', passport.authenticate('local-login', {
successRedirect : '/view/menu.html', // redirect to the secure profile section
failureRedirect : '/login.html', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}),

function(req, res) {
console.log("hello");

if (req.body.remember) {
req.session.cookie.maxAge = 1000 * 60 * 3;
} else {
req.session.cookie.expires = false;
}
res.redirect('/');
});

app.get('/logout', function(req, res) {
req.session.destroy();
req.logout();
res.redirect('/login.html');
});

function isLoggedIn(req,res,next){
// if user is authenticated in the session, carry on
if(req.isAuthenticated())
return next();
// if they aren't redirect them to the home page
res.redirect('/view/menu.html');
}
};









share|improve this question





























    0















    I am trying to create a form login with Nodejs, I am using mssql and passport.js, but it always redirect me to the failure login, I have checked the users created in the database but these are ok. I have removed bcrypt to test but it continue with the same issue.



    Could somebody help me? Thanks in advance.



    This is part of my passport.js file:



    passport.serializeUser(function(usario, done) {
    done(null, usuario.id);
    });

    // used to deserialize the user
    passport.deserializeUser(function(Id, done) {
    request.query("select * from tb_User where Id='"+Id+"'",function(err,rows){
    done(err, rows[0]);
    });
    });

    passport.use('local-login', new LocalStrategy({
    // by default, local strategy uses username and password, we will override with email
    usernameField : 'usuario',
    passwordField : 'Password',
    passReqToCallback : true // allows us to pass back the entire request to the callback
    }),

    function(req, usuario, Password, done) { // callback with email and password from our form
    request.query("select * from tb_User where usuario='"+usuario+"'",function(err,rows){
    if (err){
    return done(err);
    }
    if (!rows.length) {
    return done(null, false, req.flash('loginMessage', 'No user found.')); // ash is the way to set flashdata using connect-flash
    }
    // if the user is found but the password is wrong
    if (!(rows[0].Password == Password)){
    return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // the loginMessage and save it to session as flashdata
    // all is well, return successful user
    return done(null, rows[0]);
    console.log('loged');
    }
    });
    };


    router.js



    module.exports = function(app,passport) {
    app.get('/index', function(req, res) {
    res.render('/index.html',{ message: req.flash('loginMessage') });
    });

    // process the login form
    app.post('/login', passport.authenticate('local-login', {
    successRedirect : '/view/menu.html', // redirect to the secure profile section
    failureRedirect : '/login.html', // redirect back to the signup page if there is an error
    failureFlash : true // allow flash messages
    }),

    function(req, res) {
    console.log("hello");

    if (req.body.remember) {
    req.session.cookie.maxAge = 1000 * 60 * 3;
    } else {
    req.session.cookie.expires = false;
    }
    res.redirect('/');
    });

    app.get('/logout', function(req, res) {
    req.session.destroy();
    req.logout();
    res.redirect('/login.html');
    });

    function isLoggedIn(req,res,next){
    // if user is authenticated in the session, carry on
    if(req.isAuthenticated())
    return next();
    // if they aren't redirect them to the home page
    res.redirect('/view/menu.html');
    }
    };









    share|improve this question



























      0












      0








      0








      I am trying to create a form login with Nodejs, I am using mssql and passport.js, but it always redirect me to the failure login, I have checked the users created in the database but these are ok. I have removed bcrypt to test but it continue with the same issue.



      Could somebody help me? Thanks in advance.



      This is part of my passport.js file:



      passport.serializeUser(function(usario, done) {
      done(null, usuario.id);
      });

      // used to deserialize the user
      passport.deserializeUser(function(Id, done) {
      request.query("select * from tb_User where Id='"+Id+"'",function(err,rows){
      done(err, rows[0]);
      });
      });

      passport.use('local-login', new LocalStrategy({
      // by default, local strategy uses username and password, we will override with email
      usernameField : 'usuario',
      passwordField : 'Password',
      passReqToCallback : true // allows us to pass back the entire request to the callback
      }),

      function(req, usuario, Password, done) { // callback with email and password from our form
      request.query("select * from tb_User where usuario='"+usuario+"'",function(err,rows){
      if (err){
      return done(err);
      }
      if (!rows.length) {
      return done(null, false, req.flash('loginMessage', 'No user found.')); // ash is the way to set flashdata using connect-flash
      }
      // if the user is found but the password is wrong
      if (!(rows[0].Password == Password)){
      return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // the loginMessage and save it to session as flashdata
      // all is well, return successful user
      return done(null, rows[0]);
      console.log('loged');
      }
      });
      };


      router.js



      module.exports = function(app,passport) {
      app.get('/index', function(req, res) {
      res.render('/index.html',{ message: req.flash('loginMessage') });
      });

      // process the login form
      app.post('/login', passport.authenticate('local-login', {
      successRedirect : '/view/menu.html', // redirect to the secure profile section
      failureRedirect : '/login.html', // redirect back to the signup page if there is an error
      failureFlash : true // allow flash messages
      }),

      function(req, res) {
      console.log("hello");

      if (req.body.remember) {
      req.session.cookie.maxAge = 1000 * 60 * 3;
      } else {
      req.session.cookie.expires = false;
      }
      res.redirect('/');
      });

      app.get('/logout', function(req, res) {
      req.session.destroy();
      req.logout();
      res.redirect('/login.html');
      });

      function isLoggedIn(req,res,next){
      // if user is authenticated in the session, carry on
      if(req.isAuthenticated())
      return next();
      // if they aren't redirect them to the home page
      res.redirect('/view/menu.html');
      }
      };









      share|improve this question
















      I am trying to create a form login with Nodejs, I am using mssql and passport.js, but it always redirect me to the failure login, I have checked the users created in the database but these are ok. I have removed bcrypt to test but it continue with the same issue.



      Could somebody help me? Thanks in advance.



      This is part of my passport.js file:



      passport.serializeUser(function(usario, done) {
      done(null, usuario.id);
      });

      // used to deserialize the user
      passport.deserializeUser(function(Id, done) {
      request.query("select * from tb_User where Id='"+Id+"'",function(err,rows){
      done(err, rows[0]);
      });
      });

      passport.use('local-login', new LocalStrategy({
      // by default, local strategy uses username and password, we will override with email
      usernameField : 'usuario',
      passwordField : 'Password',
      passReqToCallback : true // allows us to pass back the entire request to the callback
      }),

      function(req, usuario, Password, done) { // callback with email and password from our form
      request.query("select * from tb_User where usuario='"+usuario+"'",function(err,rows){
      if (err){
      return done(err);
      }
      if (!rows.length) {
      return done(null, false, req.flash('loginMessage', 'No user found.')); // ash is the way to set flashdata using connect-flash
      }
      // if the user is found but the password is wrong
      if (!(rows[0].Password == Password)){
      return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // the loginMessage and save it to session as flashdata
      // all is well, return successful user
      return done(null, rows[0]);
      console.log('loged');
      }
      });
      };


      router.js



      module.exports = function(app,passport) {
      app.get('/index', function(req, res) {
      res.render('/index.html',{ message: req.flash('loginMessage') });
      });

      // process the login form
      app.post('/login', passport.authenticate('local-login', {
      successRedirect : '/view/menu.html', // redirect to the secure profile section
      failureRedirect : '/login.html', // redirect back to the signup page if there is an error
      failureFlash : true // allow flash messages
      }),

      function(req, res) {
      console.log("hello");

      if (req.body.remember) {
      req.session.cookie.maxAge = 1000 * 60 * 3;
      } else {
      req.session.cookie.expires = false;
      }
      res.redirect('/');
      });

      app.get('/logout', function(req, res) {
      req.session.destroy();
      req.logout();
      res.redirect('/login.html');
      });

      function isLoggedIn(req,res,next){
      // if user is authenticated in the session, carry on
      if(req.isAuthenticated())
      return next();
      // if they aren't redirect them to the home page
      res.redirect('/view/menu.html');
      }
      };






      node.js sql-server passport.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 19:47









      kit

      1,1063816




      1,1063816










      asked Nov 23 '18 at 15:26









      daniel gutierrezdaniel gutierrez

      13




      13
























          0






          active

          oldest

          votes











          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',
          autoActivateHeartbeat: false,
          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%2f53449305%2fsql-server-and-passportjs%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449305%2fsql-server-and-passportjs%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







          Popular posts from this blog

          Feedback on college project

          Futebolista

          Albești (Vaslui)