Sign In Register

How can we help you today?

Start a new topic
Answered

Cloud code: RegistrationRequest

I want mu users to register with a unique email. To achieve this I was thinking of adding Cloud Code snippet to RegistrationRequest. In Cloud Code I would like to verify the uniqueness of the email and if the email is already taken return an error. 


Is it possible to make script return custom errors from Cloud Code attached to RegistrationRequest?


Best Answer

Hi Domen,


No you shouldn't use throw here. A better way to handle this would be with a custom scriptError and our new Spark.exit() method. Spark exit will terminate the execution of the current script and the scriptError will display your custom error in the response. For example.


//if the displayName supplied during registration was "test", stop the registration
if(Spark.getData().displayName == "test"){
Spark.setScriptError("error", "displayName = test")
Spark.exit()
} 

  

In your case what you would want to do here is store all user emails in a custom collection, query that collection in the RegistrationRequest and then display the appropriate error if the supplied email address is found, then terminate the execution of the rest of the script with Spark.exit(). Does that make sense ?


Thanks,

Liam


Hi Domen,


You could use our "userName" field on the RegistrationRequest to set a unique email for your users. Usernames that are already taken will generate an error in the RegistrationResponse if already taken. A user won't be registered in these cases. Does that sound like it would work for you ? You could implement this manually but it would take some work. If you have any further questions just let me know.


Thanks,

Liam

Hey


yes your solution would get the job done but IMHO is a bit messy one. I have decided to put the email into scriptData of the player object so I can access it. Added a scriptData.email mongo index and created a code snippet in RegistrationRequest Cloud Code to check for uniqueness of the email, and throwing an error with  

throw "email already taken";

 when the email is not unique. This also gets the job done. 


Thank you for the suggestion thought :)


Domen

Answer

Hi Domen,


No you shouldn't use throw here. A better way to handle this would be with a custom scriptError and our new Spark.exit() method. Spark exit will terminate the execution of the current script and the scriptError will display your custom error in the response. For example.


//if the displayName supplied during registration was "test", stop the registration
if(Spark.getData().displayName == "test"){
Spark.setScriptError("error", "displayName = test")
Spark.exit()
} 

  

In your case what you would want to do here is store all user emails in a custom collection, query that collection in the RegistrationRequest and then display the appropriate error if the supplied email address is found, then terminate the execution of the rest of the script with Spark.exit(). Does that make sense ?


Thanks,

Liam

Yes that is perfect. I see that it is also possible to nest objects in the script error which is exactly what I need for a bit more complex form validation response. 


Thank you very much!

Login to post a comment