Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passport deserialize being called too many times

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?

like image 543
Garuuk Avatar asked Mar 17 '16 06:03

Garuuk


1 Answers

Move app.use(express.static(__dirname + '/public')) above app.use(passport.initialize());

like image 153
romeo111 Avatar answered Oct 20 '22 15:10

romeo111