Creating Facebook Connect

Hi, I won’t be doing an extension for Yii, You can just use this one with the documentation above to have the same result.

http://www.vadimg.com/2010/05/05/facebook-php-library-graph-social-plugins-search-posting-and-more/

Hi There, I am using the new api, so in HFacebook.php I use:

    $fb_uid=$controller->facebook->getUser();

but I am not getting a session loaded… can you help?

Hi there, I am having troubles with Facebook Connect -

it seams it must be a cookie issue - have you ever had the problem where fb connect window is not accepting your password even though your password is being entered correctly? Please let me know if you have any insights on this! Thanks!!!

Hi,

Thank you for sharing your code, mech7. I’m using it as a guide for now and substituting the FB things using the library made by Vince (thank you too for your lib! Appreciated!).

I have a question. In the LoginController.php/LogoutController.php/UserIdentity.php files I see you’re instantiating an ‘User’ object model class. The thing is, shouldn’t we use ‘YumUser’ instead?

Am I missing something?

One thing that came to my mind was to create the User model class extending YumUser.

I’m going to test this extending-model-class thing anyway to see if it works.

EDIT: Question answered. Indeed, you’re extending YumUser. Sorry for the newbie’s Q. It happens. Often. :)

Vince, I made a little modification to your lib. In line 36, I modified the path this way:




require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'facebook.php');



This way I can put your library under Yii:app()->baseUrl . ‘/vendors/’ and then I can import it into Yii:




<?php	Yii::import('application.vendors.facebook.facebookLib'); ?>



Well, those were my today’s two cents. Thank you for your lib.

And now, the feature request. Is it possible you make facebookLib::includeScript() to receive an array in a ‘key’=>‘value’ fashion instead of the way you’re doing it right now?

hi everybody…!!

i just want some different situation here…which is probably name as disconnect facebook.

after a successful login with facebook. user is get registered in my app. and redirect to authenticate page.which is only accessible by logged in user.

Everything is working fine here.but now i want to disconnect user from facebook and make him alive on my application.so that still he/she can access my application.

I am getting some problem here regarding cookies with yii.

my problem are listed here :-

(1) when a user click on log out button.still he is logged in my application.

(2) when a user click on a facebook disconnect button.then he will be logged out from facebook.but still showing connected on my app.

does anybody can help me here… even i am getting stuck with a authentication process of yii…

thanks in advance…

Dear all,

I bumped into this thread while researching for Yii support for Facebook authentication.

I ended up coding my own solution based on the OAuth 2.0 protocol and I would like to share.

This solution relies solely on Facebook authentication (no database) but it can serve as base for extensions.

It assumes the login route is ‘site/login’ provided through an https connection.

How to use it:

[list=1]

[*] Configure loginUrl and the Facebook properties in the main configuration file (you can set ‘scope’ to request more permissions).




'components'=>array(

	'user'=>array(

		'loginUrl'=>array('site/login'),

	),

	'facebook'=>array(

		'class'=>'FacebookComponent',

		'appId'=>YOUR_APP_ID,

		'appSecret'=>YOUR_APP_SECRET,

		// 'scope'=>'email',

		'optionsJS'=>array(

			'xfbml'=>true,

			'oauth'=>true,

			'cookie'=>true,

		),

	),

),



[*] Include the FacebookOAuthAction in your SiteController.




public function actions()

{

	return array(

		'login'=>array(

			'class'=>'FacebookOAuthAction',

		),

	);

}



[*] Include the FacebookLoginButton widget in your login view.




<?php $this->widget('FacebookLoginButton',array(

	'caption'=>'Login with Facebook',

	'htmlOptions'=>array('data-size'=>'large'),

)); ?>



[/list]

And here follow the required components:

[list=1]

[*] The FacebookComponent class (requires the Facebook PHP SDK).




require(dirname(__FILE__).DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR.'facebook.php');


class FacebookComponent extends CApplicationComponent

{

	public $appId;

	public $appSecret;

	public $language='';

	public $scope='';

	public $optionsJS=array();


	public function init()

	{

		if(!isset($this->optionsJS['appId'])) $this->optionsJS['appId']=$this->appId;

	}


	public function initJS()

	{

		$cs=Yii::app()->clientScript;

		$cs->registerScript('facebook','

			window.fbAsyncInit=function(){

				FB.init('.CJavaScript::encode($this->optionsJS).');

			};

			(function(){

				var d = document.createElement("div");

				d.id = "fb-root";

				var b = document.getElementsByTagName("body")[0];

				b.insertBefore(d, b.firstChild);

				var e = document.createElement("script");

				e.type = "text/javascript";

				e.src = document.location.protocol+"//connect.facebook.net/'.$this->locale.'/all.js";

				e.async = true;

				d.appendChild(e);

			}());

		',CClientScript::POS_BEGIN);

	}


	protected function getLocale()

	{

		$language=$this->language;

		if(empty($language)) $language=Yii::app()->language;

		switch($language)

		{

		case 'de': return 'de_DE';

		case 'es': return 'es_ES';

		case 'fr': return 'fr_FR';

		case 'it': return 'it_IT';

		case 'ja': return 'ja_JP';

		case 'pt': return 'pt_PT';

		case 'pt_br': return 'pt_BR';

		case 'ru': return 'ru_RU';

		case 'sv': return 'sv_SE';

		default: return 'en_US';

		}

	}


	public function getRedirectUri()

	{

		$redirectUri=Yii::app()->user->loginUrl;

		$route=$redirectUri[0];

		$params=array_splice($redirectUri,1);

		return Yii::app()->createAbsoluteUrl($route,$params,'https');

	}


	public function getUserObject()

	{

		try

		{

			$fb=new Facebook(array(

				'appId'=>$this->appId,

				'secret'=>$this->appSecret,

			));

			return $fb->api('/me');

		}

		catch(Exception $e)

		{

			return false;

		}

	}

}



[*] The FacebookLoginButton widget.




class FacebookLoginButton extends CWidget

{

	public $caption='';

	public $htmlOptions=array();


	public function init()

	{

		$fb=Yii::app()->facebook;

		$fb->initJS();


		if(!isset($this->htmlOptions['class'])) $this->htmlOptions['class']='fb-login-button';

		if(!isset($this->htmlOptions['data-scope'])) $this->htmlOptions['data-scope']=$fb->scope;

		if(!isset($this->htmlOptions['data-onlogin']))

		{

			$cs=Yii::app()->clientScript;

			$cs->registerScript($this->id,'

				function flb_'.$this->id.'() {

					FB.getLoginStatus(function(response) {

						if (response.authResponse) {

							location.replace("'.$fb->redirectUri.'&access_token="+response.authResponse.accessToken);

						}

					});

				}

			',CClientScript::POS_BEGIN);

			$this->htmlOptions['data-onlogin']='flb_'.$this->id.'()';

		}

	}


	public function run()

	{

		echo CHtml::tag('div',$this->htmlOptions,$this->caption);

	}

}



[*] The FacebookOAuthAction class.




class FacebookOAuthAction extends CAction

{

	public function run()

	{

		$user=Yii::app()->user;

		$identity=new FacebookUserIdentity();

		if($identity->authenticate())

		{

			$user->login($identity);

			$this->controller->redirect($user->returnUrl);

		}

		$this->controller->render('login');

	}

}



[*] The FacebookUserIdentity class.




class FacebookUserIdentity extends CUserIdentity

{

	private $_id;


	public function getId()

	{

		return $this->_id;

	}


	public function authenticate()

	{

		$fb=Yii::app()->facebook;

		$userObject=$fb->userObject;

		if($userObject===false)

		{

			$this->errorMessage='Unable to authenticate user on Facebook.';

			return false;

		}

		$this->_id=$userObject['id'];

		$this->username=$userObject['name'];

		$this->errorCode=self::ERROR_NONE;

		return true;

	}

}



[/list]

it’s not working…

I tried your solution but it not works… any ideas…?

How to download facebook client?

what is facebook client? is it facebook-php-sdk???

and where to place HFacebook.php file?