passport.js passport.initialize() middleware not in use
Follow the example to avoid the out-of-order middleware hell that express makes it so easy to enter. Straight from the docs. Note how yours does not match this exactly.
var app = express();
app.use(require('serve-static')(__dirname + '/../../public'));
app.use(require('cookie-parser')());
app.use(require('body-parser').urlencoded({ extended: true }));
app.use(require('express-session')({
secret: 'keyboard cat',
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
Docs
- cookieParser
- session
- passport.initialize
- passport.session
- app.router
You
- passport.initialize
- passport.session
- cookieParser
- session
- app.router
In my case the error was because I was trying to promisify req.login
without binding this
to req
, so when the function was called it could not find passport
settings.
The solution is binding req.login.bind(req)
before passing it to promisify
if you are using Node v8.
In my case (same error message) I've forgotten to add the passport initializations at all:
app.configure(function () {
...
app.use(passport.initialize());
app.use(passport.session());
});
UPDATE: Only working up to express version 3, version 4 does not support app.configure() anymore