Tuesday, 13 November 2018

Beginner Passport/React: login system not working

I can't get this login system to work, here's my code:React code: login(username, password) { fetch('http://localhost:8080/login', { method: 'post', headers : { 'Content-Type': 'application/json', 'Accept': 'application/json, text/plain, */*', }, body: JSON.stringify({username: username, password: password}) }).then(res=>res.json()) .then(res => console.log(res)); } Node backend, called by the React code:app.post("/login", function(request, response) { passport.authenticate("local-login", function(err, user, info) { console.log("username: ", user) if (err) { return console.log(err); } if (!user) { return response.send(false); } request.logIn(user, function(err) { if (err) { return console.log(err); } checkLogIn(request, response); return response.send(true); }); })(request, response); }); And Passport.js: passport.use( 'local-login', new LocalStrategy({ usernameField : 'username', passwordField: 'password', passReqToCallback: true }, function(req, username, password, done){ connection.query("SELECT * FROM tbl_users WHERE username = ? ", [username], function(err, rows){ if(err) return done(err); if(!rows.length){ return done(null, false, req.send(JSON.stringify({ logged: "NO" }))); } if(!bcrypt.compareSync(password, rows[0].password)) return done(null, false, req.send(JSON.stringify({ logged: "NO" }))); return done(null, rows[0]); }); }) ); No matter what credentials I put in, I am getting 'False' in the Chrome console. This 'False' comes from the Node backend, this line:if (!user) {return response.send(false);}As you can see I am also console logging the username in the Node backend, and here's what I get from this command:username: falseWhat should I modify in order to console log the user's username if his credentials are correct, and console log an error message when it's incorrect?Cheers!

Submitted November 13, 2018 at 03:21PM by pythonistaaaaaaa

No comments:

Post a Comment