async private void teacherSignInClick(object sender, EventArgs e) { /// <summary> /// Subroutine that signs a teacher into their account. It sends GET /teacher/<username>?username=True /// </summary> string username = usernameTextBox.Text; string password = passwordTextBox.Text; flushTextBoxes(); if (checkBox1.Checked == true) { MessageBox.Show("Teachers cannot use the first time sign-in feature, and should enter their password."); return; } if (username == "" || password == "") { MessageBox.Show("Username or password cannot be left blank!"); return; // Do not execute more of the subroutine } string credentials = WebRequestHandler.ConvertToBase64(username + ":" + password); APIHandler.SetAuthorizationHeader(credentials); if (await APIHandler.IsUserValid(UserType.Teacher, username, password)) { Teacher teacher = await APIHandler.GetTeacher(username : username); closedByProgram = true; // Set boolean to prevent Application.Exit() call this.Close(); // Close current form FormController.teacherMain = new TeacherMainForm(teacher); // Open the new form } else { MessageBox.Show("Your account doesn't exist."); } }
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."); } } }