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

10,012

Solution 1

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

Solution 2

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);
Share:
10,012
jcwalker2006
Author by

jcwalker2006

Updated on June 05, 2022

Comments

  • jcwalker2006
    jcwalker2006 almost 2 years

    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

    <div id="signin">
            <form class="form-horizontal" method="POST" action="/signin">
                <div class="form-group">
                <label class="control-label col-sm-2">Username:</label> 
                <div class="col-xs-3">
                    <input type="text" class="form-control"></input><br>
                </div>
                </div>
                <div class="form-group">
                <label class="control-label col-sm-2">Password:</label>
                <div class="col-xs-3">
                    <input type="password" class="form-control"></input><br><br>
                </div>
                </div>
                <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" class="btn btn-default btn-lg">Sign In</button><br><br>
                </div>
                </div>
            </form>
    
            <div id="accountlinks">
                <a href="/signup">Create Account</a><br>
                <a href="">Forgot Password</a>
            </div>
        </div>
    

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