/// <summary>
        /// Exposed method for users to call when trying to login to a Speckle server via code.
        /// </summary>
        /// <param name="email">The email of the account you wish to login with.</param>
        /// <param name="password">The corresponding password for the account.</param>
        /// <param name="callBack">A method callback which takes a <c>User</c>.</param>
        /// <returns>An async Task which can be awaited with a coroutine or just ignored.</returns>
        /// <remarks>If login was successful, the resulting user object is passed back. If failed, null
        /// is passed. Need to be using the <c>SpeckleCore</c> namespace to access this type.</remarks>
        public virtual async Task LoginAsync(string email, string password, Action <User> callBack)
        {
            if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(password))
            {
                throw new InvalidOperationException("The email and password arguments both need to be provided if you wish to login.");
            }

            SpeckleApiClient loginClient = new SpeckleApiClient(serverUrl.Trim());

            User user = new User {
                Email = email.Trim(), Password = password.Trim()
            };

            Debug.Log("Atempting login");

            ResponseUser userGet = await loginClient.UserLoginAsync(user);

            if (userGet == null)
            {
                Debug.LogError("Could not login");
                callBack?.Invoke(null);
            }
            else
            {
                loggedInUser = userGet.Resource;
                Debug.Log("Logged in as " + loggedInUser.Name + " " + loggedInUser.Surname);

                loginClient?.Dispose(true);

                callBack?.Invoke(loggedInUser);
            }
        }
        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 LoginButton_Click(object sender, RoutedEventArgs e)
        {
            var errs = ValidateLogin();

            if (errs != "")
            {
                MessageBox.Show(errs);
                return;
            }

            var myUser = new User()
            {
                Email    = this.LoginEmail.Text,
                Password = this.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;

                saveAccountToDisk(myUser.Email, response.Resource.Apitoken, ( string )serverName, this.ServerAddress.ToString(), this.ServerAddress.ToString());

                MessageBox.Show("Account login ok: You're good to go.");
                this.restApi  = this.RegisterServerUrl.Text;
                this.apitoken = response.Resource.Apitoken;

                this.Close();
            }
            catch (Exception err)
            {
                MessageBox.Show("Failed to login user. " + err.InnerException.ToString()); return;
            }
        }
예제 #4
0
        static async Task TestAccounts(SpeckleApiClient myClient)
        {
            Console.WriteLine();
            try
            {
                Console.WriteLine("Logging in a user.");
                var Response = await myClient.UserLoginAsync(new User()
                {
                    Email = "*****@*****.**", Password = "******"
                });

                Console.WriteLine("OK Got user: "******"Registering user." );
            //try
            //{
            //  var Response = await myClient.UserRegisterAsync( new User() { Name = "Dim", Email = DateTime.Now.ToString() + "*****@*****.**", Password = "******" } );
            //  Console.WriteLine( "OK Results:: " + Response.Resource.ToJson() );
            //}
            //catch ( Exception e )
            //{
            //  Console.WriteLine( e.Message );
            //}

            Console.WriteLine();
            Console.WriteLine("Searching for some users.");
            try
            {
                var Response = await myClient.UserSearchAsync(new User()
                {
                    Email = "testing.com"
                });

                Console.WriteLine("OK Results:: " + Response.Resources.Count);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }


            Console.WriteLine();
            Console.WriteLine("Getting profile.");
            try
            {
                var Response = await myClient.UserGetAsync();

                Console.WriteLine("OK Results:: " + Response.Resource.Email);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }


            Console.WriteLine();
            Console.WriteLine("Updating profile.");
            try
            {
                var Response = await myClient.UserUpdateProfileAsync(new User()
                {
                    Company = "BARARARA"
                });

                Console.WriteLine("OK Results:: " + Response.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            string userId = "lol";

            Console.WriteLine();
            Console.WriteLine("Getting profile.");
            try
            {
                var Response = await myClient.UserGetAsync(); userId = Response.Resource._id;
                Console.WriteLine("OK Results:: " + Response.Resource.Company);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }


            Console.WriteLine();
            Console.WriteLine("Getting profile by id.");
            try
            {
                var Response = await myClient.UserGetProfileByIdAsync(userId);

                Console.WriteLine("OK Results:: " + Response.Resource.Company);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }