I don't understand why my deserialize method is being called so many times. Here's my server.js file
var express = require('express');
var app = express();
var passport = require('passport');
var bodyParser = require('body-parser');
var session = require('express-session');
var MongoSessionStore = require('connect-mongo/es5')(session);
// Local ENV ======================================================================
var env = require('./config/config.json')[app.get('env')];
// Mongo Database ======================================================================
var mongoInstance = require('./config/db.config.js');
mongoInstance.connection.on('open', function(){
console.log('connection works');
});
// Configuration ======================================================================
app.use(session({
secret: 'secretsarenofun', // Change this to anything else
resave: false,
saveUninitialized: true,
store: new MongoSessionStore({
mongooseConnection: mongoInstance.connection,
autoRemoveInterval: 60
})
}));
require('./config/passport.config.js')(passport, env, mongoInstance);
app.use(passport.initialize());
app.use(passport.session());
// Routes ======================================================================
var auth = require('./routes/auth.route')(app, passport);
var users = require('./routes/users.route')(passport);
// Middleware ======================================================================
app.use(express.static(__dirname + '/public'));
app.use('/users', users);
app.use('/auth', auth);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Launch ======================================================================
var server = app.listen(8000, function(){
console.log('The dagger flies at 8000');
});
var io = require('socket.io').listen(server);
require('./sockets')(io);
And in my passport module I set this
module.exports = function(passport, env, db){
var request = require('request');
var OpenIDStrategy = require('passport-openid').Strategy;
var steamKey = 'test';
var User = require('../models/user.model');
var SteamStrategy = new OpenIDStrategy({
providerURL: 'http://steamcommunity.com/openid',
stateless: true,
returnURL: env.path+'/auth/openid/return',
realm: env.path,
},
function(identifier, done) {
var steamId = identifier.match(/\d+$/)[0];
return done(null, {
identifier: identifier,
steamId: identifier.match(/\d+$/)[0]
});
});
passport.use(SteamStrategy);
passport.serializeUser(function(user, done) {
done(null, user.identifier.match(/\d+$/)[0]);
});
// used to deserialize the user
passport.deserializeUser(function(identifier, done) {
console.log('this calls too many times');
//fetch
done(null, identifier);
});
};
When I just visit the home page after I have auth'd the deserialize function calls like it's supposed to but it's calling up to 8 times.
Why is this? I tried moving the app.use(passport.initialize()); app.use(passport.session()); around with some luck but not completely. Why does this occur?
Move app.use(express.static(__dirname + '/public'))
above app.use(passport.initialize());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With