/* * 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; } } } }
/* * Called when the Sign Up button is clicked */ async void SignUpButtonClicked(Object sender, EventArgs args) { string givenFirstName = InputValidation.Trim(firstNameInput.Text); string givenLastName = InputValidation.Trim(lastNameInput.Text); string givenPassword = InputValidation.Trim(passwordInput.Text); string givenEmail = InputValidation.Trim(emailInput.Text); string givenUsername = InputValidation.Trim(usernameInput.Text); string givenNhi = InputValidation.Trim(nhiInput.Text); if (!ServerConfig.Instance.IsConnectedToInternet()) { await DisplayAlert("", "Server unavailable, please check connection", "OK"); return; } // Check for valid inputs if (!InputValidation.IsValidTextInput(givenFirstName, false, false)) { await DisplayAlert("", "Please enter a valid first name", "OK"); return; } else if (!InputValidation.IsValidTextInput(givenLastName, false, false)) { await DisplayAlert("", "Please enter a valid last name", "OK"); return; } // Check if a username and valid email is entered else if (!InputValidation.IsValidEmail(givenEmail)) { await DisplayAlert("", "Valid email is required", "OK"); return; } else if (!InputValidation.IsValidTextInput(givenUsername, true, false)) { await DisplayAlert("", "Username is required", "OK"); return; } else if (!InputValidation.IsValidNhiInput(givenNhi)) { await DisplayAlert("", "Please enter a valid NHI number", "OK"); return; } else if (!InputValidation.IsValidTextInput(givenPassword, true, false)) { await DisplayAlert("", "Please enter a password", "OK"); return; } // DOB validation is through constraints on the DatePicker in the XAML // Check uniqueness Tuple <bool, bool, bool, bool> uniquenessResult = await isUsernameEmailUnique(); if (!uniquenessResult.Item1 || !uniquenessResult.Item2 || !uniquenessResult.Item3) { // Username + email taken await DisplayAlert( "", "Email/username/NHI is taken", "OK"); return; } LoginAPI loginAPI = new LoginAPI(); User inputUser = new User(); inputUser.name = new List <string> { givenFirstName, "", givenLastName }; inputUser.preferredName = new List <string> { givenFirstName, "", givenLastName }; inputUser.email = givenEmail; inputUser.nhi = givenNhi; inputUser.username = givenUsername; inputUser.password = givenPassword; inputUser.dateOfBirth = new CustomDate(dobInput.Date); inputUser.creationTime = new CustomDateTime(DateTime.Now); //Server requires to initialise the organs and user history items on creation inputUser.organs = new List <Organ>(); inputUser.userHistory = new List <HistoryItem>(); HttpStatusCode registerUserResult = await loginAPI.RegisterUser(inputUser); switch (registerUserResult) { case HttpStatusCode.Created: await LoginRegisteredUser(givenUsername, givenEmail, givenPassword); break; case HttpStatusCode.ServiceUnavailable: await DisplayAlert( "Failed to Register", "Server unavailable, check connection", "OK"); break; case HttpStatusCode.InternalServerError: await DisplayAlert( "Failed to Register", "Server error, please try again", "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; } }