public async void Login(string email, string password) { if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) { await App.AppCurrent.MainPage.DisplayAlert(AppResource.alertInvalidFields, AppResource.alertFillEmailPassword, AppResource.textOk); return; } Acr.UserDialogs.UserDialogs.Instance.ShowLoading(AppResource.alertLoading); ApiService service = new ApiService(); try { LoginVm loginVm = new LoginVm(); loginVm.Email = email.Trim(); loginVm.Password = password; string deviceType = ""; switch (Device.OS) { case TargetPlatform.Other: deviceType = "Other"; break; case TargetPlatform.iOS: deviceType = "ios"; break; case TargetPlatform.Android: deviceType = "android"; break; case TargetPlatform.WinPhone: deviceType = "WindowsPhone"; break; case TargetPlatform.Windows: deviceType = "Windows"; break; default: deviceType = "None"; break; } loginVm.PushId = App.AppDeviceId; loginVm.PushToken = App.AppDeviceToken; loginVm.Latitude = App.AppCurrent.Latitude; loginVm.Longitude = App.AppCurrent.Longitude; decimal offset = (decimal)DateTimeOffset.Now.Offset.TotalHours; var userJson = JsonConvert.SerializeObject(loginVm); var resultToken = await service.Auth(loginVm.Email, loginVm.Password); var token = JsonConvert.DeserializeObject <AuthAcessToken>(resultToken); var result = await service.GetMe(loginVm.PushId, loginVm.PushToken, loginVm.Latitude, loginVm.Longitude, token.AcessToken); var user = JsonConvert.DeserializeObject <User>(result); App.AppCurrent.StoreUser(user.Id, user.Name, user.Photo, user.UserRole, user.CompanyId, false, offset, user.DesirableLanguage, token.AcessToken); App.AppCurrent.FirstLogin = true; if (Helpers.Settings.DisplayUserRole == Enums.UserRole.Manager.ToString()) { var resultCompanies = await service.GetAsync("company/getcompaniesbymanagerid?id=" + Helpers.Settings.DisplayUserId); var list = JsonConvert.DeserializeObject <List <Company> >(resultCompanies); if (list.Count >= 1) { Helpers.Settings.DisplayHasLocation = list.FirstOrDefault().HasLocation; App.AppCurrent.CompanyCulture = list.FirstOrDefault().CurrencyType.ToCultureInfo(); } await App.AppCurrent.ConfigureAppPhase(); } else if (Helpers.Settings.DisplayUserRole == Enums.UserRole.Employee.ToString()) { if (user.Company != null && user.Company.HasLocation) { Helpers.Settings.DisplayHasLocation = true; } App.AppCurrent.CompanyCulture = user.Company.CurrencyType.ToCultureInfo(); await App.AppCurrent.ConfigureAppPhase(); } else { await App.AppCurrent.ConfigureAppPhase(); } } catch (Exception ex) { await DisplayAlert(AppResource.alertAlert, ex.Message, AppResource.textOk); } finally { Acr.UserDialogs.UserDialogs.Instance.HideLoading(); } }
private async void OnVisitanteTapped(object sender, EventArgs e) { Acr.UserDialogs.UserDialogs.Instance.ShowLoading(AppResource.alertLoging); ApiService service = new ApiService(); try { LoginVm loginVm = new LoginVm(); loginVm.Email = "guest"; loginVm.Password = "******"; string deviceType = ""; switch (Device.OS) { case TargetPlatform.Other: deviceType = "Other"; break; case TargetPlatform.iOS: deviceType = "ios"; break; case TargetPlatform.Android: deviceType = "android"; break; case TargetPlatform.WinPhone: deviceType = "WindowsPhone"; break; case TargetPlatform.Windows: deviceType = "Windows"; break; default: deviceType = "None"; break; } loginVm.PushId = App.AppDeviceId; loginVm.PushToken = App.AppDeviceToken; loginVm.Latitude = App.AppCurrent.Latitude; loginVm.Longitude = App.AppCurrent.Longitude; decimal offset = (decimal)DateTimeOffset.Now.Offset.TotalHours; var userJson = JsonConvert.SerializeObject(loginVm); //var result = await service.PostAsync(userJson, "user/login"); var resultToken = await service.Auth(loginVm.Email, loginVm.Password); var token = JsonConvert.DeserializeObject <AuthAcessToken>(resultToken); var result = await service.GetMe(loginVm.PushId, loginVm.PushToken, loginVm.Latitude, loginVm.Longitude, token.AcessToken); var user = JsonConvert.DeserializeObject <User>(result); //App.AppCurrent.StoreUser(user.Id, user.Name, user.Photo, user.UserRole, user.CompanyId); App.AppCurrent.StoreUser(user.Id, user.Name, user.Photo, user.UserRole, user.CompanyId, false, offset, user.DesirableLanguage, token.AcessToken); await App.AppCurrent.ConfigureAppPhase(); } catch (Exception ex) { this.DisplayAlert(MocoApp.Resources.AppResource.alertAlert, ex.Message, AppResource.textOk); } finally { Acr.UserDialogs.UserDialogs.Instance.HideLoading(); } }