0
};
var login = function(name, socket, passcode, callback) { 
    db.get(name + ':name', function(res) {
        db.get(name + ':pin', function(pin) {
            if (name === res) {
                if (passcode === pin) {
                    players[sockets.indexOf(socket)] = name;
                }
            }
        });
    });
};
var register = function(name, socket, passcode, callback) {
    if (name === null) {
        callback('Name null!');
        return;
    }
    if (name === '') {
        callback('Name empty!');
        return;
    }
    db.get(name + ':name', function(data) {
        if (data !== null) {
        callback('This name has been taken!');
    }
    else {
        db.set(name + ':name', name, function() {
            db.set(name + ':pin', passcode, function() {
                players[sockets.indexOf(socket)] = name;
                callback('Logged in as: ' + players[sockets.indexOf(socket)]);
            });
        });
    }
});
};

Why does the login part let me login with any PIN to any user, and the register part not callback?

They are called via

   login(args[1], socket, args[2], function(data) {
        callback(null, data)
    }

db is a redis client.

Full code: github.com/creativemud, file server.js

1 Answer 1

1

You never execute the callback inside login. You need to call callback when you're done.

Also, you seem to lack some error handling. Is db a redis client? In that case, the first parameter to every callback is an error, which is custom in node.js land. I would do something like:

var login = function(name, socket, passcode, callback) { 
    db.get(name + ':name', function(err, res) {
        if(err) return callback(err);

        if(res !== name) return callback(new Error('invalid result'));

        db.get(name + ':pin', function(err, pin) {
            if(err) return callback(err);

            if(pin !== passcode) return callback(new Error('invalid passcode'));

            players[sockets.indexOf(socket)] = name;
            callback(null, name);
        });
    });
};

Might not be very clean, but I hope you catch the drift.

Edit: You don't show how you call these functions. Socket.io has pretty nice support for authentication, you might want to check it out: Authorization and handshaking.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.