Is there a way to not use Storage to get profile data during authentication?

0 votes

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.

 

asked Mar 23, 2017 in Unity by xerolp933 (22 points)
edited Mar 24, 2017 by xerolp933

1 Answer

0 votes
Hi Xero,

 

Could you please share the code which you are using to save the user profile in Storage service? Please share with the complete flow and the code snippet at support@shephertz.com. It will help us to provide better support to you.

 

Regards,

Himanshu Sharma
answered Mar 24, 2017 by hs00105 (2,005 points)
i've just updated the post, check it out
Hi Xerolp,

As per our understanding, you don't require to save user data in Storage service. You can directly call userService to save user first name and last name in User Profile to get the data at the time of authenticating the user.  

Now consider, you are required to save user record in Storage service due to your app logic. In that case first, you have to fetch user entry from Storage service. If the user entry exists in Storage service then update that record based on new inputs using addOrUpdateKeys/updateDocumentByDocId (http://api.shephertz.com/app42-docs/nosql-storage-service/) method of Storage service and after that create the new user.

In case you face any issue while implementing the above thing, share your project source code, will check and provide better support to you.  

Regards,
Himanshu Sharma
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...