/// <summary>
        /// Attempts the API log in request to log a user into the application
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns>
        /// UserLoginInfoModel containing all of the user's info from login
        /// </returns>
        private async Task <UserLoginInfoModel> connectToUserAcct(String username, String password)
        {
            UserLoginInfoModel ret = new UserLoginInfoModel();

            SimpleLoginPostModel loginInfo = new SimpleLoginPostModel();

            loginInfo.username = username;

            if (ApiHelper.ApiClient == null)
            {
                ApiHelper.InitializeClient();
            }

            HttpResponseMessage response =
                await ApiHelper.ApiClient.PostAsJsonAsync("/v1/login", loginInfo);

            if (response.IsSuccessStatusCode)
            {
                var a = response.ToString();

                string content = await response.Content.ReadAsStringAsync();

                ret = JsonConvert.DeserializeObject <UserLoginInfoModel>(content);
            }


            //id (int)
            //username (string)
            //inserted_at (date time)

            return(ret);
        }
Esempio n. 2
0
        /// <summary>
        /// ctor that converts a UserLoginInfoModel to a CurrentUser
        /// </summary>
        /// <param name="loginInfo">
        /// UserLoginInfoModel to be converted to a CurrentUser for the each use cycle
        /// of the application
        /// </param>
        public AppWindow(UserLoginInfoModel loginInfo)
        {
            InitializeComponent();
            CurrentUser user = new CurrentUser(loginInfo);
            HomeScreen  home = new HomeScreen(user);

            this.Content = home;
        }
        /// <summary>
        /// Log in logic when "Log In" button is pressed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            // This is the handling of logging in
            String username = Username.Text;
            String password = Password.Password;

            // insure username and password are not empty lengths
            if (username.Length > 0 && password.Length > 0)
            {
                LoginButton.IsEnabled = false;
                NewAccount.IsEnabled  = false;
                //attempt log in API call
                try
                {
                    // await response because we do not want to proceed until
                    // log in is validated
                    UserLoginInfoModel user =
                        await connectToUserAcct(username, password);

                    if (user.id != -1)
                    {
                        // User log in was successful
                        AppWindow home = new AppWindow(user);
                        home.Show();
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Invalid Username/Password");
                        LoginButton.IsEnabled = true;
                        NewAccount.IsEnabled  = true;
                    }
                }
                catch (HttpRequestException)
                {
                    var result = MessageBox.Show(
                        "Connection Failed Successfully. Yes to continue without account, " +
                        "No to re-attempt login, Cancel to close the application",
                        "No API Response", MessageBoxButton.YesNoCancel);

                    if (result == MessageBoxResult.Yes)
                    {
                        // This is a simulated log in available if the API is down for testing
                        // purposes only
                        UserLoginInfoModel user = new UserLoginInfoModel();
                        user.id          = 0;
                        user.username    = "******";
                        user.inserted_at = "potatoes";
                        AppWindow home = new AppWindow(user);
                        home.Show();
                        this.Close();
                    }
                    else if (result == MessageBoxResult.Cancel)
                    {
                        this.Close();
                    }
                    else
                    {
                        LoginButton.IsEnabled = true;
                        NewAccount.IsEnabled  = true;
                    }
                }
            }
            else
            {
                MessageBox.Show("Please enter your username and password", "Empty Username/Password",
                                MessageBoxButton.OK);
            }
        }
 /// <summary>
 /// Alt Ctor for a CurrentUser with a UserLoginInfoModel parameter
 /// </summary>
 /// <param name="info">
 /// UserLoginInfoModel from login containing most of the user information
 /// </param>
 public CurrentUser(UserLoginInfoModel info)
 {
     Username = info.username;
     UserId   = info.id;
 }