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: false
What 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!
node.js reactjs express passport.js
add a comment |
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: false
What 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!
node.js reactjs express passport.js
You're trying to send inside theLocalStrategy
, usingreq.send
(which doesn't exist). Use this instead:return done(null, false, logged: "NO" );
(you can access that data viainfo
in your route handler) Also double check that username and password are correct, and that your columns are calledusername
andpassword
.
– Chris G
Nov 13 '18 at 16:21
user & pass are correct, and the columns name are indeed username & password. I just changed this line and it didn't do anything..... :( When I console log the 'info' in the node backend, it gives me[0] username: message: 'Missing credentials'
– mokiliii Lo
Nov 13 '18 at 16:34
I can't replicate this becauseuser
beingfalse
suggests that the code tries to callreq.send()
, and this crashes node for me. Since you are gettingfalse
as a reply from the server, the only explanation I can see is that either the query returns zero rows, orbcrypt.compareSync
returns false. Just putconsole.log()
s all over the place and double check that variables actually contain what you think they contain, and functions return what you think they return.
– Chris G
Nov 13 '18 at 16:53
add a comment |
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: false
What 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!
node.js reactjs express passport.js
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: false
What 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!
node.js reactjs express passport.js
node.js reactjs express passport.js
asked Nov 13 '18 at 15:10
mokiliii Lomokiliii Lo
5118
5118
You're trying to send inside theLocalStrategy
, usingreq.send
(which doesn't exist). Use this instead:return done(null, false, logged: "NO" );
(you can access that data viainfo
in your route handler) Also double check that username and password are correct, and that your columns are calledusername
andpassword
.
– Chris G
Nov 13 '18 at 16:21
user & pass are correct, and the columns name are indeed username & password. I just changed this line and it didn't do anything..... :( When I console log the 'info' in the node backend, it gives me[0] username: message: 'Missing credentials'
– mokiliii Lo
Nov 13 '18 at 16:34
I can't replicate this becauseuser
beingfalse
suggests that the code tries to callreq.send()
, and this crashes node for me. Since you are gettingfalse
as a reply from the server, the only explanation I can see is that either the query returns zero rows, orbcrypt.compareSync
returns false. Just putconsole.log()
s all over the place and double check that variables actually contain what you think they contain, and functions return what you think they return.
– Chris G
Nov 13 '18 at 16:53
add a comment |
You're trying to send inside theLocalStrategy
, usingreq.send
(which doesn't exist). Use this instead:return done(null, false, logged: "NO" );
(you can access that data viainfo
in your route handler) Also double check that username and password are correct, and that your columns are calledusername
andpassword
.
– Chris G
Nov 13 '18 at 16:21
user & pass are correct, and the columns name are indeed username & password. I just changed this line and it didn't do anything..... :( When I console log the 'info' in the node backend, it gives me[0] username: message: 'Missing credentials'
– mokiliii Lo
Nov 13 '18 at 16:34
I can't replicate this becauseuser
beingfalse
suggests that the code tries to callreq.send()
, and this crashes node for me. Since you are gettingfalse
as a reply from the server, the only explanation I can see is that either the query returns zero rows, orbcrypt.compareSync
returns false. Just putconsole.log()
s all over the place and double check that variables actually contain what you think they contain, and functions return what you think they return.
– Chris G
Nov 13 '18 at 16:53
You're trying to send inside the
LocalStrategy
, using req.send
(which doesn't exist). Use this instead: return done(null, false, logged: "NO" );
(you can access that data via info
in your route handler) Also double check that username and password are correct, and that your columns are called username
and password
.– Chris G
Nov 13 '18 at 16:21
You're trying to send inside the
LocalStrategy
, using req.send
(which doesn't exist). Use this instead: return done(null, false, logged: "NO" );
(you can access that data via info
in your route handler) Also double check that username and password are correct, and that your columns are called username
and password
.– Chris G
Nov 13 '18 at 16:21
user & pass are correct, and the columns name are indeed username & password. I just changed this line and it didn't do anything..... :( When I console log the 'info' in the node backend, it gives me
[0] username: message: 'Missing credentials'
– mokiliii Lo
Nov 13 '18 at 16:34
user & pass are correct, and the columns name are indeed username & password. I just changed this line and it didn't do anything..... :( When I console log the 'info' in the node backend, it gives me
[0] username: message: 'Missing credentials'
– mokiliii Lo
Nov 13 '18 at 16:34
I can't replicate this because
user
being false
suggests that the code tries to call req.send()
, and this crashes node for me. Since you are getting false
as a reply from the server, the only explanation I can see is that either the query returns zero rows, or bcrypt.compareSync
returns false. Just put console.log()
s all over the place and double check that variables actually contain what you think they contain, and functions return what you think they return.– Chris G
Nov 13 '18 at 16:53
I can't replicate this because
user
being false
suggests that the code tries to call req.send()
, and this crashes node for me. Since you are getting false
as a reply from the server, the only explanation I can see is that either the query returns zero rows, or bcrypt.compareSync
returns false. Just put console.log()
s all over the place and double check that variables actually contain what you think they contain, and functions return what you think they return.– Chris G
Nov 13 '18 at 16:53
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283986%2fbeginner-passport-react-login-system-not-working%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
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283986%2fbeginner-passport-react-login-system-not-working%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
You're trying to send inside the
LocalStrategy
, usingreq.send
(which doesn't exist). Use this instead:return done(null, false, logged: "NO" );
(you can access that data viainfo
in your route handler) Also double check that username and password are correct, and that your columns are calledusername
andpassword
.– Chris G
Nov 13 '18 at 16:21
user & pass are correct, and the columns name are indeed username & password. I just changed this line and it didn't do anything..... :( When I console log the 'info' in the node backend, it gives me
[0] username: message: 'Missing credentials'
– mokiliii Lo
Nov 13 '18 at 16:34
I can't replicate this because
user
beingfalse
suggests that the code tries to callreq.send()
, and this crashes node for me. Since you are gettingfalse
as a reply from the server, the only explanation I can see is that either the query returns zero rows, orbcrypt.compareSync
returns false. Just putconsole.log()
s all over the place and double check that variables actually contain what you think they contain, and functions return what you think they return.– Chris G
Nov 13 '18 at 16:53