async void RelogUserAsync() { logoutStatus.Text = ""; activityIndicator.IsVisible = true; activityIndicator.IsRunning = true; var httpTask = await Task.Run <string>(() => HttpRequestHandler.PostUserLogout()); var httpResult = httpTask.ToString(); if (httpResult == "Success") { //Clear user session userSession.userLoginCookie = ""; userSession.username = ""; Settings.Username = string.Empty; Settings.Password = string.Empty; logoutStatus.Text = "Please re-login with your new password."; logoutStatus.TextColor = Color.Default; logoutStatus.FontAttributes = FontAttributes.None; activityIndicator.IsVisible = false; activityIndicator.IsRunning = false; await Task.Delay(3000); //App.Current.MainPage = new rootPage { Detail = new NavigationPage(new loginPage()) App.Current.MainPage = new rootPage(); var page = App.Current.MainPage as rootPage; var loginPage = new loginPage(); page.changePage(loginPage); } else { logoutStatus.TextColor = Color.Red; logoutStatus.FontAttributes = FontAttributes.None; logoutStatus.Text = "Error: Logout Unsuccessful, " + httpResult; } activityIndicator.IsVisible = false; activityIndicator.IsRunning = false; }
async void onUpdateBtnClicked(object sender, EventArgs e) { string oldPassword = "", newPassword = "", confPassword = ""; bool isInputsValid = true; errorLbl.Text = ""; //Inputs validation if (String.IsNullOrEmpty(confPasswordInput.Text) || String.IsNullOrWhiteSpace(confPasswordInput.Text)) { errorLbl.Text = "Please enter Confirm Password. "; isInputsValid = false; } else { confPassword = confPasswordInput.Text; } if (String.IsNullOrEmpty(newPasswordInput.Text) || String.IsNullOrWhiteSpace(newPasswordInput.Text)) { errorLbl.Text = "Please enter New Password. "; isInputsValid = false; } else { newPassword = newPasswordInput.Text; } //if new password and confirm password are not empty, check if they matches if (isInputsValid) { if (confPassword != newPassword) { errorLbl.Text = "Confirm Password does not match. "; isInputsValid = false; } else { if (String.IsNullOrEmpty(oldPasswordInput.Text) || String.IsNullOrWhiteSpace(oldPasswordInput.Text)) { errorLbl.Text = "Please enter Old Password. "; isInputsValid = false; } else { oldPassword = oldPasswordInput.Text; } if (isInputsValid) { activityIndicator.IsVisible = true; activityIndicator.IsRunning = true; //Send HTTP request to update password string httpTask = await Task.Run <string>(() => HttpRequestHandler.adminChangePassword(oldPassword, newPassword, confPassword)); string httpResult = httpTask.ToString(); activityIndicator.IsVisible = false; activityIndicator.IsRunning = false; if (httpResult == "Successfully changed password!") { await DisplayAlert("Success", "Password has been successfully changed! You will be logged out now.", "OK"); adminAuth.DeleteCredentials(); App.Current.MainPage = new rootPage(); var page = App.Current.MainPage as rootPage; var loginPage = new loginPage(); page.changePage(loginPage); } else { await DisplayAlert("Failed", httpResult, "OK"); errorLbl.Text = httpResult; } } } } }
async void OnRegisterButtonClicked(object sender, EventArgs e) { bool isErrorPresent = false; string errorMessage = ""; //Validate inputs if (confPasswordEntry.Text != passwordEntry.Text) { isErrorPresent = true; errorMessage = "Confirm password does not match"; } if (String.IsNullOrEmpty(passwordEntry.Text)) { isErrorPresent = true; errorMessage = "Please enter your Password"; } else { if (!Regex.IsMatch(passwordEntry.Text, @"^[a-zA-Z0-9]*$")) { isErrorPresent = true; errorMessage = "Wrong format for Password. Password may only contain alphabets and numbers."; } } if (String.IsNullOrEmpty(usernameEntry.Text)) { isErrorPresent = true; errorMessage = "Please enter your Username"; } else { if (!Regex.IsMatch(usernameEntry.Text, @"^[a-zA-Z0-9]*$")) { isErrorPresent = true; errorMessage = "Wrong format for Username. Username may only contain alphabets and numbers."; } } if (String.IsNullOrEmpty(emailEntry.Text)) { isErrorPresent = true; errorMessage = "Please enter your Email"; } else { if (!Regex.IsMatch(emailEntry.Text, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$")) { isErrorPresent = true; errorMessage = "Wrong format for Email. e.g. [email protected]"; } } if (String.IsNullOrEmpty(fullNameEntry.Text)) { isErrorPresent = true; errorMessage = "Please enter your Full Name"; } else { if (!Regex.IsMatch(fullNameEntry.Text, @"^[a-zA-Z]+(([',. -][a-zA-Z ])?[a-zA-Z]*)*$")) { isErrorPresent = true; errorMessage = "Wrong format for Full Name. Full name must be alphabetic."; } } if (isErrorPresent) { //Display error message if inputs have error await DisplayAlert("Failed", errorMessage, "OK"); } else { //Send HTTP request to register user activityIndicator.IsVisible = true; activityIndicator.IsRunning = true; string httpTask = await Task.Run <string>(() => HttpRequestHandler.PostRegisterNewUser(fullNameEntry.Text, emailEntry.Text, usernameEntry.Text, passwordEntry.Text, confPasswordEntry.Text)); string httpResult = httpTask.ToString(); if (httpResult == "Registration successful") { await DisplayAlert("Success", "You have successfully registered an account! Login now.", "OK"); var page = App.Current.MainPage as rootPage; var loginPage = new loginPage(); page.changePage(loginPage); } } }
async void LogoutUserAsync() { logoutStatus.Text = ""; activityIndicator.IsVisible = true; activityIndicator.IsRunning = true; //Clear user session userSession.userLoginCookie = ""; userSession.username = ""; Settings.Username = string.Empty; Settings.Password = string.Empty; Settings.Role = string.Empty; logoutStatus.Text = "You have successfully logout! You will be redirected to login page shortly."; logoutStatus.TextColor = Color.Default; logoutStatus.FontAttributes = FontAttributes.None; activityIndicator.IsVisible = false; activityIndicator.IsRunning = false; await Task.Delay(1000); App.Current.MainPage = new rootPage(); var page = App.Current.MainPage as rootPage; var loginPage = new loginPage(); page.changePage(loginPage); /*var httpTask = await Task.Run<string>(() => HttpRequestHandler.PostUserLogout()); * var httpResult = httpTask.ToString(); * * if (httpResult == "Success") * { * //Clear user session * userSession.userLoginCookie = ""; * userSession.username = ""; * Settings.Username = string.Empty; * Settings.Password = string.Empty; * Settings.Role = string.Empty; * logoutStatus.Text = "You have successfully logout! You will be redirected to login page shortly."; * logoutStatus.TextColor = Color.Default; * logoutStatus.FontAttributes = FontAttributes.None; * * activityIndicator.IsVisible = false; * activityIndicator.IsRunning = false; * await Task.Delay(1000); * * //App.Current.MainPage = new rootPage { Detail = new NavigationPage(new loginPage()) * App.Current.MainPage = new rootPage(); * var page = App.Current.MainPage as rootPage; * var loginPage = new loginPage(); * page.changePage(loginPage); * * } * else * { * logoutStatus.TextColor = Color.Red; * logoutStatus.FontAttributes = FontAttributes.None; * logoutStatus.Text = "Error: Logout Unsuccessful, " + httpResult; * }*/ }