Esempio n. 1
0
        public JsonResult LoginByFacebook(string fbAccessToken)
        {
            var customerApi = new CustomerApi();
            //Get Facebook client profile by accessToken
            var fb = new FacebookClient();

            fb.AccessToken = fbAccessToken;
            dynamic me       = fb.Get("me?fields=id,name,gender,email,birthday");
            string  fbEmail  = me.email;
            string  fbName   = me.name;
            bool?   fbGender = null;

            if (me.gender == "male")
            {
                fbGender = true;
            }
            else if (me.gender == "female")
            {
                fbGender = false;
            }
            DateTime fbBirthday = new DateTime();

            if (me.birthday != null)
            {
                fbBirthday = DateTime.ParseExact(me.birthday, "MM/dd/yyyy", CultureInfo.InvariantCulture);
            }

            //Check Cusomter Facebook email exitested
            var customer = customerApi.Get().Where(q => q.fbEmail != null && q.fbEmail.Equals(fbEmail)).FirstOrDefault();

            if (customer != null)
            {
                //Check customer membershipcard
                customerApi.Edit(customer.ID, customer);
                var newToken = GenerateToken(customer.ID.ToString());
                return(Json(new
                {
                    status = new
                    {
                        success = true,
                        message = ConstantManager.MES_LOGIN_SUCCESS,
                        status = ConstantManager.STATUS_SUCCESS
                    },
                    data = new
                    {
                        data = new
                        {
                            accessToken = newToken,
                            isfirstLogin = false,
                            isPhoneNoLogin = false,
                            CustomerInfo = customer
                        }
                    },
                }, JsonRequestBehavior.AllowGet));
            }
            else
            {
                //Create customer by facebook profile data
                CustomerViewModel newCustomer = new CustomerViewModel();
                newCustomer.Name     = fbName;
                newCustomer.Birthday = fbBirthday;
                newCustomer.Gender   = fbGender;
                newCustomer.fbEmail  = fbEmail;
                int resId = -1;
                resId = customerApi.AddCustomer(newCustomer);
                if (resId != -1)
                {
                    var createdCustomer = customerApi.Get().Where(q => q.ID == resId).FirstOrDefault();
                    var newToken        = GenerateToken(createdCustomer.ID.ToString());
                    return(Json(new
                    {
                        status = new
                        {
                            success = true,
                            message = ConstantManager.MES_LOGIN_SUCCESS,
                            status = ConstantManager.STATUS_SUCCESS
                        },
                        data = new
                        {
                            data = new
                            {
                                accessToken = newToken,
                                isfirstLogin = true,
                                isPhoneNoLogin = false,
                                CustomerInfo = createdCustomer
                            }
                        },
                    }, JsonRequestBehavior.AllowGet));
                }
            }
            return(Json(new
            {
                status = new
                {
                    success = false,
                    message = ConstantManager.MES_LOGIN_FAIL,
                    status = ConstantManager.STATUS_SUCCESS
                },
                data = new { }
            }, JsonRequestBehavior.AllowGet));
        }