Hey, all I want is to get user's profile data during authentication.
Right now I use this code before authentication.
Everythings starts from SignUp(string) when a user opens the game for the first time and press LOG IN button, and then it starts from Authenticate() since IsRegistered becomes true after successful registration.
void Start()
{
tempUserName = SystemInfo.deviceUniqueIdentifier;
tempUserPassword = tempUserName.Substring(tempUserName.Length - 8, 8);
tempUserEmail = tempUserName.Substring(0, 8) + "@email.com";
serviceAPI = new ServiceAPI("", "");
scoreboardService = serviceAPI.BuildScoreBoardService();
userService = serviceAPI.BuildUserService();
socialService = serviceAPI.BuildSocialService();
storageService = serviceAPI.BuildStorageService();
if(IsRegistered)
{
Authenticate();
}
else
{
NicknameManager.ShowNicknameField();
}
}
private void Authenticate()
{
if(!IsLinkedToFB)
{
var metaHeaders = new Dictionary<string, string>();
metaHeaders.Add("userProfile", "true");
userService.SetOtherMetaHeaders(metaHeaders);
App42API.SetDbName(DB_NAME);
var query = QueryBuilder.Build("_$owner.owner", UserName, Operator.EQUALS);
userService.SetQuery(COLLECTION_NAME, query);
userService.Authenticate(UserName, tempUserPassword, new App42MonoCallback<User>(
(error)=> {
Debug.LogError("Couldn't authenticate");
isRegistered = false;
NicknameManager.ShowNicknameField();
},
OnAuthenticate
));
}
}
private void OnAuthenticate(User user)
{
this.user = user;
var jsonDocList = user.GetJsonDocList();
if(jsonDocList.Count>0)
{
var json = jsonDocList[0].GetJsonDoc();
var jobj = SimpleJSON.JSON.Parse(json);
var dict = jobj.AsObject.m_Dict;
if (dict.ContainsKey("firstName"))
{
Nickname = dict["firstName"];
Debug.Log("Authentication has been completed successfully!");
NicknameManager.ShowGreetings();
}
}
else
{
Debug.LogError("jsonDocList is empty");
}
}
public void SignUp(string nickname)
{
var profile = new User.Profile();
profile.SetFirstName(nickname);
userService.CreateUserWithProfile(UserName, tempUserPassword, tempUserEmail, profile, new App42MonoCallback<User>(
OnSignUpFailed,
OnSignUp
));
}
private void OnSignUpFailed(App42Exception e)
{
var appErrorCode = e.GetAppErrorCode();
switch(appErrorCode)
{
case 2001: //username already exists
IsRegistered = true;
Authenticate();
break;
}
}
private void OnSignUp(User userWithProfile)
{
this.user = userWithProfile;
var profile = userWithProfile.GetProfile();
Nickname = profile.GetFirstName();
IsRegistered = true;
SaveProfileToStorage();
}
private void SaveProfileToStorage()
{
if (string.IsNullOrEmpty(FirstNameDocumentID))
{
App42API.SetLoggedInUser(UserName);
var json = new Dictionary<string, object>();
json.Add("firstName", Nickname);
storageService.InsertJSONDocument(DB_NAME, COLLECTION_NAME, json, new App42MonoCallback<Storage>(
(error) => { Debug.LogError("An error has occured during saving data to the storage"); },
OnDataSaved
));
}
}
private void OnDataSaved(Storage storage)
{
var docID = storage.GetJsonDocList()[0].GetDocId();
Debug.Log("doc id: " + docID);
FirstNameDocumentID = docID;
NicknameManager.ShowGreetings();
}
}
public class App42MonoCallback<T> : App42CallBack
{
private Action<App42Exception> _onError;
private Action<T> _onSuccess;
public App42MonoCallback(Action<App42Exception> onError = null, Action<T> onSuccess = null)
{
_onError = onError;
_onSuccess = onSuccess;
}
public void OnException(Exception ex)
{
App42Exception e = (App42Exception)ex;
Debug.Log("Exception: " + ex.Message+"\n code: "+e.GetAppErrorCode()+" http code: "+e.GetHttpErrorCode());
if (_onError != null)
_onError(e);
}
public void OnSuccess(object response)
{
T user = (T)response;
if (_onSuccess != null)
_onSuccess(user);
Debug.Log("response: " + response);
}
}
But to make this work, at first I have to save the data in some database that is in Storage section.
But when I create a user for the first time I already save profile data together with username, password and email, why I have to copy this data to the storage?
If I somehow lose user's record from User section, and register a new user, with new profile data, then I will have two records in the Storage section. And even if I check whether there is a profile data in the Storage section and will not save the data again, I will get different profile data after authentication, because profile data from the Storage and profile data from the User section may be different.