How to get return value from custom authentication

Options
Dev
Dev
I am currently developing a 3d multiplayer game using unity and photon. I have a login/register system, match making system and character selection system in the process, however I have came across numerous 'issues' that I have decided that using your custom authentication system will fix. From my understanding, unity sends the initial request (username/password) to photon using:
PhotonNetwork.AuthValues = new AuthenticationValues();
PhotonNetwork.AuthValues.AuthType = CustomAuthenticationType.Custom;
PhotonNetwork.AuthValues.AddAuthParameter("Username", username);
PhotonNetwork.AuthValues.AddAuthParameter("Password", password);
From there, in the photon dashboard, I set up the key-value pairs as:

Key: Username - Value: username (i assume this value is what is being sent from unity and validated in our php script?)
Key: Password - Value: password

I have created a IEnumerator function that takes in username and password as parameters from which it sends those as auth parameters. Once this happens it should presumably be sent to our custom server (in this case my custom php script that queries to the database and checks whether or not the account is valid). From my understanding we need to return a JSON object and use that to determine if the login was successful or not. So my question is, how do I check the returned JSON object in unity? This is part of my php script, which one can infer what it's doing.
if ($user === true) 
{ 
			
     $login_info = array
     ( 
          "ResultCode" => 1, 
	   "Message" => "Auth OK", 
     );
			

}
else 
{ 
  
   //For failure return use this variable array 
   $login_info = array
   ( 
	"ResultCode" => 2, 
	"Message" => "Authentication failed.", 
  ); 
			
}  

$json = json_encode($login_info); 
echo $json;
So I guess what I'm asking is, how can I check in my unity script what is being returned? The way i previously did it was sending a WWW form and waiting for a direct response from the php script, but with this, I am not sure how to do that as we're sending the data via auth parameters, not WWW fields. I have been trying to do this for quite some time now, as there isn't much information on it in the documentation. I am just looking for someone to point me in the right direction! Any help is appreciated.

Comments

  • JohnTube
    JohnTube ✭✭✭✭✭
    edited December 2015
    Options
    Hi @Dev,

    I think you're misunderstanding how custom authentication works.

    From client you can send user credentials as POST data or query string parameters. From dashboard you need to configure key/values for parameters that should not be sent from client. Which is completely optional.

    From the response the Photon client automatically sets the local player's UserId from the one returned by Photon server. If you do not return a UserId from your custom authentication provider (your php script) a randomly generated one will be returned by Photon servers (a GUID).
    From the php script you can also return an AuthCookie which is a secret data that is not accessible to the user not only used from Photon server. The third optional returned property is Data which can be a JSON object of anything you want.
    The <a href="http://doc.photonengine.com/en/realtime/current/reference/custom-authentication">documentation</a> was updated recently and contains all the information you need in details. It contains how you get the Data from client but I think this is not completely PUN friendly.
    I think this will be much more easier in the next PUN update after the holidays. My colleague @Tobias will add callback for the authentication where you can easily get the custom auth data.
  • Dev
    Options
    Hello. Thank you for taking the time to read my post. I am a bit confused on what you mean by optional key value pairs? How does photon know what is being sent and how does it know to redirect it to the php script? I guess what i'm asking is how does unity know when the user was actually authenticated? The documentation is somewhat lacking information on how one can actually authenticate a user via custom server. Could you please provide a very basic example of what should happen from both unity and php? If i provide links to my client side login code and server side would you be able to annotate what i'm doing wrong?
  • JohnTube
    JohnTube ✭✭✭✭✭
    edited January 2016
    Options
    Hi @Dev,

    I apologize for taking too long to reply.

    I'm also sorry to hear that the updated Custom Authentication documentation is still confusing for you.

    If I understand your use case correctly, you just need to verify user credentials in a PHP script.

    First I advise you to not store or exchange plain text passwords and make a hash out of them first.

    I don't know if you want to allow anonymous clients or if you want to accept clients when your custom authentication provider is down. If not you should untick the corresponding checkboxes in the settings.

    You don't need any key/value pairs setup for the custom authentication of your application from the Photon dashboard. Those are static values that are sent by Photon server and are invisible to the client. Those key/value parameters are independent from what the client sends.
    I think the fact that you are setting Username and Password keys from both client and server (dashboard) result in a query string with duplicate parameters which may be preventing your PHP script from getting those values . You should not use same key from client and dashboard for the same application.


    Your code seems correct. However, from the PHP script, I recommend adding a Nickname and UserId properties in the returned JSON object. If you do so, those values will be automatically assigned to the local client.

    You don't need to check the returned JSON object from PUN (Unity) UNLESS you need extra custom data from server during authentication. If so then please take a look at the code snippet in the documentation page or wait until the appropriate callback is added to PUN 1.66 as stated here.
    how does unity know when the user was actually authenticated?
    If you get a OnJoinedLobby() or a OnConnectedToMaster() then you're authenticated.
    Otherwise you will get OnCustomAuthenticationFailed (string debugMessage).

    I don't think you need to the whole raw authentication response from Photon server or the exact moment when the client is authenticated.
  • affgoo
    affgoo
    edited August 2017
    Options
    @JohnTube

    I am sending Nickname but it isn't setting photonnetwork.player.nickname although it is setting: photonnetwork.playername

    what is the difference here?
    array("ResultCode" => 1, "Message" => "Success!", "Nickname" => $user);
  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    Hi @affgoo,

    Thank you choosing Photon and for reporting this!
    We managed to reproduce and we will fix this in an upcoming release.