Passport local returns error 400 bad request with Angular

passport authenticate error handling
passport.authenticate not working
passport authenticate returns bad request
passport-local example
token error bad request passport
passport-local register user
express bad request
passport authenticate 400

I am trying to integrate passport to my code's login form. Client side calling server side works as it should until i call passport.authenticate in the request, 400 Bad Request was returned. What am I missing here.

HTML

        <div>
            <div class="row">
                <div class="input-field col s12">
                    <input id="user-email" type="text" ng-model="user.email">
                    <label for="user-email">Your email address</label>
                </div>
            </div>
            <div class="row">
                <div class="input-field col s12">
                    <input id="user-password" type="password" ng-model="user.password">
                    <label for="user-password">Your password</label>
                </div>
            </div>
            <div id="login-button-panel" class="center-align">
                <button class="btn" id="login-btn" ng-click="vm.login(user);">Login</button> 
            </div>
            <div class="section center">
                <a class="modal-trigger">Forgot password?</a>
            </div>
        </div>

JS

$http.post('/api/login',user).success(function(result){
    console.log(result)
})

server.js

passport.use(new LocalStrategy(
    function(username, password, done) {
        return done(null, false, {message:'Unable to login'})
    }
));
passport.serializeUser(function(user,done){
    done(null,user);
});

passport.deserializeUser(function(user,done){
    done(null,user);
});
app.post('/api/login', passport.authenticate('local'), function(req,res){
    res.json(req.user)
});

Bad Request was thrown by passport for missing access on username and password.

It is checking body and URL query for fields username and password. If either is falsy the request is rejected with status 400.

On creating your LocalStrategy you may pass set of options in additional argument to constructor choosing differently named fields using options usernameField and/or passwordField. In your particular case this would look like this:

passport.use(new LocalStrategy(
    {usernameField:"user-email", passwordField:"user-password"},
    function(username, password, done) {
        return done(null, false, {message:'Unable to login'})
    }
));

Route with passport.authenticate results in bad request · Issue #371 , Passport.js custom error for unauthorized and bad request - node.js. html css asp.net node.js angularjs Returning an error from passport.authenticate callback is not enough. !user) { const err = {}; err.status = 400; err.code = '​CP_SI_ValidationFailed'; return res.json(err); // send the error response to client } return next();  Hello, we reviewed this issue and determined that it doesn't fall into the bug report or feature request category. This issue tracker is not suitable for support requests, please repost your issue on StackOverflow using tag angular.

In my case (Express 4.0), I wasn't using body-parser

Passport.js custom error for unauthorized and bad request, Error:400 Bad Request - node.js. return done(null, user); }); }); }); passport.use(​localLogin); Note: My code is not using the requireLogin which is used to  Another common cause of a 400 Bad Request is when local DNS lookup data becomes either corrupted or out-of-date. Local DNS data isn’t stored by the browser but by the operating system itself. We have put together a detailed guide to clear the DNS cache for Windows and macOS operating systems.

This error also comes from trying to access the HTML DOM elements without using body-parser

body-parser is a module that let's you traverse the html document tree to read response especially in case of input fields

Use -

var parser = require('body-parser');
var urlencodedParser = parser.urlencoded({extended : false});


    app.post("/authenticate", urlencodedParser, passport.authenticate('local'), function (request, response)
    {           
        response.redirect('/');                      
    });

Error:400 Bad Request - node.js - html, I'm trying to authenticate Users with a Passport local Strategy. Same thing happens for Logging in--just a "Bad Request". This fixed the entire Error! able to send notifications to an angular web client and react native android & iOS clients. Last updated on: May 25th, 2020. In this tutorial, you'll learn how to build an authentication in your Node Angular app using Passport.js. The tutorial assumes the reader to be familiar with creating a basic application using Angular and Node.js Express framework.

passport.use(new LocalStrategy(
    {
        usernameField: 'email',
        passwordField: 'password'
    },
    function (email, password, done) {
        db.collection('User').findOne({ email: email }, async function (err, user) {
            console.log('user requested password caught in passport', password);
            if (err) { return done(err); }
            if (!user) { return done(null, false); }
            const matchPassword = await comparePassword(password, user.password);
            if (!matchPassword) { return done(null, false); }
            return done(null, user);
        });
    }
));

Passport Local Strategy login/register wont redirect and displays , passport-local by jaredhanson - Username and password authentication local returns error 400 bad request with Angular; Passport-local and  400 Bad Request errors, like all errors of this type, could be seen in any operating system and in any browser.

passport-local by jaredhanson, I am trying to integrate passport to my code's login form. Client side calling server side works as it should until i call passport.authenticate in the request, 400 Bad  So to address the 400 bad request errors, we must be mindful of how big the message content can be, and the message size should be bigger than the content size. Another thing to note is that when using the basicHttp binding, the default TransferMode is set to buffered.

MEAN Web Development, our user's server controller is able to process the Angular service requests. message = 'Something went wrong'; } } else { for (let errName in err.errors) { if function(req, res, next) { passport.authenticate('local', function(err, user, info) { if (​err || !user) { res.status(400).send(info); } else { // Remove sensitive data before  passport-local. Passport strategy for authenticating with a username and password. This module lets you authenticate using a username and password in your Node.js applications. By plugging into Passport, local authentication can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware

Full-Stack TypeScript Apps, Besides that, what makes combining Nest.js and Angular a good idea is that Then, if you head to http://localhost:3000 in your browser, you will see a "Hello World! For example, the following request should return a 400 (bad request) status, I also red about Passport Strategies in the Nest.js docs, but found this article  🐞 bug report Affected Package The issue is caused by package @angular/http Description When you call an HTTP endpoint that returns a response without a status code, e.g. "400", but not "404 Not Found", Angular by default sets statusCode

Comments
  • sorry for the late reply. And yes you are right. Thanks!
  • The options object should be the first argument in the LocalStrategy constructor, not the second argument in the passport.use method.
  • @Fkids Thanks for the advice. Fixed it.
  • Huge help! Thank you sir
  • Similarly, I had my app.use(express.json()); json body parser set after my /login
  • Try to explain more clearly why this is an answer to the question.
  • by default LocalStrategy takes takes username and password while authenticating from the request but we need is email and password.To make this happen we need { usernameField: 'email', passwordField: 'password' }