Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I have been stuck on various issues while following a guide on how to setup a shopping cart in nodeJS/Express/MongoDb. The guide is a few years old , and I have been trying to find help in the comments with no luck. My issue is that when trying to start the server , I get an error saying : TypeError : passport.authenticate is not a function.

it is refering to my index.js file which looks like this :

var express = require('express');

var router = express.Router();

var Product = require('D:/Other/Projects/Code/Powershell/shopping-cart/models/product');

var csrf = require('csurf');

var passport = require('D:/Other/Projects/Code/Powershell/shopping-cart/config/passport.js');

var csrfProtection = csrf();

router.use(csrfProtection);

/* GET home page. */

router.get('/', function(req, res, next) {

  Product.find(function(err, docs) {

    var productChunks = [];

    var chunkSize = 3;

    for (var i = 0; i < docs.length; i += chunkSize) {

           productChunks.push(docs.slice(i, i + chunkSize));

    }

    res.render('shop/index', { title: 'Shopping Cart', products: productChunks });

  });

});

router.get('/user/signup', function (req, res, next) {

   res.render('user/signup', {csrfToken: req.csrfToken()});

});

router.post('/user/signup', passport.authenticate('local.signup'), {

  successRedirect: '/user/profile',

  failureRedirect: '/user/signup',

  failureFlash: true

});

router.get('/user/profile', function(req, res, next) {

  res.render('user/profile');

});

module.exports = router;


And this is how my app.js file is looking :

var createError = require('http-errors');

var express = require('express');

var path = require('path');

var cookieParser = require('cookie-parser');

var logger = require('morgan');

var routes = require('./routes/index')(passport);

var indexRouter = require('./routes/index');

var usersRouter = require('./routes/users');

var expressHbs = require('express-handlebars');

var mongoose =require('mongoose');

var app = express();

var session = require('express-session');

var passport = require('passport');

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

const bodyParser = require('body-parser');

mongoose.connect('mongodb://localhost:27017/shopping', {useNewUrlParser: true, useUnifiedTopology: true});

mongoose.connection.on('error', err => {

  throw 'failed to connect to MongoDB';

});

require('passport');

// view engine setup

app.engine('.hbs', expressHbs({defaultLayout: 'layout', extname:'.hbs'}))

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

app.use(bodyParser.urlencoded({extended: false}));

app.use(bodyParser.json());

app.use(logger('dev'));

app.use(express.json());

app.use(express.urlencoded({ extended: false }));

app.use(cookieParser());

app.use(session({secret: 'mysupersecret', resave: false, saveUninitialized: false}))

app.use(flash());

app.use(passport.initialize());

app.use(passport.session());

app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);

app.use('/users', usersRouter);

// catch 404 and forward to error handler

app.use(function(req, res, next) {

  next(createError(404));

});

// error handler

app.use(function(err, req, res, next) {

  // set locals, only providing error in development

  res.locals.message = err.message;

  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page

  res.status(err.status || 500);

  res.render('error');

});

module.exports = app;



Any hints on what could be the issue? all help is appreciated, thanks!

What I have tried:

Adding
var routes = require('./routes/index')(passport);


I read on some posts that this fixed the issue but it did not fix it in my case.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900