/// <summary> /// Authenticates the user with the GameSparks API. It first attempts to register the user, if that username already exist then it tries to authenticate them. /// This function will also prevent multiple users from logging in with the same name **EVENTUALLY** /// </summary> /// <param name="_userName">The username passed in through the username input field</param> /// <param name="_password">This password is an empty string because we don't require passwords, however the GameSparks API does</param> /// <param name="_registrationCallback">This is the callback containing the registration information from the GameSparks API</param> /// <param name="_authenticationCallback">This is the callback containing the authentication information from the GameSparks API</param> public void AuthenticateUser(string _userName, string _password, RegCallback _registrationCallback, AuthCallback _authenticationCallback) { //Give username and password (which is an empty string because we don't require a password) create a registration request new RegistrationRequest() .SetDisplayName(_userName) .SetUserName(_userName) .SetPassword(_password) .Send((_registrationResponse) => { if (!_registrationResponse.HasErrors) //If the registration response did not have any errors in it, then we can register the player { _registrationCallback(_registrationResponse); } else //If there was some error { if (!(bool)_registrationResponse.NewPlayer) //Check to see if the error was that this player is not a new player - if so need to authenticate { Debug.LogWarning("GSM| Existing User, Switching to Authentication"); //Given username and password (remember, password is just an empty string in our scenario) create a authentication request new AuthenticationRequest() .SetUserName(_userName) .SetPassword(_password) .Send((_authenticationResponse) => { if (!_authenticationResponse.HasErrors) //Authentication was successful { _authenticationCallback(_authenticationResponse); //Set player ID to true if it is this player, or add ID if we don't have it already if (m_PlayerIds.TryGetValue(_authenticationResponse.UserId, out bool me)) { if (!me) { m_PlayerIds[_authenticationResponse.UserId] = true; } } else { m_PlayerIds.Add(_authenticationResponse.UserId, true); } } else //Authentication was not successful - reason is not known. { Debug.LogWarning("GSM| Error Authenticating User\n" + _authenticationResponse.Errors.JSON); } }); } else //Player is a new player, but authentication/registration was unsuccessful { Debug.LogWarning("GSM| Error Authenticating User\n" + _registrationResponse.Errors.JSON); } } }); }
/// <summary> /// Sends an authentication request or registration request to GS. /// </summary> /// <param name="_callback1">Auth-Response</param> /// <param name="_callback2">Registration-Response</param> public void AuthenticateUser(string _userName, string _password, RegCallback _regcallback, AuthCallback _authcallback) { Debug.Log("AuthenticateUser " + _userName + " " + _password); new GameSparks.Api.Requests.RegistrationRequest() // this login method first attempts a registration // // if the player is not new, we will be able to tell as the registrationResponse has a bool 'NewPlayer' which we can check // for this example we use the user-name was the display name also // .SetDisplayName(_userName) .SetUserName(_userName) .SetPassword(_password) .Send((regResp) => { if (!regResp.HasErrors) // if we get the response back with no errors then the registration was successful { Debug.Log("GSM| Registration Successful..."); _regcallback(regResp); } else { // if we receive errors in the response, then the first thing we check is if the player is new or not if (!(bool)regResp.NewPlayer) // player already registered, lets authenticate instead { Debug.LogWarning("GSM| Existing User, Switching to Authentication"); new GameSparks.Api.Requests.AuthenticationRequest() .SetUserName(_userName) .SetPassword(_password) .Send((authResp) => { if (!authResp.HasErrors) { Debug.Log("Authentication Successful..."); _authcallback(authResp); } else { Debug.LogWarning("GSM| Error Authenticating User \n" + authResp.Errors.JSON); } }); } else { // if there is another error, then the registration must have failed Debug.LogWarning("GSM| Error Authenticating User \n" + regResp.Errors.JSON); } } }); }
public void AuthenticateUser(string _userName, string _password, RegCallback _regcallback, AuthCallback _authcallback) { new GameSparks.Api.Requests.RegistrationRequest() .SetDisplayName(_userName) .SetUserName(_userName) .SetPassword(_password) .Send((regResp) => { if (!regResp.HasErrors) { Debug.Log("GSM| Registration Successful..."); _regcallback(regResp); } else { if (!(bool)regResp.NewPlayer) { Debug.LogWarning("GSM| Existing User, Switching to Authentication"); new GameSparks.Api.Requests.AuthenticationRequest() .SetUserName(_userName) .SetPassword(_password) .Send((authResp) => { if (!authResp.HasErrors) { Debug.Log("Authentication Successful..."); _authcallback(authResp); } else { Debug.LogWarning("GSM| Error Authenticating User \n" + authResp.Errors.JSON); } }); } else { Debug.LogWarning("GSM| Error Authenticating User \n" + regResp.Errors.JSON); } } }); }
/// <summary> /// Authenticates the user with the GameSparks API. It first attempts to register the user, if that username already exist then it tries to authenticate them. /// This function will also prevent multiple users from logging in with the same name **EVENTUALLY** /// </summary> /// <param name="_userName">The username passed in through the username input field</param> /// <param name="_password">This password is an empty string because we don't require passwords, however the GameSparks API does</param> /// <param name="_registrationCallback">This is the callback containing the registration information from the GameSparks API</param> /// <param name="_authenticationCallback">This is the callback containing the authentication information from the GameSparks API</param> public void AuthenticateUser(string _userName, string _password, RegCallback _registrationCallback, AuthCallback _authenticationCallback) { //Give username and password (which is an empty string because we don't require a password) create a registration request new RegistrationRequest() .SetDisplayName(_userName) .SetUserName(_userName) .SetPassword(_password) .Send((_registrationResponse) => { if (!_registrationResponse.HasErrors) //If the registration response did not have any errors in it, then we can register the player { //Check for no errors so we can add our name - prevents async problems by doing it here. Will remove if repeat login AddPlayer(_registrationResponse.UserId); //Temp add player (unless not duplicate login, then this will really add them) _registrationCallback(_registrationResponse); } else //If there was some error { if (!(bool)_registrationResponse.NewPlayer) //Check to see if the error was that this player is not a new player - if so need to authenticate { Debug.Log("GSM| Existing User, Switching to Authentication"); //Given username and password (remember, password is just an empty string in our scenario) create a authentication request new AuthenticationRequest() .SetUserName(_userName) .SetPassword(_password) .Send((_authenticationResponse) => { new LogEventRequest() .SetEventKey("CheckOnlineStatus") .Send((_onlineResponse) => { //Extract short code from the configuration data we got GSData scriptData = _onlineResponse.ScriptData; string onlineStatus = scriptData.JSON.ToString(); if (onlineStatus.Contains("2")) //Someone is already logged in with this account { if (m_PlayerIds.TryGetValue(_authenticationResponse.UserId, out bool me)) //Remove player from our player list - not actually us { m_PlayerIds.Remove(_authenticationResponse.UserId); } _authenticationCallback(_authenticationResponse, false, "User already logged in."); } else //No one is logged in as this user - we can log in { if (!_authenticationResponse.HasErrors) //Authentication was successful { _authenticationCallback(_authenticationResponse, true, string.Empty); //Set player ID to true if it is this player, or add ID if we don't have it already AddPlayer(_authenticationResponse.UserId); } else //Authentication was not successful - reason is not known. { Debug.LogWarning("GSM| Error Authenticating User\n" + _authenticationResponse.Errors.JSON); _authenticationCallback(_authenticationResponse, false, _authenticationResponse.Errors.JSON); } } }); }); } else //Player is a new player, but authentication/registration was unsuccessful { Debug.LogWarning("GSM| Error Authenticating User\n" + _registrationResponse.Errors.JSON); } } }); }