M
M
Maxim2017-05-11 17:24:20
JavaScript
Maxim, 2017-05-11 17:24:20

Why doesn't passport js accept the correct username and password?

There is this code

passport.use(new LocalStrategy({
    usernameField: 'email',
    passwordField: 'password'
}, function(username, password, done) {

    User.findOne({ username: username }, function(err, user) {
        console.log(username, password)
        return err ?
            done(err) :
            user ?
            password === user.password ?
            done(null, user) :
            done(null, false, { message: 'Incorrect password.' }) :
            done(null, false, { message: 'Incorrect username.' });
    });
}));

app.post('/logins', function(req, res, next) {
    passport.authenticate('local',
        function(err, user, info) {
            console.log(err, user, info)
            return err ?
                next(err) :
                user ?
                req.logIn(user, function(err) {
                    return err ?
                        next(err) :
                        res.redirect('/admin');
                }) :
                res.redirect('/login');
        }
    )(req, res, next);
});

I honestly took this code from Habr, before that I used the one that is written in the documentation. But the result is the same, redirect /login. I can't understand what? Both of these blocks of code are in the same file.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Dolgolyuk, 2017-05-12
@dolgo

Check your POST request for a Content-Type header.
Should be: application/x-www-form-urlencoded.
Otherwise, it won't even reach the callback.
There was such a problem with passport, perhaps your case

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question