I've got an error with my code, with a passport function.
I followed this tutorial: Sample tutorial followed
And I get an error from /routes file in nodemon server.js:
TypeError: req.isAuthentiated is not a function
And this error in the browser window when I attempt to login (I can sign up, and the db is storing users):
TypeError: req.isAuthentiated is not a function at Object.isLoggedIn [as handle]
This is a passport function and I'm confused on how to remedy this.
server.js:
var express = require('express');
var app = express();
var port = process.env.PORT || 8080;
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash'); // messages stored in session
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var configDB = require('./config/database.js');
// configuration
mongoose.connect(configDB.url); // connect to our database
require('./config/passport')(passport); // pass passport for configuration
// set up our express application
app.use(morgan('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser.json()); // get information from html forms
app.use(bodyParser.urlencoded({ extended : true})); // for express 4
app.set('view engine', 'ejs'); // set up ejs for templating
// required for passport
app.use(session({ secret: 'ilovescotchscotchyscotchscotch' })); // 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
// routes
require('./models/routes.js')(app, passport);
// load our routes and pass in our app and fully configured passport
// launch
app.listen(port);
console.log('The magic happens on port ' + port);
routes.js
var flash = require('connect-flash');
var express = require('express');
var router = express.Router();
//module.exports = function (app, passport){
module.exports = function (app, passport){
// get homepage
app.get('/', function(req, res){
res.render('index.ejs');
});
// show login form
app.get('/login', function(req, res){
// render page and pass in any flash data i(f there is any)
res.render('login.ejs', {message: req.flash('loginMessage') });
});
// process login form
app.get('/signup', function(req, res) {
//render page, pass in any flash data (if there is any)
res.render('signup.ejs', {message: req.flash('signupMessage') });
});
// process signup form
// app.post('/signup', do all passport stuff here);
// PROFILE SECTION
// route to middleware to verify this (w/ isLoggedIn fn)
app.get('/profile', isLoggedIn, function(req, res) {
res.render('profile.ejs', {
user: req.user //get user out of session pass to template
});
});
// LOG OUT
app.get('/logout', function(req, res) {
req.logout(); // provided by passport
res.redirect('/');
});
// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
// process the login form
app.post('/login', passport.authenticate('local-login', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/login', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
return router;
};
// route to middleware to make sure user is logged in
function isLoggedIn(req, res, next) {
// if user is logged in -
if (req.isAuthentiated())
return next();
// if they aren't redirect them to home
res.redirect('/');
}
function isLoggedIn(req, res, next) {
if (req.isAuthenticated()) // <-- typo here
return next();
res.redirect('/');
}