last day (14 days later) » 

2:33 AM
0
A: jQuery AJAX call to Passportjs login on Express (nodejs) Framework

xShiraseThis should work, untested, let me know if there's typos. Basically, you need your express app to listen for a POST request on /login, in this route, you can add your passport auth. app.post('/login', function(req, res, next) { passport.authenticate('loginUsers', function(err, user, info) {...

 
mdv
I've edited the original question, im doing that (post route) but I cant pass the variables to passport correctly. Thanks
 
what does your local strategy look like?
 
mdv
see update on original question.
I'm not sure about passReqToCallback parameter.
 
you use it to to pass back the entire request to the callback if you want to display flash messages for example. If you don't need the req object, you can remove it
 
mdv
Is this line: data: {username: $('#us').val(), password: $('#pass').val()}, valid?
 
2:33 AM
yes, if you have the html elements with id 'us' and 'pass'. console.log your 'data' variable if you're unsure
 
mdv
Im getting passport message: "missing credentials"
 
so, first, check the data
you know what , I'll make a gist for you from some working code of mine
 
mdv
first of all
Thanks for your time
 
tell me if that works for you
no problems, I know what it is to struggle on a little problem when there's a lot more waiting :)
 
mdv
Indeed
Im checking the data object
 
2:46 AM
check the link I sent you, the first 27 lines are my strategy, the rest is the route
that's working code, I'm using it in a project
 
mdv
The thing is, the strategy that im using is working fine whiout Ajax
so the problem is in the ajax variables itself
 
mm ok
 
mdv
My route and strategy was working fine, but I wanted to use ajax so I dont need to reload the whole page
 
in that case, before your ajax call, console.log($('#us').val()) and $('#pass').val()
 
mdv
k just a asec
whell something strange happened
console throws
ndefined ?username=asd%40asd.com&password=asd:165
undefined ?username=asd%40asd.com&password=asd:168
(values are fine) asd@asd.com and password asd
but why undefined and a get route
lol
 
2:53 AM
that's weird :)
what does your html look like?
 
mdv
 <form role="form">
                                               <div class="form-group">
                                                   <label for="exampleInputEmail1">Email address</label>
                                                   <input type="email" class="form-control" id="email" name="username" id="us" placeholder="Enter email">
                                               </div>
                                               <div class="form-group">
                                                   <label for="exampleInputPassword1">Password</label>
 
yeah, nothing strange here
 
mdv
in the Jquery ajax im doing
<script type="text/javascript">

$(document).ready(function() {


$('form').submit(function() {
console.log($('#us').val());
password: $('#pass').val();

$.ajax({
type:'POST',
url:'/login',
data: {username: $('#us').val(), password: $('#pass').val()},
success:function(result){
if(!result){
//$('form input[name="username"]').css("background-color", "red");
console.log(data);
}

},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
}
});

});

});
 
oh, you have 2 "id=" on your inputs
id="email" name="username" id="us"
id="password" name="password" id="pass"
 
mdv
oh
wtf
let me fix that
 
2:58 AM
use just us and pass
then try that : $(document).ready(function() {


$('form').submit(function(e) {
e.preventDefault()
$.ajax({
type: 'POST',
url: '/login',
data: {
username: $('#us').val(),
password: $('#pass').val()
},
success: function(result) {
if (!result) {
//$('form input[name="username"]').css("background-color", "red");
console.log(result);
}
},
error: function(xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
}
});

});

});
 
mdv
1 sec
 
wait
made a small mistake
$(document).ready(function() {


$('form').on('submit', function(e){
e.preventDefault()
$.ajax({
type: 'POST',
url: '/login',
data: {
username: $('#us').val(),
password: $('#pass').val()
},
success: function(result) {
if (!result) {
//$('form input[name="username"]').css("background-color", "red");
console.log(result);
}
},
error: function(xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
}
});

});

});
 
mdv
whats the change?
 
$('form').on('submit', function(e){
e.preventDefault()
using the 'on' so it actually have the 'e' parameter
 
mdv
kk 1 sec
I guess theres some typo somewhere
cant close first (
.on(
 
mdv
checking
wow
now im getting:
[+] DB: user/password failure
POST /login 500 18ms - 23b
But that means that credentials are being read :D
 
well, thats working then :D
 
mdv
yup, now I need to know how to handle de data for the 500 error, but the original problem is solved
thank so much
Im going to rate your answare
 
thanks, i appreciate
 
mdv
Do u use Bitcoin?
 
3:10 AM
not anymore, I sold them all when they reched 1000$ (bought them at 13$) and never regretted :D
 
mdv
oh ok, I wanted to donate something for the troubles
 
that's fine really, it's not what stack overflow is about
but thanks anyway ;)
 
mdv
oh, ok, thank you so much for your time :D
 
ure welcome!
 

last day (14 days later) »