parse-community / Parse-SDK-dotNET

Parse SDK for .NET, Xamarin, Unity.
http://parseplatform.org
Apache License 2.0
322 stars 260 forks source link

ParseClient.Instance.GetCurrentUser() returns null when closing and restarting the app #366

Open tssma opened 1 year ago

tssma commented 1 year ago

New Issue Checklist

Hello and first of all thank you for your outstanding work!

I'm building a cross-platform app for Android and iOS using Xamarin.Forms.

The issue I'm running into is that ParseClient.Instance.GetCurrentUser() returns null if I close and restart my app.

I initialize the ParseClient in the following way:

public partial class App : Application
{

    public App()
    {

        MetadataMutator metadataMutator = new MetadataMutator
        {
            EnvironmentData = new EnvironmentData { OSVersion = Environment.OSVersion.ToString(), Platform = Device.RuntimePlatform, TimeZone = TimeZoneInfo.Local.StandardName },
            HostManifestData = new HostManifestData
            {
                Version = this.GetType().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion,
                Name = this.GetType().Assembly.GetName().Name,
                ShortVersion = this.GetType().Assembly.GetName().Version.ToString(),
                Identifier = AppDomain.CurrentDomain.FriendlyName
            }
        };

        ParseClient parseClient = new ParseClient(new ServerConnectionData
        {
            ApplicationID = Constants.ApplicationId,
            Key = Constants.Key,
            ServerURI = Constants.ServerUri
        },
        new LateInitializedMutableServiceHub { MetadataController = metadataMutator },
        metadataMutator);

        parseClient.Publicize();

        Console.WriteLine(String.Format("Is currentUser null BEFORE hardcoded login? {0}", ParseClient.Instance.GetCurrentUser() == null));

        if (ParseClient.Instance.GetCurrentUser() == null)
        {
            ParseClient.Instance
                .LogInAsync(username: "Test", password: "password")
                .Wait();
        }

        Console.WriteLine(String.Format("Is currentUser null AFTER hardcoded login? {0}", ParseClient.Instance.GetCurrentUser() == null));

    }

}

When I start the app for the first time, the outputs are:

Is currentUser null BEFORE hardcoded login? True Is currentUser null AFTER hardcoded login? False

And when I close the app and restart it the outputs are again:

Is currentUser null BEFORE hardcoded login? True Is currentUser null AFTER hardcoded login? False

Expected behaviour would be that the logged in user stays logged in when the app is closed and restarted. So the following output would be expected when the app is started for the second time:

Is currentUser null BEFORE hardcoded login? False Is currentUser null AFTER hardcoded login? False

In my actual App, the user credentials are not hardcoded - Logging in is done on a Login Page where users enter their username and password. Users having to freshly log in every time they start the app would be fatal.

Please help me with this issue, I tried all initialization variants of ParseClient I could find and days of search did not bring me any further either. If this kind of persistence can not be provided, this inconvenience for end users would make parse-dotNET unusable for probably not only my app but every Xamarin.Forms app in which users can sign up and log in.

The probably only relevant NuGet Packages I use are: NETStandard.Library 2.0.3 Parse 2.0.0-develop-1 Xamarin.Forms 5.0.0.2515

I connect to Parse on back4app.com with Parse Version 4.5.0. ServerUri = "https://parseapi.back4app.com/";