1
votes

I am new to passport.js. I have been following the following blog to register user

https://scotch.io/tutorials/easy-node-authentication-setup-and-local

but every time i run the server and the i load the /register page it shows the above error

TypeError: passport.authenticate is not a function at module.exports (/home/punit/Documents/****/routes/index.js:10:34) at Layer.handle [as handle_request] (/home/punit/Documents/****/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/home/punit/Documents/****/node_modules/express/lib/router/index.js:312:13) at /home/punit/Documents/****/node_modules/express/lib/router/index.js:280:7 at Function.process_params (/home/punit/Documents/****/node_modules/express/lib/router/index.js:330:12) at next (/home/punit/Documents/****/node_modules/express/lib/router/index.js:271:10) at /home/punit/Documents/****/node_modules/connect-flash/lib/flash.js:21:5 at Layer.handle [as handle_request] (/home/punit/Documents/****/node_modules/express/lib/router/layer.js:95:5)

i have check the variables are populated in the /routes/index.js file.

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

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

mongoose.connect("localhost:27017/****");

require('./models/strategies')(passport)

var app = express();

session initialization is as follows

app.use(session({
  secret: '*******',
  resave: true,
  saveUninitialized: false
})); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session


app.use('/', routes);
app.use('/users', users);
app.use('/register', routes);

require('./routes/index')(app, passport)

the strategies.js file is as follows

var LocalStrategy = require('passport-local').Strategy
var Profile = require('./profile.js')

var ex = function (passport) {

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

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

  passport.use('register', new LocalStrategy({
    usernameField: 'email',
    passwordField: 'password',
    passReqToCallback: true
  },
  function(req, mobile, password, done) {
    process.nextTick(function() {
      return done(null, false);

    });
  }))
}

console.log(ex)

module.exports = ex;

and the routes/index.js file is as follows

module.exports = function (app, passport) {
  /* GET home page. */
  app.get('/login', function(req, res, next) {
    res.render('index', { title: 'Express' });
  });
  app.get('/register', function(req, res, next) {
    res.render('register', { title: 'Express' });
  });

  app.post('/register', passport.authenticate('register', {
    successRedirect: '/profile',
    faliureRedirect: '/register',
    faliureFlash: true // allows flash message
  }), function(req, res, err) {
    console.log(req)
  })

}

Any kind of help would be appreciated.

2

2 Answers

1
votes

You should either use the router in the routes files or pass the app

Only one will work.

//app.use('/', routes);
//app.use('/users', users);
//app.use('/register', routes);

require('./routes/index')(app, passport)

This way the passport object will be available in the routes file.

0
votes

Might not be the best answer but should work in your case, change the strategies.js file:

var LocalStrategy = require('passport-local').Strategy
var Profile = require('./profile.js')

var ex = function (passport) {
 return (
  passport.serializeUser(function (user, done) {
    done(null, user.id)
  });

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

  passport.use('register', new LocalStrategy({
    usernameField: 'email',
    passwordField: 'password',
    passReqToCallback: true
  },
  function(req, mobile, password, done) {
    process.nextTick(function() {
      return done(null, false);

    });
  }))
 )
}

console.log(ex)

module.exports = ex;