Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fix TypeError: Cannot read property 'authenticate' of undefined passportjs

im working on a node passport login authentication but im stack on this error which makes it hard for me to run my node server

/home/emil/Documents/myapp/app/routes.js:14 app.post('/login', passport.authenticate('local-login', { ^ TypeError: Cannot read property 'authenticate' of undefined

how can i fix it?

my code: server.js

var express = require('express');
var app = express();
var port = 8080;

var cookieParser = require('cookie-parser');
var session = require('express-session');
var morgan = require('morgan');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var passport = require('passport');

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

app.use(passport.initialize());
app.use(passport.session());
app.use(morgan('dev'));
app.use(cookieParser());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(session({
secret: 'anystringoftext',
saveUninitialized: true,
resave: true
}));

app.set('view engine', 'ejs');

//app.use('/', function(req, res){
//  res.send('Our First Express program!');
//  console.log(req.cookies);
//  console.log('===============');
//  console.log(req.session);
//});

require('./app/routes.js')(app);

app.listen(port);
console.log('Server running on port: ' + port);

routes.js:

var User = require('./models/user');

module.exports = function (app, passport) {
app.get('/', function (req, res) {
    res.render('index.ejs');
});

 app.get('/login', function (req, res) {
    res.render('login.ejs', {
        message: req.flash('loginMessage')
    });
});

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

app.get('/signup', function (req, res) {
    res.render('signup.ejs', {
        message: req.flash('signupMessage')
    });
});

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

app.get('/profile', isLoggedIn, function(req, res){
   res.render('profile.ejs', { user: req.user });
});

app.get('/:username/:password', function (req, res) {
    var newUser = new User();
    newUser.local.username = req.params.username;
    newUser.local.password = req.params.password;
    console.log(newUser.local.username + " " + newUser.local.password);
    newUser.save(function (err) {
        if (err)
            console.log(err);
    });
    res.send("Success!");
})
};

function isLoggedIn(req, res, next) {
if(req.isAuthenticated()){
    return next();
}
res.redirect('/login');
}

passport.js

var LocalStrategy = require('passport-local').Strategy;

var User = require('../app/models/user');

module.exports - function(passport) {

passport.serializeUser(function(usser, 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: 'email',
    passwordField: 'password',
    passReqToCallback: true
},
function(req, email, password, done){
    process.nextTick(function(){
        User.findOne({'local.username': email}, function(err, user){
            if(err)
                return done(err);
            if(user){
                return done(null, false, req.flash('signupMessage', 'That account exists'));
            } else {
                var newUser = new User();
                newUser.local.username = email;
                newUser.local.password = password;

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

    });
}));

passport.use('local-login', new LocalStrategy({
    usernameField: 'email',
    passwordField: 'password',
    passReqToCallback: true
},
function(req, email, password, done){
    process.nextTick(function(){
        User.findOne({ 'local.username': email}, function(err, user){
           if(err)
               return done(err);
            if(!user)
                return done(null, false, req.flash('loginMessage', 'No user found'));
            if(user.local.password != password)
                return done(null, false, req.flash('loginMessage', 'invalid password'));
        }
           return done(null, user);  
    })
    })
}
))
like image 312
emilkitua Avatar asked Feb 10 '23 08:02

emilkitua


1 Answers

You have not passed passport to the routes. require('./app/routes.js')(app); should be require('./app/routes.js')(app, passport) in server.js;

like image 169
Sarbbottam Avatar answered Feb 12 '23 20:02

Sarbbottam