public ActionResult RegisterFacebook(RegisterFacebookModel model)
        {
            if (!ModelState.IsValid || Members.IsLoggedIn() || string.IsNullOrEmpty(model.Email))
            {
                return(PartialView("Profile/RegisterFacebook", model));
            }

            var memberService    = Services.MemberService;
            var checkMemberEmail = Services.MemberService.GetByEmail(model.Email);

            if (checkMemberEmail != null)
            {
                ModelState.AddModelError("FormGenericError", "The email already in use!");
                return(PartialView("Profile/RegisterFacebook", model));
            }

            // check if username unique
            if (Services.MemberService.GetByUsername(model.Username.Trim().ToLower()) != null)
            {
                ModelState.AddModelError("FormGenericError", "Το username χρησιμοποιείται ήδη από άλλο μέλος, επιλέξτε ένα άλλο.");
                return(PartialView("Profile/RegisterFacebook", model));
            }

            // last check FB
            var client = new Facebook.FacebookClient(model.AccessToken);

            if (client != null)
            {
                var userDetail = client.Get("/me", new { fields = "last_name,id,email,name,birthday,age_range,middle_name,first_name,locale,verified" });
                if (userDetail != null)
                {
                    var loginDetail = new FacebookLoginDetail(JObject.FromObject(userDetail), model.AccessToken);
                    if (loginDetail == null || loginDetail.UserId != model.FacebookUserId)
                    {
                        ModelState.AddModelError("FormGenericError", "Κάντε Login στο Facebook με τον δικό σας λογαριασμό και ξαναδοκιμάστε!");
                        return(PartialView("Profile/RegisterFacebook", model));
                    }

                    if (loginDetail == null
#if !DEBUG
                        || !loginDetail.IsFacebookVerified
#endif
                        )
                    {
                        ModelState.AddModelError("FormGenericError", "Πρέπει να έχετε επιβεβαιώση τον λογαριασμό σας στο Facebook ώστε να κάνετε την εγγραφή σας.");
                        return(PartialView("Profile/RegisterFacebook", model));
                    }

                    if (string.Compare(loginDetail.Username, model.Username, true) != 0 &&
                        string.Compare(loginDetail.Email, model.Email, true) != 0)
                    {
                        ModelState.AddModelError("FormGenericError",
                                                 "Μπορείτε να αλλάξετε μόνο είτε το email σας, είτε το username σας όταν κάνετε εγγραφή μέσω Facebook.");
                        return(PartialView("Profile/RegisterFacebook", model));
                    }

                    // assign correct userid.
                    model.FacebookUserId = loginDetail.UserId;
                }
            }
            else
            {
                // could not logon server side
                ModelState.AddModelError("FormGenericError", "Παρακάλω κάντε login στο Facebook πρώτα ώστε να προχωρήσετε με την εγγραφή σας στο site.");
                return(PartialView("Profile/RegisterFacebook", model));
            }

            // check if user exists as a facebook user already.
            if (!string.IsNullOrEmpty(model.FacebookUserId))
            {
                var existingMember = Services.MemberService.GetMembersByPropertyValue("facebookUserId", model.FacebookUserId).FirstOrDefault();
                if (existingMember != null && existingMember.IsApproved)
                {
                    // log him in.
                    Members.Login(existingMember.Username, model.FacebookUserId);
                    return(RedirectToCurrentUmbracoPage());
                }
            }

            // all good!, create the member
            try
            {
                var curPage   = Umbraco.TypedContent(model.Cpid);
                var culture   = curPage.GetCulture();
                var newMember = memberService.CreateMember(model.Username.Trim(), model.Email.Trim(), $"{model.Surname.Trim()} {model.Name.Trim()}", Constants.MemberAlias);
                if (newMember == null)
                {
                    ModelState.AddModelError("FormGenericError", "Κάποιο σοβαρό σφάλμα προεκυψε. Προσπαθήστε ξανά!");
                    LogHelper.Warn(typeof(AuthSurfaceController), $"Cannot create new Facebook member: {model.Email}");
                    return(PartialView("Profile/RegisterFacebook", model));
                }
                // Membership properties
                newMember.SetValue("emailVerifyHash", "ok");
                newMember.SetValue("hasVerifiedEmail", true);
                newMember.IsApproved = true;
                newMember.SetValue("facebookUserId", model.FacebookUserId);
                newMember.SetValue("memberisOver18", true);
                Services.MemberService.Save(newMember);
                Services.MemberService.SavePassword(newMember, model.FacebookUserId);

                // assign member to group in order to see profile page
                var memberRoles = Services.MemberService.GetAllRoles(newMember.Id);
                if (!memberRoles.Any(z => string.Compare(z, Constants.VerifiedMemberGroup, true) == 0))
                {
                    Services.MemberService.AssignRole(newMember.Id, Constants.VerifiedMemberGroup);
                }

                //Services.MemberService.Save(newMember);
                if (!Members.Login(newMember.Username, model.FacebookUserId))
                {
                    ModelState.AddModelError("FormGenericError", "Συνέβη κάποιο σοβαρό σφάλμα. Παρακαλώ προσπαθήστε ξάνα.");
                    LogHelper.Warn(typeof(AuthSurfaceController), $"Cannot Login Facebook new member: {model.Email}");
                    return(PartialView("Profile/RegisterFacebook", model));
                }
                return(GetLoggedInMember(newMember.Name, newMember.Id, curPage.Id.ToString(), Umbraco));
            }
            catch (Exception ex)
            {
                LogHelper.Error(typeof(AuthSurfaceController), $"Error while registering Facebook member {model.Email}", ex);
                ModelState.AddModelError("FormGenericError", "Συνέβη κάποιο σοβαρό σφάλμα. Παρακαλώ προσπαθήστε ξάνα. Αλλιώς επικοινωνήστε με το support μας.");
                return(PartialView("Profile/RegisterFacebook", model));
            }
        }
        public JsonResult ValidateFacebook(LoginModel model)
        {
            // get all data.
            var accessToken = model != null && !string.IsNullOrEmpty(model.AccessToken) ? model.AccessToken : null;

            Facebook.FacebookClient client = null;
            if (accessToken != null)
            {
                client = new Facebook.FacebookClient(accessToken);
            }

            if (accessToken == null || client == null)
            {
                return(Json(new
                {
                    ErrorMessage = "You should provide correct credentials to login with facebook.",
                    Success = false
                }));
            }

            var userDetail = client.Get("/me", new { fields = "last_name,id,email,name,middle_name,first_name,locale,verified" });

            if (userDetail == null)
            {
                return(Json(new
                {
                    ErrorMessage = "You should provide correct credentials to login with facebook.",
                    Success = false
                }));
            }

            var loginDetail = new FacebookLoginDetail(JObject.FromObject(userDetail), accessToken);

            if (loginDetail == null
#if !DEBUG
                || !loginDetail.IsFacebookVerified
#endif
                )
            {
                return(Json(new
                {
                    ErrorMessage = "You have to verify you facebook acount!",
                    Success = false
                }));
            }

            // ok, check if exists as Member with userid, if so, log him in. and return state
            if (!string.IsNullOrEmpty(loginDetail.UserId))
            {
                var member = Services.MemberService.GetMembersByPropertyValue("facebookUserId", loginDetail.UserId).FirstOrDefault();
                if (member != null)
                {
                    var tryLogin = Members.Login(member.Username, loginDetail.UserId);
                    if (!tryLogin)
                    {
                        return(Json(new
                        {
                            ErrorMessage = "Your password is invalid",
                            Success = false,
                            RedirectUrl = string.Empty,
                            Data = loginDetail
                        }));
                    }
                    return(GetLoggedInMember(member.Name, member.Id, model.CurrentPageId, Umbraco));
                }
            }

            // Redirect him to register, with predifined fields
            return(Json(new
            {
                ErrorMessage = string.Empty,
                Success = false,
                IsValidMember = false,
                NeedsRegister = true,
                Data = loginDetail
            }));
        }