Esempio n. 1
0
        public async void OnSingIn()
        {
            try
            {
                JObject jRes = await myHttpClient.sendHttpPostAsyncJson(Constants.ApiSignIn, user);

                if (user.email.Length == 0 || user.password.Length == 0)
                {
                    await App.Current.MainPage.DisplayAlert("Empty fields", "You must fill all the fields.", "OK");
                }
                else if (!MyHelper.IsValidEmail(user.email.Trim()))
                {
                    await App.Current.MainPage.DisplayAlert("Invalid email", "Enter a valid email : [email protected]", "OK");
                }
                else if ((bool)jRes["HttpClient error"])
                {
                    await App.Current.MainPage.DisplayAlert("Connection failed", "Connection failed.", "OK");
                }
                else if ((bool)jRes["HttpClient parsing error"])
                {
                }
                else
                {
                    if ((bool)jRes["API error"])
                    {
                        await App.Current.MainPage.DisplayAlert("Connection failed", "Connection failed.", "OK");
                    }
                    else if (!(bool)jRes["LoggedIn"])
                    {
                        await App.Current.MainPage.DisplayAlert("Message", (string)jRes["Message"], "OK");
                    }
                    else if ((bool)jRes["LoggedIn"])
                    {
                        await MyHelper.SaveValueForAppAsync("LoggedIn", "true");

                        await MyHelper.SaveValueForAppAsync("JWT", (string)jRes["JWT"]);

                        await MyHelper.SaveValueForAppAsync("id_user", (string)jRes["id_client"]);

                        JObject user_info = (JObject)jRes["user_info"];
                        await MyHelper.SaveValueForAppAsync("user_info", user_info.ToString());

                        user = user_info.ToObject <Client>();

                        App.client = user;

                        //await App.Current.MainPage.DisplayAlert("Connection successful", (string)jRes["JWT"], "OK");

                        App.Current.MainPage = new AppShell();
                    }
                }
            }
            catch (Exception ex)
            {
                await App.Current.MainPage.DisplayAlert("Connection failed", "Connection failed.", "OK");
            }
        }