/*
         * Catches the Google login redirect after successful Google authentication.
         * Attempts to switch login method for the logged in user to Google
         */
        public async Task Handle_RedirectUriCaught(string code)
        {
            string id = await GoogleServices.GetUserId(code);

            LoginAPI loginApi = new LoginAPI();

            HttpStatusCode responseCode = await loginApi.GoogleRegisterUser(UserController.Instance.LoggedInUser.id, id);

            switch (responseCode)
            {
            case HttpStatusCode.OK:
                await DisplayAlert("",
                                   "Login method changed to Google",
                                   "OK");

                break;

            case HttpStatusCode.BadRequest:
                await DisplayAlert("",
                                   "Failed to change login method (400)",
                                   "OK");

                break;

            case HttpStatusCode.ServiceUnavailable:
                await DisplayAlert("",
                                   "Server unavailable, check connection",
                                   "OK");

                break;

            case HttpStatusCode.Unauthorized:
                await DisplayAlert("",
                                   "Unauthorised to modify profile",
                                   "OK");

                break;

            case HttpStatusCode.InternalServerError:
                await DisplayAlert("",
                                   "Server error, please try again (500)",
                                   "OK");

                break;
            }
            updateButtons();
        }
示例#2
0
        /*
         * 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;
            }
        }