Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
123
rated 0 times [  126] [ 3]  / answers: 1 / hits: 17850  / 8 Years ago, sat, april 23, 2016, 12:00:00

Hello I am new in NodeJs and I have been following this tutorial http://code.tutsplus.com/tutorials/authenticating-nodejs-applications-with-passport--cms-21619 to create a app with authenticating.
I tried to follow all the structre and code from the tutorial (code is on github https://github.com/tutsplus/passport-mongo) but when I open my app in browser
i get error this error




TypeError: passport.authenticate is not a function
at module.exports (C:myApproutesindex.js:24:34)




This is my index.js route file



var express = require('express');
var router = express.Router();
var passport = require('passport');

var isAuthenticated = function (req, res, next) {
// if user is authenticated in the session, call the next() to call the next request handler
// Passport adds this method to request object. A middleware is allowed to add properties to
// request and response objects
if (req.isAuthenticated())
return next();
// if the user is not authenticated then redirect him to the login page
res.redirect('/');
}

module.exports = function(passport){

/* GET login page. */
router.get('/', function(req, res) {
// Display the Login page with any flash message, if any
res.render('index', { message: req.flash('message') });
});

/* Handle Login POST */
router.post('/login', passport.authenticate('login', {
successRedirect: '/home',
failureRedirect: '/',
failureFlash : true
}));

/* GET Registration Page */
router.get('/signup', function(req, res){
res.render('register',{message: req.flash('message')});
});

/* Handle Registration POST */
router.post('/signup', passport.authenticate('signup', {
successRedirect: '/home',
failureRedirect: '/signup',
failureFlash : true
}));

/* GET Home Page */
router.get('/home', isAuthenticated, function(req, res){
res.render('home', { user: req.user });
});

/* Handle Logout */
router.get('/signout', function(req, res) {
req.logout();
res.redirect('/');
});

return router;
}


Probabbly the problem is there, maybe routing was change in some version of express, but I cant figure out what is the problem.
Can you help pme please ?


More From » node.js

 Answers
56

I had same problem. Look at app.js. There must be:



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

[#62429] Thursday, April 21, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jeniferjaliyahf

Total Points: 650
Total Questions: 104
Total Answers: 86

Location: Grenada
Member since Sun, Dec 20, 2020
4 Years ago
jeniferjaliyahf questions
Fri, Feb 11, 22, 00:00, 2 Years ago
Thu, Feb 10, 22, 00:00, 2 Years ago
Thu, Jun 17, 21, 00:00, 3 Years ago
Thu, Mar 25, 21, 00:00, 3 Years ago
Wed, Feb 17, 21, 00:00, 3 Years ago
;