Esempio n. 1
0
        async private void studentSignInClick(object sender, EventArgs e)
        {
            /// <summary>
            /// Subroutine that is executed when a student wants to sign into the program.
            /// It sends a GET request to the API to try and obtain student details.
            /// </summary>

            string username = usernameTextBox.Text;
            string password = passwordTextBox.Text;

            flushTextBoxes();

            if (checkBox1.Checked)
            {
                if (username == "")
                {
                    MessageBox.Show("Username cannot be left blank!");
                    return;
                }
                StudentGetPassword passwordForm = new StudentGetPassword();
                var dialog = passwordForm.ShowDialog();
                if (dialog == DialogResult.OK)
                {
                    password = passwordForm.passwordResult;
                }
                else
                {
                    MessageBox.Show("An error has occurred, please try again.");
                    return;
                }

                // Send request to API to change password of `username` to `password`
                try {
                    await APIHandler.ResetPassword(username, password);

                    string newCredentials = WebRequestHandler.ConvertToBase64(username + ":" + password);
                    WebRequestHandler.SetAuthorizationHeader(newCredentials);

                    Student student = await APIHandler.GetStudent(username : username); // Get the student with username `username` from the API

                    closedByProgram = true;                                             // Set boolean to prevent Application.Exit() call
                    this.Close();                                                       // Close current form
                    FormController.studentMain = new StudentMainForm(student);          // Open the new form
                } catch (HttpStatusUnauthorized) {
                    MessageBox.Show("Your account already has a password - please ask a teacher to reset your password.");
                    return;
                }
            }
            else
            {
                if (username == "" || password == "")
                {
                    MessageBox.Show("Username or password cannot be left blank!"); //TODO: Does this need a messagebox? Nicer way of showing error?
                    return;                                                        // Do not execute more of the subroutine
                }

                string credentials = WebRequestHandler.ConvertToBase64(username + ":" + password);
                APIHandler.SetAuthorizationHeader(credentials);
                if (await APIHandler.IsUserValid(UserType.Student, username, password))
                {
                    Student student = await APIHandler.GetStudent(username : username); // Get the student with username `username` from the API

                    closedByProgram = true;                                             // Set boolean to prevent Application.Exit() call
                    this.Close();                                                       // Close current form
                    FormController.studentMain = new StudentMainForm(student);          // Open the new form
                }
                else
                {
                    MessageBox.Show("Your account doesn't exist.");
                }
            }
        }