/* * Function which sends an API call with the inputted username/email and password * to return a user object from the login call. */ private async Task LoginRegisteredUser(string givenUsername, string givenEmail, string givenPassword) { LoginAPI loginAPI = new LoginAPI(); // Attempt login with the newly created account HttpStatusCode loginResult; if (usernameInput.Text != null) { loginResult = await loginAPI.LoginUser(givenUsername, givenPassword); } else { loginResult = await loginAPI.LoginUser(givenEmail, givenPassword); } switch (loginResult) { case HttpStatusCode.OK: // Pop away login screen on successful login await DisplayAlert("", "Account successfully created", "OK"); // Dismiss the register modal dialog await Navigation.PopModalAsync(); // Dismiss the login modal dialog await parentLoginPage.OpenMainPageFromSignUp(); break; case HttpStatusCode.Unauthorized: await DisplayAlert( "Failed to Login", "Incorrent username/password", "OK"); break; case HttpStatusCode.ServiceUnavailable: await DisplayAlert( "Failed to Login", "Server unavailable, check connection", "OK"); break; case HttpStatusCode.InternalServerError: await DisplayAlert( "Failed to Login", "Server error", "OK"); break; } }
public async Task updateUser() { await loginAPI.LoginUser("buzz", "drowssap"); UserController.Instance.LoggedInUser.name[0] = "Buzzy"; HttpStatusCode result = await userAPI.UpdateUser(false); Assert.AreEqual(HttpStatusCode.Created, result); UserController.Instance.Logout(); await loginAPI.LoginUser("buzz", "drowssap"); Assert.AreEqual("Buzzy", UserController.Instance.LoggedInUser.FullName); }
public async Task testValidInteraction() { // Login to provide DrugAPI with gender + age await loginAPI.LoginUser("buzz", "drowssap"); DrugInteractionResult result = await drugAPI.RetrieveDrugInteractions("Ibuprofen", "Asacol"); Assert.AreEqual(69, result.ageInteractions.Count); }
public async Task loginCorrectUser() { HttpStatusCode result = await loginAPI.LoginUser("buzz", "drowssap"); Assert.AreEqual(HttpStatusCode.OK, result); Assert.AreEqual("Buzz", UserController.Instance.LoggedInUser.name[0]); }
/* * Brings up lthe Facebook authentication page and either * logs in or creates a new user depending on whether the Facebook ID is recognised */ private async void WebViewOnNavigated(object sender, WebNavigatedEventArgs e) { var accessToken = facebookServices.ExtractAccessTokenFromUrl(e.Url); if (accessToken != "") { FacebookProfile facebookProfile = await facebookServices.GetFacebookProfileAsync(accessToken); string password = "******"; UserAPI userAPI = new UserAPI(); LoginAPI loginAPI = new LoginAPI(); //Do a check to see if user is already in the database - if they are then skip the register and go to login if not just login Tuple <HttpStatusCode, bool> isUniqueEmailResult = await userAPI.isUniqueApiId(facebookProfile.Id); if (isUniqueEmailResult.Item1 != HttpStatusCode.OK) { Console.WriteLine("Failed to connect to server for checking of unique email"); } //This account already exists if (isUniqueEmailResult.Item2 == false) { HttpStatusCode statusCode = await loginAPI.LoginUser(facebookProfile.Id); switch (statusCode) { case HttpStatusCode.OK: // Pop away login screen on successful login HttpStatusCode httpStatusCode = await userAPI.GetUserPhoto(); UserController.Instance.mainPageController.updateMenuPhoto(); await Navigation.PopModalAsync(); await parentLoginPage.OpenMainPageFromSignUp(); break; case HttpStatusCode.Unauthorized: await DisplayAlert( "Failed to Login", "Incorrect username/password", "OK"); break; case HttpStatusCode.ServiceUnavailable: await DisplayAlert( "Failed to Login", "Server unavailable, check connection", "OK"); break; case HttpStatusCode.InternalServerError: await DisplayAlert( "Failed to Login", "Server error", "OK"); break; } } else //Create new account { var waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset); var modalPage = new NavigationPage(new IncompleteFacebookDetailsPage(facebookProfile)); modalPage.Disappearing += (sender2, e2) => { waitHandle.Set(); }; await Navigation.PushModalAsync(modalPage); await Task.Run(() => waitHandle.WaitOne()); facebookProfile.Email = UserController.Instance.FacebookEmail; facebookProfile.Birthday = UserController.Instance.FacebookDateOfBirth; facebookProfile.NHI = UserController.Instance.NHI; User inputUser = new User { name = new List <string> { facebookProfile.FirstName, "", facebookProfile.LastName }, preferredName = new List <string> { facebookProfile.FirstName, "", facebookProfile.LastName }, email = facebookProfile.Email, username = facebookProfile.Email, password = password, dateOfBirth = new CustomDate(DateTime.Parse(facebookProfile.Birthday)), creationTime = new CustomDateTime(DateTime.Now), gender = facebookProfile.Gender.ToUpper(), organs = new List <Organ>(), userHistory = new List <HistoryItem>(), nhi = facebookProfile.NHI }; //Server requires to initialise the organs and user history items on creation HttpStatusCode registerUserResult = await loginAPI.RegisterUser(inputUser); switch (registerUserResult) { case HttpStatusCode.Created: HttpStatusCode registerFacebookUserResult = await loginAPI.FacebookRegisterUser(inputUser.id, facebookProfile.Id); //Set the local profile picture to the picture object HttpClient client = ServerConfig.Instance.client; // Get the single userController instance UserController userController = UserController.Instance; var bytes = await client.GetByteArrayAsync(facebookProfile.Picture.Data.Url); Photo receievedPhoto = new Photo(Convert.ToBase64String(bytes)); userController.photoObject = receievedPhoto; ImageSource source = ImageSource.FromStream(() => new MemoryStream(bytes)); userController.ProfilePhotoSource = source; HttpStatusCode loginUserResult = await loginAPI.LoginUser(facebookProfile.Id); switch (loginUserResult) { case HttpStatusCode.OK: //Upload the photo to the server - must happen after a login as the token is required HttpStatusCode photoUpdated = await userAPI.UpdateUserPhoto(); if (photoUpdated != HttpStatusCode.OK) { Console.WriteLine("Error uploading facebook photo to the server"); } await Navigation.PopModalAsync(); await parentLoginPage.OpenMainPageFromSignUp(); break; case HttpStatusCode.Unauthorized: await DisplayAlert( "Failed to Login", "Incorrect username/password", "OK"); break; case HttpStatusCode.ServiceUnavailable: await DisplayAlert( "Failed to Login", "Server unavailable, check connection", "OK"); break; case HttpStatusCode.InternalServerError: await DisplayAlert( "Failed to Login", "Server error", "OK"); break; } break; case HttpStatusCode.ServiceUnavailable: await DisplayAlert( "Failed to Register", "Server unavailable, check connection", "OK"); break; case HttpStatusCode.InternalServerError: await DisplayAlert( "Failed to Register", "Username/Email may be taken", "OK"); break; } } } }
/* * Creates a new user with a Google login */ public async Task RegisterNewUser() { UserAPI userAPI = new UserAPI(); LoginAPI loginAPI = new LoginAPI(); HttpStatusCode registerUserResult = await loginAPI.RegisterUser(googleUser); HttpStatusCode accountUpdate = await loginAPI.GoogleRegisterUser(googleUser.id, api_id); switch (registerUserResult) { case HttpStatusCode.Created: //Set the local profile picture to the picture object HttpClient client = ServerConfig.Instance.client; // Get the single userController instance UserController userController = UserController.Instance; var bytes = await client.GetByteArrayAsync(profileImageURL); Photo receievedPhoto = new Photo(Convert.ToBase64String(bytes)); userController.photoObject = receievedPhoto; ImageSource source = ImageSource.FromStream(() => new MemoryStream(bytes)); userController.ProfilePhotoSource = source; HttpStatusCode loginUserResult = await loginAPI.LoginUser(api_id); switch (loginUserResult) { case HttpStatusCode.OK: //Upload the photo to the server - must happen after a login as the token is required HttpStatusCode photoUpdated = await userAPI.UpdateUserPhoto(); if (photoUpdated != HttpStatusCode.OK) { Console.WriteLine("Error uploading facebook photo to the server"); } await Navigation.PopModalAsync(); await parentLoginPage.OpenMainPageFromSignUp(); break; case HttpStatusCode.Unauthorized: await DisplayAlert( "Failed to Login", "Incorrect username/password", "OK"); break; case HttpStatusCode.ServiceUnavailable: await DisplayAlert( "Failed to Login", "Server unavailable, check connection", "OK"); break; case HttpStatusCode.InternalServerError: await DisplayAlert( "Failed to Login", "Server error", "OK"); break; } break; case HttpStatusCode.ServiceUnavailable: await DisplayAlert( "Failed to Register", "Server unavailable, check connection", "OK"); break; case HttpStatusCode.InternalServerError: await DisplayAlert( "Failed to Register", "Username/Email may be taken", "OK"); break; } }
/* * Attempts to login via Google * If the Google account is not recognised, a new accout is created */ private async Task LoginAsGoogleUser(Tuple <User, string, string> parsedUser) { User googleUser = parsedUser.Item1; string profileImageURL = parsedUser.Item2; string id = parsedUser.Item3; string password = "******"; UserAPI userAPI = new UserAPI(); LoginAPI loginAPI = new LoginAPI(); //Do a check to see if user is already in the database - if they are then skip the register and go to login if not just login Tuple <HttpStatusCode, bool> isUniqueEmailResult = await userAPI.isUniqueApiId(id); if (isUniqueEmailResult.Item1 != HttpStatusCode.OK) { Console.WriteLine("Failed to connect to server for checking of unique email"); } if (isUniqueEmailResult.Item2 == false) { HttpStatusCode statusCode = await loginAPI.LoginUser(id); switch (statusCode) { case HttpStatusCode.OK: // Pop away login screen on successful login HttpStatusCode httpStatusCode = await userAPI.GetUserPhoto(); UserController.Instance.mainPageController.updateMenuPhoto(); baseMainPage.userLoggedIn(); await Navigation.PushModalAsync(baseMainPage); break; case HttpStatusCode.Unauthorized: await DisplayAlert( "Failed to Login", "Incorrect username/password", "OK"); break; case HttpStatusCode.ServiceUnavailable: await DisplayAlert( "Failed to Login", "Server unavailable, check connection", "OK"); break; case HttpStatusCode.InternalServerError: await DisplayAlert( "Failed to Login", "Server error", "OK"); break; } } else { await Navigation.PushModalAsync(new GooglePage(this, googleUser, profileImageURL, id)); } IsLoading = false; }
/// <summary> /// Logs in as a previously logged in user who did not log out /// </summary> /// <param name="usernameEmail"></param> /// <param name="password"></param> async void LoginStoredUser(string usernameEmail, string password) { IsLoading = true; string givenUsernameEmail = usernameEmail; string givenPassword = password; if (!InputValidation.IsValidTextInput(givenUsernameEmail, true, false) || !InputValidation.IsValidTextInput(givenPassword, true, false)) { await DisplayAlert("", "Username/email and password is invalid", "OK"); return; } LoginAPI loginAPI = new LoginAPI(); HttpStatusCode statusCode = await loginAPI.LoginUser(givenUsernameEmail, givenPassword); switch (statusCode) { case HttpStatusCode.OK: // Fetch photo only on user login if (!ClinicianController.Instance.isLoggedIn()) { UserAPI userAPI = new UserAPI(); await userAPI.GetUserPhoto(); } if (ClinicianController.Instance.isLoggedIn()) { baseMainPage.clinicianLoggedIn(); } else { baseMainPage.userLoggedIn(); } await Navigation.PushModalAsync(baseMainPage); IsLoading = false; usernameEmailInput.Text = ""; passwordInput.Text = ""; break; case HttpStatusCode.Unauthorized: IsLoading = false; await DisplayAlert( "Failed to Login", "Incorrect username/password", "OK"); break; case HttpStatusCode.ServiceUnavailable: IsLoading = false; await DisplayAlert( "Failed to Login", "Server unavailable, check connection", "OK"); break; case HttpStatusCode.InternalServerError: IsLoading = false; await DisplayAlert( "Failed to Login", "Server error", "OK"); break; case HttpStatusCode.Conflict: IsLoading = false; await DisplayAlert( "Failed to Login", "User is deceased. Please consult a Registered Clinician", "OK"); break; } }
/* * Called when the Login button is pressed */ async void LoginButtonClicked(object sender, EventArgs args) { IsLoading = true; string givenUsernameEmail = InputValidation.Trim(usernameEmailInput.Text); string givenPassword = InputValidation.Trim(passwordInput.Text); rememberLogin = RememberMeSwitch.IsToggled; if (!InputValidation.IsValidTextInput(givenUsernameEmail, true, false) || !InputValidation.IsValidTextInput(givenPassword, true, false)) { await DisplayAlert("", "Please enter a valid username/email and password", "OK"); IsLoading = false; return; } LoginAPI loginAPI = new LoginAPI(); HttpStatusCode statusCode = await loginAPI.LoginUser(givenUsernameEmail, givenPassword); switch (statusCode) { case HttpStatusCode.OK: // Fetch photo only on user login if (!ClinicianController.Instance.isLoggedIn()) { UserAPI userAPI = new UserAPI(); await userAPI.GetUserPhoto(); } if (ClinicianController.Instance.isLoggedIn()) { baseMainPage.clinicianLoggedIn(); } else { baseMainPage.userLoggedIn(); } if (rememberLogin) { await storeLoginDetails(givenUsernameEmail, givenPassword); } await Navigation.PushModalAsync(baseMainPage); IsLoading = false; usernameEmailInput.Text = ""; passwordInput.Text = ""; break; case HttpStatusCode.Unauthorized: IsLoading = false; await DisplayAlert( "Failed to Login", "Incorrect username/password", "OK"); break; case HttpStatusCode.BadRequest: IsLoading = false; await DisplayAlert( "Failed to Login", "Incorrect username/password", "OK"); break; case HttpStatusCode.ServiceUnavailable: IsLoading = false; await DisplayAlert( "Failed to Login", "Server unavailable, check connection", "OK"); break; case HttpStatusCode.InternalServerError: IsLoading = false; await DisplayAlert( "Failed to Login", "Server error", "OK"); break; case HttpStatusCode.Conflict: IsLoading = false; await DisplayAlert( "Failed to Login", "User is deceased. Please consult a Registered Clinician", "OK"); break; } }