Exemplo n.º 1
0
        async Task LoginFacebookAsync(AuthNetwork authNetwork)
        {
            try
            {
                if (_facebookService.IsLoggedIn)
                {
                    _facebookService.Logout();
                }

                EventHandler <FBEventArgs <string> > userDataDelegate = null;

                userDataDelegate = async(object sender, FBEventArgs <string> e) =>
                {
                    switch (e.Status)
                    {
                    case FacebookActionStatus.Completed:
                        var facebookProfile = await Task.Run(() => JsonConvert.DeserializeObject <FacebookProfile>(e.Data));

                        var socialLoginData = new NetworkAuthData
                        {
                            Id         = facebookProfile.Id,
                            Logo       = authNetwork.Icon,
                            Background = authNetwork.Background,
                            Picture    = facebookProfile.Picture.Data.Url,
                            Name       = $"{facebookProfile.FirstName} {facebookProfile.LastName}",
                        };
                        await App.Current.MainPage.Navigation.PushModalAsync(new HomePage(socialLoginData));

                        break;

                    case FacebookActionStatus.Canceled:
                        await App.Current.MainPage.DisplayAlert("Facebook Auth", "Canceled", "Ok");

                        break;

                    case FacebookActionStatus.Error:
                        await App.Current.MainPage.DisplayAlert("Facebook Auth", "Error", "Ok");

                        break;

                    case FacebookActionStatus.Unauthorized:
                        await App.Current.MainPage.DisplayAlert("Facebook Auth", "Unauthorized", "Ok");

                        break;
                    }

                    _facebookService.OnUserData -= userDataDelegate;
                };

                _facebookService.OnUserData += userDataDelegate;

                string[] fbRequestFields = { "email", "first_name", "picture", "gender", "last_name" };
                string[] fbPermisions    = { "email" };
                await _facebookService.RequestUserDataAsync(fbRequestFields, fbPermisions);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }
Exemplo n.º 2
0
        async Task LoginFacebookAsync()
        {
            try
            {
                if (_facebookService.IsLoggedIn)
                {
                    _facebookService.Logout();
                }

                EventHandler <FBEventArgs <string> > userDataDelegate = null;

                userDataDelegate = async(object sender, FBEventArgs <string> e) =>
                {
                    if (e == null)
                    {
                        return;
                    }

                    switch (e.Status)
                    {
                    case FacebookActionStatus.Completed:
                        var facebookProfile = await Task.Run(() => JsonConvert.DeserializeObject <FacebookProfile>(e.Data));

                        var socialLoginData = new NetworkAuthData
                        {
                            Email    = facebookProfile.Email,
                            Name     = $"{facebookProfile.FirstName}",
                            LastName = $"{facebookProfile.LastName}",
                            Id       = facebookProfile.Id,
                            Picture  = "http://graph.facebook.com/" + facebookProfile.Id + "/picture?type=normal"
                        };

                        var navigationParams = new NavigationParameters
                        {
                            { "NetworkAuthData", socialLoginData }
                        };

                        await _navigationService.NavigateAsync("DashboardPage", navigationParams);

                        break;

                    case FacebookActionStatus.Canceled:
                        break;
                    }

                    _facebookService.OnUserData -= userDataDelegate;
                };

                _facebookService.OnUserData += userDataDelegate;

                string[] fbRequestFields = { "email", "first_name", "gender", "last_name" };
                string[] fbPermisions    = { "email" };
                await _facebookService.RequestUserDataAsync(fbRequestFields, fbPermisions);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }
Exemplo n.º 3
0
        private async Task LoginFacebookAsync(SocialNetwork socialNetwork)
        {
            try
            {
                if (_facebookClient.IsLoggedIn)
                {
                    _facebookClient.Logout();
                }

                EventHandler <FBEventArgs <string> > userDataDelegate = null;

                userDataDelegate = async(object sender, FBEventArgs <string> e) =>
                {
                    switch (e.Status)
                    {
                    case FacebookActionStatus.Completed:
                        var facebookProfile = await Task.Run(() => JsonConvert.DeserializeObject <FacebookProfile>(e.Data));

                        var userProfile = new UserProfile(facebookProfile.Id, $"{facebookProfile.FirstName} {facebookProfile.LastName}", facebookProfile.Email, facebookProfile.Picture.Data.Url);

                        await _userProfileService.CreateUserProfile(userProfile);

                        var userProfileFromDb = await _userProfileService.GetUserProfileByProfileId(userProfile.ProfileId);

                        await App.Current.MainPage.Navigation.PushModalAsync(new HomePage(userProfileFromDb));

                        break;

                    case FacebookActionStatus.Canceled:
                        await App.Current.MainPage.DisplayAlert("Facebook Auth", "Cancelled", "Ok");

                        break;

                    case FacebookActionStatus.Error:
                        await App.Current.MainPage.DisplayAlert("Facebook Auth", "Error", "Ok");

                        break;

                    case FacebookActionStatus.Unauthorized:
                        await App.Current.MainPage.DisplayAlert("Facebook Auth", "Unauthorized", "Ok");

                        break;
                    }

                    _facebookClient.OnUserData -= userDataDelegate;
                };

                _facebookClient.OnUserData += userDataDelegate;

                string[] fbRequestFields = { "email", "first_name", "picture", "gender", "last_name" };
                string[] fbPermisions    = { "email" };
                await _facebookClient.RequestUserDataAsync(fbRequestFields, fbPermisions);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }
Exemplo n.º 4
0
        async Task LoginFacebookAsync()
        {
            //Perform your "Can Login?" logic here...
            try
            {
                if (_facebookService.IsLoggedIn)
                {
                    _facebookService.Logout();
                }

                EventHandler <FBEventArgs <string> > userDataDelegate = null;

                userDataDelegate = async(object sender, FBEventArgs <string> e) =>
                {
                    if (e == null)
                    {
                        return;
                    }

                    switch (e.Status)
                    {
                    case FacebookActionStatus.Completed:
                        var facebookProfile = await Task.Run(() => JsonConvert.DeserializeObject <FacebookProfile>(e.Data));

                        var socialLoginData = new NetworkAuthData
                        {
                            Email = facebookProfile.Email,
                            Name  = $"{facebookProfile.FirstName} {facebookProfile.LastName}",
                            Id    = facebookProfile.Id
                        };
                        await App.Current.MainPage.Navigation.PushModalAsync(new MainMenuPage());

                        break;

                    case FacebookActionStatus.Canceled:
                        break;
                    }

                    _facebookService.OnUserData -= userDataDelegate;
                };

                _facebookService.OnUserData += userDataDelegate;

                string[] fbRequestFields = { "email", "first_name", "gender", "last_name" };
                string[] fbPermisions    = { "email" };
                await _facebookService.RequestUserDataAsync(fbRequestFields, fbPermisions);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }
Exemplo n.º 5
0
        async Task LoginFacebookAsync()
        {
            try
            {
                if (_facebookService.IsLoggedIn)
                {
                    _facebookService.Logout();
                }

                EventHandler <FBEventArgs <string> > userDataDelegate = null;

                userDataDelegate = async(object sender, FBEventArgs <string> e) =>
                {
                    if (e == null)
                    {
                        return;
                    }

                    switch (e.Status)
                    {
                    case FacebookActionStatus.Completed:
                        var facebookProfile = await Task.Run(() => JsonConvert.DeserializeObject <FacebookProfile>(e.Data));

                        dataClass.SignedIn     = true;
                        dataClass.LoggedInUser = new Account()
                        {
                            //Email = facebookProfile.Email,
                            //UserName = facebookProfile.FirstName + " " + facebookProfile.LastName,
                            //Uid = facebookProfile.Id
                        };
                        Application.Current.MainPage = new UserTabbedPage();
                        break;

                    case FacebookActionStatus.Canceled:
                        break;
                    }

                    _facebookService.OnUserData -= userDataDelegate;
                };

                _facebookService.OnUserData += userDataDelegate;

                string[] fbRequestFields = { "email", "first_name", "gender", "last_name" };
                string[] fbPermisions    = { "email" };
                await _facebookService.RequestUserDataAsync(fbRequestFields, fbPermisions);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }
Exemplo n.º 6
0
        public static async Task FacebookLinked()
        {
            try
            {
                if (_facebookService.IsLoggedIn)
                {
                    _facebookService.Logout();
                }

                EventHandler <FBEventArgs <string> > userDataDelegate = null;

                userDataDelegate = async(object sender, FBEventArgs <string> e) =>
                {
                    if (e == null)
                    {
                        return;
                    }
                    //switch (e.Status)
                    //{
                    //    case FacebookActionStatus.Completed:
                    //        break;
                    //    case FacebookActionStatus.Canceled:
                    //        break;
                    //}
                    _facebookService.OnUserData -= userDataDelegate;
                };

                _facebookService.OnUserData += userDataDelegate;

                string[] fbRequestFields = { "email", "first_name", "gender", "last_name", "picture.width(500).height(500)" };
                string[] fbPermisions    = { "email" };
                var      res             = await _facebookService.RequestUserDataAsync(fbRequestFields, fbPermisions);

                var fbProfile = await Task.Run(() => JsonConvert.DeserializeObject <FacebookUser>(res.Data));

                var fbUser = new User()
                {
                    FullName   = fbProfile.FirstName + " " + fbProfile.LastName,
                    Email      = fbProfile.Email,
                    FacebookId = long.Parse(fbProfile.Id),
                    AvatarUrl  = fbProfile.Picture.Data.Url
                };

                //check exist
                try
                {
                    ApiResponse response = await ApiHelper.Put("api/auth/sociallinked", fbUser, true);

                    if (response.IsSuccess)
                    {
                        var user = JsonConvert.DeserializeObject <User>(response.Content.ToString());
                        UserLogged.SaveProfile(user);
                        await Application.Current.MainPage.DisplayAlert("Thông báo", Language.lien_ket_facebook_thanh_cong, Language.dong);
                    }
                    else
                    {
                        throw new Exception(response.Message);
                    }
                }
                catch (Exception ex)
                {
                    await Application.Current.MainPage.DisplayAlert("Thông báo", ex.Message, Language.dong);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }
Exemplo n.º 7
0
        private async Task LoginFacebookAsync()
        {
            try
            {
                //check if logged in
                if (_facebookClient.IsLoggedIn)
                {
                    //TODO: Give appropriate handler
                    _facebookClient.Logout();
                }



                EventHandler <FBEventArgs <string> > userDataDelegate = null;

                userDataDelegate = async(object sender, FBEventArgs <string> e) =>
                {
                    if (e == null)
                    {
                        return;
                    }

                    string url = string.Empty;

                    switch (e.Status)
                    {
                    case FacebookActionStatus.Completed:
                        //Handles the FBEvent handler and deserialize it. This is a custom FB Event Handler, hence you can get e.Data
                        var fbUser = await Task.Run(() => JsonConvert.DeserializeObject <FacebookProfile>(e.Data));

                        //A check on null picture
                        if (fbUser.Picture.Data.Url != null)
                        {
                            url = fbUser.Picture.Data.Url;
                        }

                        //Assign the deserialize values into a generic socialMediaUser model
                        var socialMediaProfile = new SocialMediaUser
                        {
                            Name       = fbUser.FirstName,
                            Email      = fbUser.Email,
                            PictureURL = url
                        };

                        //Login is completed
                        //Send the data to the "Select League" page
                        await App.Current.MainPage.Navigation.PushModalAsync(new SelectLeagues());

                        break;

                    case FacebookActionStatus.Canceled:
                        //Redirect back to login
                        await App.Current.MainPage.Navigation.PushModalAsync(new LoginPage());

                        break;
                    }
                    //_facebookClient.OnUserData -= userDataDelegate;
                };

                _facebookClient.OnUserData += userDataDelegate;

                string[] fbRequestFields = { "email", "first_name", "last_name", "picture" };
                string[] fbPermisions    = { "email" };
                await _facebookClient.RequestUserDataAsync(fbRequestFields, fbPermisions);
            }
            catch (Exception e)
            {
                //TODO: A proper dialog command
                //https://github.com/aritchie/userdialogs/blob/master/src/Samples/Samples/ViewModels/StandardViewModel.cs
            }
        }
Exemplo n.º 8
0
        private async Task LoginFacebookAsync()
        {
            try
            {
                if (_facebookService.IsLoggedIn)
                {
                    _facebookService.Logout();
                }

                EventHandler <FBEventArgs <string> > userDataDelegate = null;

                userDataDelegate = async(object sender, FBEventArgs <string> e) =>
                {
                    switch (e.Status)
                    {
                    case FacebookActionStatus.Completed:
                        /*   System.Diagnostics.Debug.WriteLine(CrossFacebookClient.Current.ActiveToken);
                         * var facebookProfile = await Task.Run(() => JsonConvert.DeserializeObject<FacebookProfile>(e.Data));
                         *
                         * var profil = new Profil_Model();
                         * CultureInfo provider = CultureInfo.InvariantCulture;
                         * profil.Birth_Date = DateTime.ParseExact(facebookProfile.Birthday, "MM/dd/yyyy", provider);
                         * profil.FirstName = facebookProfile.FirstName;
                         * profil.LastName = facebookProfile.LastName;
                         * profil.Sexe = facebookProfile.Gender.First().ToString().ToUpper() + facebookProfile.Gender.Substring(1);
                         * profil.Email = facebookProfile.Email;
                         * profil.Avatar = facebookProfile.Picture.Data.Url;
                         * //   Application.Current.MainPage = new MainPage();
                         *
                         * var result = await RestApi.SocialChack("facebook", facebookProfile.Id);
                         * if (result.Item1)
                         * {
                         *     if (result.Item2)
                         *     {
                         *         var result2 = await RestApi.GetProfile();
                         *         if (result2.Item1)
                         *         {
                         *             DataStore.DeleteAllProfiles();
                         *             DataStore.AddProfil(JsonConvert.DeserializeObject<Profil_Model>(result2.Item2));
                         *             if (DataStore.GetSettingsAsync().Count() < 1)
                         *             {
                         *                 var settings = new Settings_Model();
                         *                 DataStore.AddSettings(settings);
                         *             }
                         *
                         *             Application.Current.MainPage = new MainPage();
                         *         }
                         *         else
                         *         {
                         *             DependencyService.Get<IMessage>().ShortAlert(result2.Item2);
                         *         }
                         *     }
                         *     else
                         *     {
                         *         DataStore.DeleteAllProfiles();
                         *         DataStore.AddProfil(profil);
                         *         await Navigation.PushAsync(new Profil_Base_Page(facebookProfile.Id, facebookProfile.Id), true);
                         *     }
                         * }
                         * else
                         * {
                         *     DependencyService.Get<IMessage>().ShortAlert(result.Item3);
                         * }*/
                        var Result = await RestApi.SocialChack(CrossFacebookClient.Current.ActiveToken);

                        if (Result.Item1)
                        {
                            var result2 = await RestApi.GetProfile();

                            if (result2.Item1)
                            {
                                var profil = JsonConvert.DeserializeObject <Profil_Model>(result2.Item2);
                                if (!string.IsNullOrEmpty(profil.DiabetesType))
                                {
                                    DataStore.DeleteAllProfiles();
                                    DataStore.AddProfil(profil);
                                    if (DataStore.GetSettingsAsync().Count() < 1)
                                    {
                                        var settings = new Settings_Model();
                                        DataStore.AddSettings(settings);
                                    }
                                    Application.Current.MainPage = new MainPage();
                                }
                                else
                                {
                                    DataStore.DeleteAllProfiles();
                                    DataStore.AddProfil(profil);
                                    var facebookProfile = await Task.Run(() => JsonConvert.DeserializeObject <FacebookProfile>(e.Data));

                                    await Navigation.PushAsync(new Profil_Base_Page("FacebookLogin", facebookProfile.Id, facebookProfile.Id), true);
                                }
                            }
                            else
                            {
                                DependencyService.Get <IMessage>().ShortAlert(result2.Item2);
                            }
                        }
                        else
                        {
                            DependencyService.Get <IMessage>().ShortAlert(Result.Item2);
                        }
                        break;

                    case FacebookActionStatus.Canceled:
                        await App.Current.MainPage.DisplayAlert("Facebook Auth", "Canceled", "Ok");

                        break;

                    case FacebookActionStatus.Error:
                        await App.Current.MainPage.DisplayAlert("Facebook Auth", "Error", "Ok");

                        break;

                    case FacebookActionStatus.Unauthorized:
                        await App.Current.MainPage.DisplayAlert("Facebook Auth", "Unauthorized", "Ok");

                        break;
                    }

                    _facebookService.OnUserData -= userDataDelegate;
                };

                _facebookService.OnUserData += userDataDelegate;

                string[] fbRequestFields = { "email", "birthday", "first_name", "picture", "gender", "last_name" };
                string[] fbPermisions    = { "email", "user_birthday", "user_gender" };
                await _facebookService.RequestUserDataAsync(fbRequestFields, fbPermisions);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }