Node.js passport.authenticate is not a function, how do I solve this?

1

As requested, I narrowed down the codes a little and kept only the essentials that I believe were involved with the problem. Reformulating my question: I have a simple register in a modal (only name, email, phone and password) I can navigate the site quietly but when I enter data in this register and try to submit (sending the data through a post for the signup route down there , in the third code sample) the following error occurs.

passport.authenticate is not a function

TypeError: passport.authenticate is not a function     at module.exports (C: \ Users \ bruno \ Desktop \ pe \ routes \ rotaslogin-cadastro.js: 26: 33)     at the Layer.handle [as handle_request] (C: \ Users \ bruno \ Desktop \ pe \ node_modules \ express \ lib \ router \ layer.js: 95: 5)     at trim_prefix (C: \ Users \ bruno \ Desktop \ pe \ node_modules \ express \ lib \ router \ index.js: 317: 13)     at C: \ Users \ bruno \ Desktop \ pe \ node_modules \ express \ lib \ router \ index.js: 284: 7     at Function.process_params (C: \ Users \ bruno \ Desktop \ pe \ node_modules \ express \ lib \ router \ index.js: 335: 12)     at next (C: \ Users \ bruno \ Desktop \ pe \ node_modules \ express \ lib \ router \ index.js: 275: 10)     at C: \ Users \ bruno \ Desktop \ pe \ node_modules \ express \ lib \ router \ index.js: 635: 15     at next (C: \ Users \ bruno \ Desktop \ pe \ node_modules \ express \ lib \ router \ index.js: 260: 14)     at Function.handle (C: \ Users \ bruno \ Desktop \ pe \ node_modules \ express \ lib \ router \ index.js: 174: 3)     at router (C: \ Users \ bruno \ Desktop \ pe \ node_modules \ express \ lib \ router \ index.js: 47: 12)     at the Layer.handle [as handle_request] (C: \ Users \ bruno \ Desktop \ pe \ node_modules \ express \ lib \ router \ layer.js: 95: 5)     at trim_prefix (C: \ Users \ bruno \ Desktop \ pe \ node_modules \ express \ lib \ router \ index.js: 317: 13)     at C: \ Users \ bruno \ Desktop \ pe \ node_modules \ express \ lib \ router \ index.js: 284: 7     at Function.process_params (C: \ Users \ bruno \ Desktop \ pe \ node_modules \ express \ lib \ router \ index.js: 335: 12)     at next (C: \ Users \ bruno \ Desktop \ pe \ node_modules \ express \ lib \ router \ index.js: 275: 10)     at: \ Users \ bruno \ Desktop \ pe \ node_modules \ connect-flash \ lib \ flash.js: 21: 5

Follow the code lines related to the app.js passport

    var mongoose = require('mongoose');
    var passport = require('passport');
    var flash = require('connect-flash');

    var configDB = require('./config/database.js');


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

    app.use(passport.initialize());
    app.use(passport.session()); // persistent login sessions
    app.use(flash()); // use connect-flash for flash messages stored in session


    require('./routes/rotaslogin-cadastro')(app, passport);
    require('./config/passport')(passport); // pass passport for configuration

    module.exports = app;

Follow the passport.js code:

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);
        });
    });

 // LOCAL SIGNUP
    passport.use('local-signup', new LocalStrategy({
            // by default, local strategy uses username and password, we will override with email
            usernameField: 'email',
            passwordField: 'senha',
            telefoneField: 'telefone',
            nomeField: 'nome',
            passReqToCallback: true // allows us to pass back the entire request to the callback
        },
        function(req, email, senha, telefone, nome, done) {

            // asynchronous
            // User.findOne wont fire unless data is sent back
            process.nextTick(function() {

                // find a user whose email is the same as the forms email
                // we are checking to see if the user trying to login already exists
                User.findOne({ 'local.email': email }, function(err, user) {
                    // if there are any errors, return the error
                    if (err)
                        return done(err);

                    // check to see if theres already a user with that email
                    if (user) {
                        return done(null, false, req.flash('signupMessage', 'Esse email já existe.'));
                    } else {

                        // create the user
                        var newUser = new User();

                        // set the user's local credentials
                        newUser.local.email = email;
                        newUser.local.senha = newUser.generateHash(senha);
                        newUser.local.telefone = telefone;
                        newUser.local.nome = nome;

                        // save the user
                        newUser.save(function(err) {
                            if (err)
                                throw err;
                            return done(null, newUser);
                        });
                    }

                });

            });

        }));


    // LOCAL LOGIN 

    passport.use('local-login', new LocalStrategy({
            // by default, local strategy uses username and password, we will override with email
            usernameField: 'email',
            passwordField: 'senha',
            passReqToCallback: true // allows us to pass back the entire request to the callback
        },
        function(req, email, senha, telefone, nome, done) { // callback with email and password from our form

            // find a user whose email is the same as the forms email
            // we are checking to see if the user trying to login already exists
            User.findOne({ 'local.email': email }, function(err, user) {
                // if there are any errors, return the error before anything else
                if (err)
                    return done(err);

                // if no user is found, return the message
                if (!user)
                    return done(null, false, req.flash('loginMessage', 'Não existe um usuario com esse email.')); // req.flash is the way to set flashdata using connect-flash

                // if the user is found but the password is wrong
                if (!user.validPassword(password))
                    return done(null, false, req.flash('loginMessage', 'Oops! senha errada.')); // create the loginMessage and save it to session as flashdata

                // all is well, return successful user
                return done(null, user);
            });

        }));

};

Here is the code for the sign-up and login routes (it's the passport.authenticate function of the problem I think):

module.exports = function(app, passport) {

// SIGNUP ==============================
// show the signup form
app.get('/signup', function(req, res, next) {
    passport.authenticate('local-login', function(err, user, info) {
        if (err) { return next(err); }
        //if there is no user in the response send the info back to modal
        if (!user) {
            return res.send(info);
        }
        //user was able to login, send true and redirect
        req.logIn(user, function(err) {
            if (err) { return next(err); }
            return res.send({ valid: true });
        });
    })(req, res, next);
});

// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
    successRedirect: '/', // redirect to the secure profile section
    failureRedirect: '/signup', // redirect back to the signup page if there is an error
    failureFlash: true // allow flash messages
}));
}

Here are the dependencies of the software:

"name": "pe",
"version": "0.0.0",
"private": true,
"scripts": {
    "start": "node ./bin/www"
},
"dependencies": {
    "body-parser": "~1.17.1",
    "cookie-parser": "~1.4.3",
    "debug": "~2.6.3",
    "ejs": "~2.5.6",
    "express": "~4.15.2",
    "express-session": "~1.15.3",
    "morgan": "~1.8.1",
    "serve-favicon": "~2.4.2",
    "passport-twitter": "~1.0.4",
    "passport-google-oauth": "~1.0.0",
    "bcrypt-nodejs": "~0.0.3",
    "mongoose": "~4.11.1",
    "passport": "~0.3.2",
    "passport-facebook": "~2.1.1",
    "passport-local": "~1.0.0",
    "connect-flash": "~0.1.1"
}

}

    
asked by anonymous 13.07.2017 / 21:11

0 answers