public FacebookAuthHelper(bool requiresPublishPermissions, Action afterAuthAction)
 {
     FacebookAuthHelper._instance        = this;
     this.requiresPublishPermissions     = requiresPublishPermissions;
     this.afterAuthAction                = afterAuthAction;
     FacebookManager.sessionOpenedEvent += new Action(this.sessionOpenedEvent);
     FacebookManager.loginFailedEvent   += new Action <P31Error>(this.loginFailedEvent);
     if (requiresPublishPermissions)
     {
         FacebookManager.reauthorizationSucceededEvent += new Action(this.reauthorizationSucceededEvent);
         FacebookManager.reauthorizationFailedEvent    += new Action <P31Error>(this.reauthorizationFailedEvent);
     }
 }
 public void cleanup()
 {
     if (this.afterAuthAction != null)
     {
         FacebookManager.sessionOpenedEvent -= new Action(this.sessionOpenedEvent);
         FacebookManager.loginFailedEvent   -= new Action <P31Error>(this.loginFailedEvent);
         if (this.requiresPublishPermissions)
         {
             FacebookManager.reauthorizationSucceededEvent -= new Action(this.reauthorizationSucceededEvent);
             FacebookManager.reauthorizationFailedEvent    -= new Action <P31Error>(this.reauthorizationFailedEvent);
         }
     }
     FacebookAuthHelper._instance = null;
 }
    public void cleanup()
    {
        // if the afterAuthAction is not null we have not yet cleaned up
        if (afterAuthAction != null)
        {
            FacebookManager.sessionOpenedEvent -= sessionOpenedEvent;
            FacebookManager.loginFailedEvent   -= loginFailedEvent;

            if (requiresPublishPermissions)
            {
                FacebookManager.reauthorizationSucceededEvent -= reauthorizationSucceededEvent;
                FacebookManager.reauthorizationFailedEvent    -= reauthorizationFailedEvent;
            }
        }

        _instance = null;
    }
        #pragma warning restore


    public FacebookAuthHelper(bool requiresPublishPermissions, Action afterAuthAction)
    {
        _instance = this;
        this.requiresPublishPermissions = requiresPublishPermissions;
        this.afterAuthAction            = afterAuthAction;

        // login
        FacebookManager.sessionOpenedEvent += sessionOpenedEvent;
        FacebookManager.loginFailedEvent   += loginFailedEvent;

        // reauth
        if (requiresPublishPermissions)
        {
            FacebookManager.reauthorizationSucceededEvent += reauthorizationSucceededEvent;
            FacebookManager.reauthorizationFailedEvent    += reauthorizationFailedEvent;
        }
    }
	public void cleanup()
	{
		// if the afterAuthAction is not null we have not yet cleaned up
		if( afterAuthAction != null )
		{
			FacebookManager.sessionOpenedEvent -= sessionOpenedEvent;
			FacebookManager.loginFailedEvent -= loginFailedEvent;

			if( requiresPublishPermissions )
			{
				FacebookManager.reauthorizationSucceededEvent -= reauthorizationSucceededEvent;
				FacebookManager.reauthorizationFailedEvent -= reauthorizationFailedEvent;
			}
		}

		_instance = null;
	}
	#pragma warning restore


	public FacebookAuthHelper( bool requiresPublishPermissions, Action afterAuthAction )
	{
		_instance = this;
		this.requiresPublishPermissions = requiresPublishPermissions;
		this.afterAuthAction = afterAuthAction;

		// login
		FacebookManager.sessionOpenedEvent += sessionOpenedEvent;
		FacebookManager.loginFailedEvent += loginFailedEvent;

		// reauth
		if( requiresPublishPermissions )
		{
			FacebookManager.reauthorizationSucceededEvent += reauthorizationSucceededEvent;
			FacebookManager.reauthorizationFailedEvent += reauthorizationFailedEvent;
		}
	}
        public async Task <IHttpActionResult> Register(Awpbs.RegisterWebModel model)
        {
            // temp: changing passwords on some accounts
            //try
            //{
            //    UserManager.ChangePassword("d786aeaf-6035-43eb-afb1-6a8c833f21cc", LoginHelper.BuildPasswordFromEmail_PreAugust2015("*****@*****.**"), LoginHelper.BuildLoginPasswordFromFacebookId("10153286890728926"));
            //    UserManager.ChangePassword("85747d20-d9c6-47b0-939f-b2279c0d9453", LoginHelper.BuildPasswordFromEmail_PreAugust2015("*****@*****.**"), LoginHelper.BuildLoginPasswordFromFacebookId("10102050416755296"));
            //    UserManager.ChangePassword("8958934d-ca86-420d-9116-c4082a59e9c1", LoginHelper.BuildPasswordFromEmail_PreAugust2015("*****@*****.**"), LoginHelper.BuildLoginPasswordFromFacebookId("10155681835300533"));
            //}
            //catch (Exception exc)
            //{
            //    int asdf = 15;
            //}

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // validate facebook's access token, if provided
            bool hasFacebook = string.IsNullOrEmpty(model.FacebookId) == false;
            BasicFacebookProfileInfo facebookProfileInfo = null;

            if (hasFacebook)
            {
                facebookProfileInfo = await FacebookAuthHelper.Validate(model.FacebookAccessToken);

                if (facebookProfileInfo == null)
                {
                    throw new Exception("Couldn't validate Facebook's access token");
                }
            }

            var db    = new ApplicationDbContext();
            var logic = new UserProfileLogic(db);

            // make sure that there is no user by this email in the system
            var existingUsersWithThisEmail = (from i in db.Athletes
                                              where i.RealEmail != null
                                              where i.RealEmail.ToLower() == model.Email.ToLower()
                                              select i).ToList();

            if (existingUsersWithThisEmail.Count > 0)
            {
                throw new Exception("There is already a user with this email");
            }

            // create a user
            var user = new ApplicationUser()
            {
                UserName = model.Email, Email = model.Email
            };
            IdentityResult result = await UserManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }

            // create athlete record
            string nameInFacebook = null;

            if (hasFacebook)
            {
                nameInFacebook = model.Name;
            }
            var athlete = logic.CreateAthleteForUserName(model.Email, model.FacebookId, nameInFacebook);

            logic.UpdateAthleteProfile(athlete.AthleteID, model.Name, (GenderEnum)model.Gender, model.DOB);
            if (hasFacebook && string.IsNullOrEmpty(facebookProfileInfo.EMail) == false)
            {
                logic.UpdateRealEmail(athlete.AthleteID, facebookProfileInfo.EMail);
            }

            // if the password fits the PIN format - use the password as the PIN
            if (new AccessPinHelper().Validate(model.Password))
            {
                logic.SetPin(athlete.AthleteID, model.Password);
            }

            // update picture
            string picture = null;

            if (hasFacebook)
            {
                picture = "http://res.cloudinary.com/bestmybest/image/facebook/" + HttpUtility.UrlEncode(model.FacebookId) + ".jpg";
            }
            if (picture != null)
            {
                logic.UpdateAthletePicture(athlete.AthleteID, picture);
            }

            return(Ok());
        }