Sign In Register

How can we help you today?

Start a new topic

Login with email and/or name


We would like for our users to be able to login with either their registered email (user name) or their name (display name).

For that I wanted to use the following code:


//var l_UserName = Spark.getData().userName;   // <-- does not work
var l_UserName = JSON.parse(JSON.stringify( Spark.getData())).userName;   // <-- works but super hacky

var l_PlayerCollection = Spark.systemCollection("player");

var l_Result = l_PlayerCollection.findOne({"displayName":l_UserName}, {}, {"userName": 1});

if (l_Result !== null)
    Spark.getLog().info(l_UserName+"  ->  "+l_Result.userName)
    Spark.getData().userName = l_Result.userName;     // <-- does not work at all


Their, I got two problems / potential bugs:

1. Getting the user name is quite tricky. Spark.getData())).userName only returns null. But logging the whole data gives you a normal JSON. Only if I parse it to string and back, I get the name out correctly. But hey, at least it's working..

2. But overwriting the name with the email does not work at all. How can i change the user name before actually doing the authentication?

Hey Fiete,

Firstly, presuming the code above is in the AuthenticationRequest script, you can get the userName with -> Spark.getData().userName.
You can test this out using the test-harness by just getting the script to return the user-name.

Spark.setScriptData('userName', Spark.getData().userName);

If this doesnt work for you then please let me know so we can investigate that further.

There is not way to change the field which AuthenticationRequest uses. It will always check the userName field against the userName on the player doc.
So even if you put some code into the AuthenticationRequest script, the authentication will take place with the userName submittted and there is no way to change that.

Sorry i couldnt be more helpful on this issue.



Hi Sean,

thank you for your fast answer. Yes, the code was from the AuthenticationRequest.

1. Like I mentioned, my first try was with Spark.getData().userName, but yesterday it returned nothing. Only the hack over the JSON parsing worked for me.Today it seams to work again. Maybe a similar bug like with the cache before, someday it works, some day not...
2. That's sad to hear. Is there a way for the user to login with his or her user name and/or display name?


Hi Fiete,

I can see you've sent some AuthenticationRequests with no userName field included at all, this would lead to Spark.getData().userName returning null, I've never encountered this returning null in any other case in the AuthenticationRequest myself. There is no way to login with either an email or userName, just one. You can only authenticate with what is in the userName field, this could be a specific userName or an email address in the userName field.



 Hi Liam,

thanks allot for your fast answer. Sean mailed me as well. Don't know how the missing userName fields happened, but it was probably my fault.

Thanks allot and best regards

Login to post a comment