facundoolano/socketio-auth

Issue authenticating when using socket.io swift client

kalmicka opened this issue · 3 comments

Hi there I am having an issue when trying to authenticate when using the socket.io swift client. Here is my app.js server code:

require('socketio-auth')(io, {
      authenticate: socketAuth.authenticate, 
      postAuthenticate: socketAuth.postAuthenticate,
      timeout: 10000
});

the class that is providing the authenticate method

exports.authenticate = function authenticate(data, callback) {
  var username = data.username;
  var password = data.password;

  console.log("authenticate");
  console.log(data.username);
  console.log(data.password);
  console.log(data);
};

Now when I am trying to connect through the swift client I get stuck at the following problem:

socket.on("connect") {[weak self] data, ack in
            var dict: AnyObject? = data?[0]

            let dictionary = ["username":"example@email.com","password":"secret"]
            let data = NSJSONSerialization.dataWithJSONObject(dictionary, options: nil, error: nil)
            let string = NSString(data: data!, encoding: NSUTF8StringEncoding) //creates a JSON

            self!.socket.emit("authentication", string!) 
            //JSON being send looks like this {"username":"example@email.com","password":"secret"} -> This doesn't trigger the authenticate method on the server

           self!.socket.emit("authentication", "{username: example@email.com, password: secret}") 
           // object is not a valid JSON and looks like this {username: example@email.com, password: secret} -> this triggers the authenticate method but the data object on the server doesn't recognize it as JSON
//this is the console output on the server:
//Log: authenticate 
//Log: undefined 
//Log: undefined 
//Log: {username: example@emial.com, password: secret}
        }

If I produce a valid JSON object in swift then it doesn't get recognized on the server and doesn't trigger the authenticate method. If I don't provide a valid JSON then it does trigger the authenticate method but doesn't allow me to treat data as a JSON object. Is there some solution I am not thinking of?
Thanks for the help

I'm not familiar with swift, but it looks like in both cases you're sending a string to the server. You need to find out what the swift client expects in order to send an actual JSON object to the server. According to this it looks like you need to pass a dictionary.

I'm closing this as it doesn't seem directly related to this module.

Hey there,

Thanks for the quick response. Helped me a lot and when using nsdictionary it actually worked. Thanks for the help :)

Bye Michael

On 05.07.2015, at 15:49, Facundo Olano notifications@github.com wrote:

I'm closing this as it doesn't seem directly related to this module.


Reply to this email directly or view it on GitHub.