Social login is a very popular and quick way to authenticate , And user prefers to login/signup with social sites rather than custom login forms . so every one should have social login button . I was looking for any solution to enable login with Facebook . So i customized my user module to login with Facebook without any extension . for this you need to add a field to store facebook id in your database . So update your user table.

ALTER TABLE `tbl_users` ADD `facebook_id` VARCHAR(50) NOT NULL AFTER `password`;

now you need to add Facebook cdn on your page . Add the given code in your head tag


<div id="fb-root"></div>
<script type="text/javascript" src="//connect.facebook.net/en_US/all.js"></script>

Now you need to add a facebook login/signup button in login and register form

<a href="#" class="login_fb" onclick="fb_login('facebook app id')">Login with facebook</a>
<a href="#" class="login_fb" onclick="fb_login('facebook app id')">Sign up with facebook</a>

Now we need to create a function fb_login which will return the facebook data need to login or register .

function fb_login(fb_id)
{	
    if(fb_id != "")
    {
        
        FB.init({
              appId      : fb_id,                        // App ID from the app dashboard
              status     : true, 
              cookie	 : true,                                // Check Facebook Login status
              xfbml      : true                                  // Look for social plugins on the page
            });
         FB.login(function(response) {
                 if (response.authResponse) {
                     FB.api('/me?fields=email,name,gender,first_name,last_name,birthday,bio', function(response) {
                         console.log(response);
                      
                        sociallogin(response);
                     });
                   } 
         }, {scope: 'public_profile,email,user_likes,user_birthday,user_location,user_about_me'});
    }
}

// make an ajax call to authenticate 
function sociallogin(response)
{
	var url=base_url+'/user/login/fblogin/';
	ajaxcall(url, response, function(output) 
	{
		var data=JSON.parse(output);
		if(data.status=='register')
			window.location.href = data.redirect; //
		else if(data.status=='login')
			window.location.href = data.redirect;
		else
			displayflash('error','some error to process your request');	
	});
}

Now make an function in your login controller in User module

public function actionFblogin()
{
	if(isset($_POST['email']) && isset($_POST['id']))
	{
		$fbid=$_POST['id'];
		$email=$_POST['email'];
		$fbuser=$this->getuserbyattribute(array('email'=>$email));
		if($fbuser)
		{
			if(!$fbuser->facebook_id)
			{
				$fbuser->facebook_id=$fbid;
				$fbuser->save();
			}
			$identity=new UserIdentity($fbuser->email,$fbuser->facebook_id);
			$identity->authenticate('facebook');
			if($login=Yii::app()->user->login($identity,0))
			echo json_encode(array('status'=>'login','redirect'=>$this->createUrl('/home')));
			else
			echo json_encode(array('status'=>'error'));
		}else
		{
			$fistname=$_POST['first_name'];   $lastname=$_POST['last_name']; 
			$gender=$_POST['gender'];
			$model=array('email'=>$email,'gender'=>$gender,'facebook_id'=>$fbid);
			$profile=array('firstname'=>$fistname,'lastname'=>$lastname);
			Yii::app()->user->setState("fbregister",array('profile'=>$profile,'user'=>$model));
			echo json_encode(array('status'=>'register','redirect'=>$this->createurl('user/registeration')));
		}
	}
}

After authenticate if user exists with same email then system will login else it will redirect to the register page with data in the session so that the form will autofill.
case 1) if user found
If user found in the system then you need to login user . For this you need to change your authenticate() method. file path “protected/modules/user/UserIdentity.php ”

public function authenticate($type=null)
{
	//~ if (strpos($this->username,"@")) {
	$user=User::model()->notsafe()->findByAttributes(array('email'=>$this->username));
	//~ } else {
		//~ $user=User::model()->notsafe()->findByAttributes(array('username'=>$this->username));
	//~ }
	if($type)
	{
		if($user===null)
		//~ if (strpos($this->username,"@")) {
		$this->errorCode=self::ERROR_EMAIL_INVALID;
		else if($this->password!==$user->facebook_id)
		$this->errorCode=self::ERROR_PASSWORD_INVALID;
		else {
			$this->_id=$user->id;
			$this->username=$user->username;
			$this->errorCode=self::ERROR_NONE;
		}
	}else
	{
		if($user===null)
			//~ if (strpos($this->username,"@")) {
				$this->errorCode=self::ERROR_EMAIL_INVALID;
			//~ } else {
				//~ $this->errorCode=self::ERROR_USERNAME_INVALID;
			//~ }
		else if(Yii::app()->getModule('user')->encrypting($this->password)!==$user->password)
			$this->errorCode=self::ERROR_PASSWORD_INVALID;
		else if($user->status==0&&Yii::app()->getModule('user')->loginNotActiv==false)
			$this->errorCode=self::ERROR_STATUS_NOTACTIV;
		else if($user->status==-1)
			$this->errorCode=self::ERROR_STATUS_BAN;
		else {
			$this->_id=$user->id;
			$this->username=$user->username;
			$this->errorCode=self::ERROR_NONE;
		}
	}
	return !$this->errorCode;
}

Case 2) user not found
if user not found you need to redirect back to register page with auto filled facebook data.Edit you register function . file location “protected/modules/user/controller/RegistrationController.php”

public function actionRegistration() {
	$model = new RegistrationForm;
	$profile=new Profile;
	$profile->regMode = true;
	// check if session set
	if(Yii::app()->user->hasState("fbregister"))
	{
		$fbdata=Yii::app()->user->getState("fbregister");
		$profile->attributes=$fbdata['user'];
		$model->attributes=$fbdata['user'];
	}
	// other code
	
	// other code
}
http://codingbin.com/wp-content/uploads/2015/12/Untitled-1-copy3.jpghttp://codingbin.com/wp-content/uploads/2015/12/Untitled-1-copy3-150x150.jpgdevidframeworksphpYiiSocial login is a very popular and quick way to authenticate , And user prefers to login/signup with social sites rather than custom login forms . so every one should have social login button . I was looking for any solution to enable login with Facebook . So i...coding, php, wordpress, yii, cakephp, and  jquery

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request