예제 #1
0
        public async void SignIn(string userName, string userPassword)
        {
            if (Connectivity.NetworkAccess != NetworkAccess.Internet)
            {
                StandardMessagesDisplay.NoConnectionToast();
            }
            else
            {
                var jSignInResponse = await Account.SignIn(userName, userPassword);

                if (jSignInResponse.Contains("Error"))
                {
                    StandardMessagesDisplay.CommonToastMessage(jSignInResponse);
                }
                else if (jSignInResponse == "ConnectionError")
                {
                    StandardMessagesDisplay.NoConnectionToast();
                }
                else if (jSignInResponse == "ErrorTracked")
                {
                    StandardMessagesDisplay.ErrorTracked();
                }
                else
                {
                    try
                    {
                        var signInResponse = JsonConvert.DeserializeObject <JObject>(jSignInResponse);

                        if (signInResponse != null)
                        {
                            var localId = signInResponse?.GetValue("localId")?.ToString();
                            Preferences.Set("UserLocalId", localId);
                            Preferences.Set("ProfileEmail", signInResponse?.GetValue("email")?.ToString());

                            string accountInfoJson = await Account.AccountInfoLookup(signInResponse?.GetValue("idToken")?.ToString());

                            var accountInfo = JsonConvert.DeserializeObject <JObject>(accountInfoJson);

                            var claimsJson = accountInfo["users"][0]["customAttributes"]?.ToString();

                            var claims = JsonConvert.DeserializeObject <JObject>(claimsJson);

                            var role = claims.GetValue("Role")?.ToString();
                            Preferences.Set("UserRole", role);

                            if (RememberMe)
                            {
                                await Xamarin.Essentials.SecureStorage.SetAsync("isLogged", "1");
                            }

                            LoadProfile(Preferences.Get("UserLocalId", "").ToString());
                            await LoadCluster();
                            await LoadTeam();
                            await LoadVaccinePeriod();

                            GoToApp();
                        }
                    }
                    catch (Exception ex)
                    {
                        Crashes.TrackError(ex);
                        StandardMessagesDisplay.InputToast(ex.Message);
                    }
                }
            }
        }