Пример #1
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;
                    }
                }
            }
        }
Пример #2
0
        /// <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;
            }
        }
Пример #3
0
        /*
         * 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;
        }
Пример #4
0
        /*
         * 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;
            }
        }