jfromaniello / passport.socketio

access passport.js authenticated user information from socket.io connection
655 stars 81 forks source link

No session found #142

Closed Allihopp closed 5 years ago

Allihopp commented 7 years ago

Greetings, have a little problem here.. After auth session creates in browser and MySQL database. But still appears this fucking error message: 'No session found'.

Whole code: //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// var express = require('express'), app = express(), http = require('https'), http = require('http'), server = http.createServer(app), io = require('socket.io')(server);

// Includes var logger = require('winston'), hbs = require('express-handlebars'), path = require('path'), cookieParser = require('cookie-parser'), bodyParser = require('body-parser'), passport = require('passport'), passportSocketIo = require('passport.socketio'), passportLocal = require('passport-local').Strategy, expressSession = require('express-session'), MySQLStore = require('connect-caminte')(expressSession);

// Logger logger.remove(logger.transports.Console); logger.add(logger.transports.Console, {colorize: true, timestamp: true}); logger.info('Socket.IO > listening on port ');

// Settings app.use(express.static(path.join(__dirname, 'public')));

// Template engine app.engine('hbs', hbs({ defaultLayout: 'main', extname: '.hbs', layoutsDir: 'views' })); app.set('view engine', 'hbs'); app.set('views', path.resolve(__dirname, 'views'));

// Settings app.use(cookieParser()); app.use(bodyParser.urlencoded({extended: false})); app.use(bodyParser.json());

// Session info var sessionStore = new MySQLStore({ driver: 'mysql', collection: 'sessions', db: { host: '127.0.0.1', port: '3306', database: 'site', username: 'root', password: '' }, maxAge: 300000, // 5 min clear_interval: 60 // 1 min }); app.use(expressSession({ key: 'connect.sid', secret: 'lmao', store: sessionStore, resave: true, saveUninitialized: false })); app.use(passport.initialize()); app.use(passport.session());

// Pages app.use('/', require('./routes/index')); app.use('/users', require('./routes/users'));

passport.use(new passportLocal( function (username, password, done) { const db = require('./models/database'); var bcrypt = require('bcrypt');

    db.query('SELECT `id`, `password` FROM `users` WHERE `username` = ? OR `email` = ?', [username, username], function (err, results, fields) {
        if(err) return done(err);
        if(results.length === 0) return done(null, false);

        for(let i = 0; i < results.length; i++) {
            var hash = results[i].password.toString();
            bcrypt.compare(password, hash, function (err, response) {
                if (response) {
                    return done(null, {user_id: results[i].id});
                } else {
                    return done(null, false);
                }
            });
        }
    });
}

));

io.use(passportSocketIo.authorize({ cookieParser: cookieParser, key: 'connect.sid', secret: 'lmao', passport: passport, store: sessionStore, success: onAuthorizeSuccess, fail: onAuthorizeFail }));

function onAuthorizeSuccess(data, accept) { accept(); console.log('successful connection to socket.io'); }

function onAuthorizeFail(data, message, error, accept) { if(error) accept(new Error(message)); console.log('failed connection to socket.io:', message); }

io.on('connection', function(socket) { console.log('user connected'); });

server.listen(8080);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

What's the goddamn problem?

smachs commented 6 years ago

you try to disabled resave?