Friday, August 17, 2018

nodes passport Error: Unknown authentication strategy "local-login"

I have been trying to get local authentication work with passport on nodejs and as far as i can tell all of my code it is correct but i keep getting the same annoying error about "unknown authentication strategy so maybe someone else can help me with this problem my code is shown below.

Here is my code for passport configuration in nodejs.

var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var User = require('../models/user');

module.exports = function(passport) {


    passport.serializeUser(function(user, done) {
        done(null, user.id);
    });

    passport.deserializeUser(function(id, done) {
        User.findById(id, function(err, user) {
            done(err, user);
        });
    });

    passport.use('local-signup', new LocalStrategy({

        usernameField : 'username',
        passwordField : 'password',
        passReqToCallback : true
    },

    function(req, username, password, done) {

        process.nextTick(function() {

            User.findOne({ 'local.username' : username}, function(err, user) {
                if (err)
                    return done(err);

                if(user) {
                    return done(null, false, req.flash('signupMessage', 'That Username is already taken.'));
                }

                else {

                    var newUser = new User();

                    newUser.local.username = username;
                    newUser.local.password = newUser.generateHash(password);


                    newUser.save(function(err) {
                        if(err)
                            throw err;

                        return done(null, newUser);
                    });
                }
            });
        });
    }));



    passport.use('local-login', new LocalStrategy({

        usernameField : 'username',
        passwordField : 'password',
        passReqToCallback : true
    },
    function(req, username, password,done) {

        User.findOne({ 'local.username' : username}, function(err, user) {

            if(err)
                return done(err);

            if(!user)
                return done(null, false, req.flash('loginMessage', 'No user found.'));

            if(!user.validPassword(password))
                return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.'));

            return done(null, user);
        });
    }));

};

And here is the post on the server side.

app.post('/signin', passport.authenticate('local-login', {
    successRedirect : '/profile',
    failureRedirect : '/login',
    failureFlash : true
}));

And here is the form in the html doc






Create Account
Forgot Password

Can anyone please help me by telling me what i have done wrong. thanks

Solved

require('./config/passport')(passport);

Change the path of the file. Without this working, passport's configurations will not be passed to the routes.

Here is a snippet of where the line should be located:

// server.js

// configuration
===============================================================
mongoose.connect(configDB.url); // connect to our database

require('./config/passport')(passport); // pass passport for configuration

I also had the same problem but when I put this line of code after the app.use(flash()) it worked:

app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
require('./config/passport')(passport);

No comments:

Post a Comment