public void AddAccount(string payload) { var pieces = payload.Split(','); var newAccount = new Account() { RestApi = pieces[3], Email = pieces[0], ServerName = pieces[2], Token = pieces[1], IsDefault = false }; LocalContext.AddAccount(newAccount); UserAccounts.Add(newAccount); }
public void SaveOrUpdateAccount(Account newAccount) { var existingAccounts = LocalContext.GetAllAccounts(); var newUri = new Uri(newAccount.RestApi); var serverName = GetServerName(newUri); foreach (var acc in existingAccounts) { var eUri = new Uri(acc.RestApi); if ((eUri.Host == newUri.Host) && (acc.Email == newAccount.Email) && (eUri.Port == newUri.Port)) { acc.ServerName = serverName; acc.Token = newAccount.Token; LocalContext.RemoveAccount(acc); // TODO: Add update account method, as this is rather stupid LocalContext.AddAccount(acc); return; } } newAccount.ServerName = serverName; LocalContext.AddAccount(newAccount); }
private void LoginButton_Click(object sender, RoutedEventArgs e) { var errs = ValidateLogin(); if (errs != "") { MessageBox.Show(errs); return; } var myUser = new User() { Email = LoginEmail.Text, Password = LoginPassword.Password, }; var spkClient = new SpeckleApiClient() { BaseUrl = ServerAddress.ToString() }; string rawPingReply = ""; dynamic parsedReply = null; using (var client = new WebClient()) { try { rawPingReply = client.DownloadString(ServerAddress.ToString()); parsedReply = JsonConvert.DeserializeObject(rawPingReply); } catch { MessageBox.Show("Failed to contact " + ServerAddress.ToString()); RegisterButton.IsEnabled = true; RegisterButton.Content = "Register"; return; } } var existing = accounts.FirstOrDefault(account => account.Email == myUser.Email && account.RestApi == ServerAddress.ToString()); if (existing != null) { MessageBox.Show("You already have an account on " + ServerAddress.ToString() + " with " + myUser.Email + "."); return; } try { var response = spkClient.UserLoginAsync(myUser).Result; if (response.Success == false) { MessageBox.Show("Failed to login. " + response.Message); return; } var serverName = parsedReply.serverName; var isDefault = accounts.Any() ? false : true; var newaccount = new Account { Email = myUser.Email, RestApi = ServerAddress.ToString(), ServerName = (string)serverName, Token = response.Resource.Apitoken, IsDefault = isDefault }; LocalContext.AddAccount(newaccount); MessageBox.Show("Account login ok: You're good to go."); restApi = RegisterServerUrl.Text; apitoken = response.Resource.Apitoken; AccoutsTabControl.SelectedIndex = 0; LoadAccounts(); int index = accounts.Select((v, i) => new { acc = v, index = i }).First(x => x.acc.RestApi == LoginServerUrl.Text && x.acc.Email == LoginEmail.Text).index; AccountListBox.SelectedIndex = index; LoginServerUrl.Text = LoginEmail.Text = LoginPassword.Password = ""; } catch (Exception err) { MessageBox.Show("Failed to login user. " + err.InnerException.ToString()); return; } }
private void RegisterButton_Click(object sender, RoutedEventArgs e) { RegisterButton.IsEnabled = false; RegisterButton.Content = "Contacting server..."; var errs = ValidateRegister(); if (errs != "") { MessageBox.Show(errs); RegisterButton.IsEnabled = true; RegisterButton.Content = "Register"; return; } var names = RegisterName.Text.Split(' '); var myUser = new User() { Email = RegisterEmail.Text, Password = RegisterPassword.Password, Name = names?[0], Surname = names.Length >= 2 ? names?[1] : null, Company = RegisterCompany.Text, }; string rawPingReply = ""; dynamic parsedReply = null; using (var client = new WebClient()) { try { rawPingReply = client.DownloadString(ServerAddress.ToString()); parsedReply = JsonConvert.DeserializeObject(rawPingReply); } catch { MessageBox.Show("Failed to contact " + ServerAddress.ToString()); RegisterButton.IsEnabled = true; RegisterButton.Content = "Register"; return; } } var spkClient = new SpeckleApiClient() { BaseUrl = ServerAddress.ToString() }; try { var response = spkClient.UserRegisterAsync(myUser).Result; if (response.Success == false) { MessageBox.Show("Failed to register user. " + response.Message); RegisterButton.IsEnabled = true; RegisterButton.Content = "Register"; return; } var serverName = parsedReply.serverName; var isDefault = accounts.Any() ? false : true; var newaccount = new Account { Email = myUser.Email, RestApi = ServerAddress.ToString(), ServerName = (string)serverName, Token = response.Resource.Apitoken, IsDefault = isDefault }; LocalContext.AddAccount(newaccount); MessageBox.Show("Account creation ok: You're good to go."); restApi = RegisterServerUrl.Text; apitoken = response.Resource.Apitoken; RegisterButton.IsEnabled = true; RegisterButton.Content = "Register"; AccoutsTabControl.SelectedIndex = 0; LoadAccounts(); int index = accounts.Select((v, i) => new { acc = v, index = i }).First(x => x.acc.RestApi == RegisterServerUrl.Text && x.acc.Email == RegisterEmail.Text).index; AccountListBox.SelectedIndex = index; RegisterServerUrl.Text = RegisterEmail.Text = RegisterName.Text = RegisterCompany.Text = RegisterPassword.Password = RegisterPasswordConfirm.Password = ""; } catch (Exception err) { MessageBox.Show("Failed to register user. " + err.InnerException.ToString()); RegisterButton.IsEnabled = true; RegisterButton.Content = "Register"; return; } }