1

I'm getting an middleware error. I'm very new to node.js and express (this is my first big project). I have searched SO and google already long and good. The error should be in my order in my app.js. I have tried lots of orders, but I can't find what I'm doing wrong exactly, and especially why? I'm using passport for the Oauth

I hope you guys understand my question, English is not my first language.

Error:

    if (!this._passport) { throw new Error('passport.initialize() middleware n
                                 ^
Error: passport.initialize() middleware not in use
    at IncomingMessage.req.login.req.logIn 

Code, my app.js:

    var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
// var jsdom = require("jsdom"); 
// $ = require("jquery")(jsdom.jsdom().parentWindow()); 

var dbConfig = require('./db');
var mongoose = require('mongoose');
var app = express();
var User = require('./models/user');
//////
var http = require('http').Server(app);
var io = require('socket.io')(http);
var fs = require('fs');
var data = require('./countries.json');


// Configuring Passport
var passport = require('passport')
  , FacebookStrategy = require('passport-facebook').Strategy;

http.listen(3000);

// Connect to DB
mongoose.connect(dbConfig.url);

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.get('/auth/facebook', passport.authenticate('facebook'));


app.get('/auth/facebook/callback', passport.authenticate('facebook', {failureRedirect: '/'}), function(req, res) {
    console.log('Facebook');
    res.redirect('/home');
});

var expressSession = require('express-session');
// TODO - Why Do we need this key ?
app.use(expressSession({secret: 'mySecretKey'}));
app.use(passport.initialize());
app.use(passport.session());

 // Using the flash middleware provided by connect-flash to store messages in session
 // and displaying in templates
var flash = require('connect-flash');
app.use(flash());

// Initialize Passport
var initPassport = require('./passport/init');
initPassport(passport);

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

passport.use(new FacebookStrategy({
    clientID: "xx",
    clientSecret: "xxx",
    callbackURL: "http:localhost:3000/auth/facebook/callback"
    //redirect_uri: "/home"
  },
  function(accessToken, refreshToken, profile, done) {        
   User.findOne({ 'username' :  msg[1] }, function(err, user) {
    //save fb user to db
  }
));
module.exports = app;
  • Nobody is going to sift through your code and fix it for you. Please identify a specific problem and only post code related to it. And for the love of god, modularize. – Ben Fortune Jan 07 '15 at 16:12
  • Yes indeed, I'm sorry that was way to match code. I removed lots of it, only things that are important are still there. My problem is offcourse the error, which tells that my order is not correct. But I have tried lots of different orders but until this point haven't found the problem. – Simon Hanssens Jan 07 '15 at 17:42
  • You shouldn't show clientId and clientSecret in codes here. Use xxxxxx insetead. – NeiL Jan 07 '15 at 17:52
  • Have you read this http://stackoverflow.com/questions/16781294/passport-js-passport-initialize-middleware-not-in-use – NeiL Jan 07 '15 at 17:58
  • Yes I have, a few times. This is looking very stupid of me, and it probably is, but when I tried it (tried a couple of times), it didn't seem to work. – Simon Hanssens Jan 07 '15 at 18:03
  • I need to apologise, I made a stupid mistake and I think I will be capable of fixing it myself now. I want to thank you guys for helping me, I confused expressSession with my passport sessions. I still have a lot to learn – Simon Hanssens Jan 07 '15 at 18:11

0 Answers0