Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
AngularJS bad POST request with Express / Passport

Trying to authenticate with passport via AngularJS. I've found a lot of threads saying that the issue is the default Content-Type of the request. Tried a couple of ways and I always get a 400 Error (Bad Request).

$http.post('/login', {
  username: $scope.user.username,
  password: $scope.user.password
})
.success(function(user) {
  console.log('success');
  // $location.url('/');
})
.error(function() {
  console.log('failed');
  // location.url('/login')
})

[Snipped some stuff that I don't think is the issue]

UDPATE: I think this might be an issue with passport. Using the body-parser middleware, I'm able to tell that I'm sending the data in the following format: { username: me, password: 12345 } Alas, I still get the error code 400.

var bodyParser       = require('body-parser');
var jsonParser       = bodyParser.json();
app.post('/login', jsonParser, function(req,res) {
  if (!req.body) {
    console.log('no body');
    return res.sendStatus(400);
  }

  console.log(req.body);
  res.end(req.user);
});

Here's the local strategy

passport.use(new LocalStrategy(
  function(username, password, done) {
    if (username === "me" && password === "123") {
      return done(null, {name: "me"});
    }
    return done(null, false, {message: 'Incorrect username or password.'});
  }
));

And the route for when I try to get passport to work.

app.post('/login',passport.authenticate('local'), function(req,res) {
  console.log('login in server.js')
  res.send(req.user);
});

Pretty sure this is all standard, based on everything I've read.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Needed this...

var bodyParser = require('body-parser');
app.use(bodyParser.json());
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!