W
W
Wasya UK2016-12-27 18:47:35
JavaScript
Wasya UK, 2016-12-27 18:47:35

How to get sessionID (sid) correctly?

Please tell me how to work with sessions in NODEJS correctly, and why sid is undefined?

var async = require('async');
var cookie = require('cookie');
var cookieParser = require('cookie-parser');

var log = require('../libs/log')(module); // winston 
var config = require('../config/index');
var HttpError = require('../errors/index').HttpError;
var sessionStore = require('../libs/sessionStore');
var User = require('../models/user').User;

function loadSession(sid, callback) {
    sessionStore.load(sid, function(err, session) {
        if (arguments.length == 0) {
            return callback(null, null);
        } else {
            return callback(null, session);
        }
    });
}

function loadUser(session, callback) {
    if (!session.user) {
        log.debug("Session %s is anonimous", session.id);
        return callback(null, null);
    }
    
    log.debug("retrieving user", session.user);
    
    User.findById(session.user, function(err, user) {
        if (err) return callback(err);
        
        if (!user) return callback(null, null);
        
        log.debug("Results: " + user);
        callback(null, user);
    });
}

module.exports = function(server) {
    var io = require('socket.io')(server);
    
    io.use(function(socket, next) { // authorization
        var handshakeData = socket.request;
        
        var secret = config.get('session:secret');
        var sessionKey = config.get('session:key');
        
        async.waterfall([
            function (callback) {
                var parser = cookieParser(secret);
                            
                parser(handshakeData, {}, function (err) {
                    if (err) return callback(err);
                    
                    console.log(socket.handshake.headers.cookie); // OK
                    
                    var sid = handshakeData.signedCookies[sessionKey];
                    
                    console.log(secret); // OK
                    console.log(sessionKey); // OK
                    console.log(sid); // undefined
                    
                    loadSession(sid, callback);
                });
            },
            function(session, callback) {
                
                if (!session) {
                    callback(new HttpError(401, "No session"));
                }
                
                socket.handshake.session = session;
                loadUser(session, callback);
            }, 
            function(user, callback) {
                if (!user) {
                    callback(new HttpError(403, "Anonimous session may not connect"));
                }
                
                socket.handshake.user = user;
                callback(null);
            }
        ], function(err) {
            if (!err) {
                return next(null, true);
            }
            
            if (err instanceof HttpError) {
                return next(null, false);
            }
            
            next(err);
        });
    });
    
    io.on('connection', function(socket) {
       ...    
    }
    return io;
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question