private async void BtnLogin_Click(object sender, EventArgs e) { try { BaseAPIService.Username = txtUsername.Text; BaseAPIService.Password = txtPassword.Text; UserLoginRequest request = new UserLoginRequest() { Username = txtUsername.Text, Password = txtPassword.Text }; Model.User loggedUser = await _apiService.Login(request); if (loggedUser != null) { frmMain frm = new frmMain(); frm.Show(); this.Hide(); } else { MessageBox.Show("User not found", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception ex) { throw ex; } }
async Task Login() { if (Username == null || Username == "" || Password == null || Password == "") { await Application.Current.MainPage.DisplayAlert("Error", "Username and password are required fields!", "OK"); return; } UserLoginRequest user = new UserLoginRequest() { Username = Username, Password = Password }; BaseAPIService.Username = user.Username; BaseAPIService.Password = user.Password; try { Model.User loggedUser = await _apiService.Login(user); if (loggedUser != null) { BaseAPIService.Id = loggedUser.Id; BaseAPIService.Fullname = loggedUser.FirstName + " " + loggedUser.LastName; Application.Current.MainPage = new MainPage(); } else { await Application.Current.MainPage.DisplayAlert("Error", "Invalid username or password", "OK"); return; } } catch (Exception ex) { throw ex; } }