Yii-user login with Facebook

Hi guys,

I am working with yii-user this great extension and I’m trying to make it support Facebook login…

Do you guys have any ideas?

I write some codes but it not works… Have any of you used yii-user and Facebook successfully? Can you share some solutions…?

Here are my codes: (i learn from another post them and revise them…,sadly, still not work)

LoginController:


public function actionFacebook(){


        // Make sure the user is logged out

        Yii::app()->user->logout();

        Yii::import('application.modules.user.vendors.facebook.*');

        require_once('facebook.php');

        $facebook = new Facebook(Yii::app()->params['facebookApi'], Yii::app()->params['facebookSecretCode']);

        $fb_user_id = $facebook->get_loggedin_user();

        if($fb_user_id){

            $user = User::model()->findByAttributes(array('facebook_id' => $fb_user_id));

            

            if($user === NULL){

                

                // Save new facebook user to database

                $user = new User;

                $user->facebook_id = $fb_user_id;

                $user->status = User::STATUS_ACTIVE;

                $user->createtime = time();

                $user->lastvisit = time();

                $user->superuser = 0;


                if($user->save(false)){

                    $profile = new Profile;

                    $profile->user_id=$user->id;

                    $profile->save(false);

                }

                

            }


            $identity=new UserIdentity($user->facebook_id, $user->id);

            $identity->authenticate(true);


            switch($identity->errorCode)

            {

                case UserIdentity::ERROR_NONE:

                    $duration= 3600*24*30; // 30 days

                    Yii::app()->user->login($identity,$duration);

                    $this->redirect(Yii::app()->controller->module->returnUrl);

                    

                    break;

                case UserIdentity::ERROR_STATUS_NOTACTIV:

                    $user->addError("status",UserModule::t("You account is not activated."));

                    break;

                case UserIdentity::ERROR_STATUS_BAN:

                    $user->addError("status",UserModule::t("You account is blocked."));

                    break;

                case UserIdentity::ERROR_PASSWORD_INVALID:

                    $user->addError("password",UserModule::t("Password is incorrect."));

                    break;

            }


            $this->render('facebook', array('user' => $user));


        } 

        else

        {

            $this->redirect(Yii::app()->controller->module->returnLogoutUrl);

        }


        

  }



Login view:


<?php

/*

 if(Yii::app()->user->isGuest):?>

    <div class="facebook-login">

        <?php 

 $facebook = new Facebook(Yii::app()->params['facebookApi'], Yii::app()->params['facebookSecretCode']);

$fb_user = $facebook->get_loggedin_user();

if($fb_user === NULL):

?>

        <div class="login_sector_fb">

            <div class="login_prompt">Or <b>login</b> with Facebook:</div>

            <fb:login-button onlogin="facebook_onlogin_ready();"></fb:login-button>

        </div>

        <?php else:?>

            <fb:profile-pic uid="<?php echo $fb_user;?>" size="square" facebook-logo="true"></fb:profile-pic>

            <?php CHtml::link('Logout('.Yii::app()->user->name.')', Yii::app()->getModule('user')->logoutUrl)?>

        <?php endif;?>

    </div>

<?php endif;?>

<div class="clear"></div>

<?php

$login_url = 'xxxxxxxxxxxxx'; //simply my website address

Yii::app()->clientScript->registerScript(

   'facebook_onligin_ready',

   'function facebook_onlogin_ready() {

        window.location = "'.$login_url.'";

    }',

   CClientScript::POS_END

);

*/

?>

Here is some solutions… Has anyone used one of them to integrate with yii-user????

http://www.yiiframework.com/forum/index.php/topic/6372-creating-facebook-connect/page__p__43783#entry43783

Here is a extension… but it’s not safe… anyone can login with some urls…

also, not supported for database…(seems like)

Any ideas?

What I want to do is let the user login with their Facebook account and store their facebook_id at their first time.

…Thanks for help!!!

Thanks!!!!!!

Hi

can you submit error ? otherwise

make sure file path for this —> require_once(‘facebook.php’); or require_once(‘facebook/facebook.php’);

Thanks

Everytime, if i add the according codes in my view, I got blank page or some page without css, that means some errors happen in my view/controller.

I am following this people’s post… and I revised his typos also…Here are just some of my codes

http://www.yiiframework.com/forum/index.php/topic/6372-creating-facebook-connect/page__p__43783#entry43783

Author is mech7

I think i have make sure the file path for facebook.php…I tried /var/www/… and also tried facebook/facebook.php… I don’t think there should be any problems with this.

Hi

can you check please response ?

$facebook = new Facebook(Yii::app()->params[‘facebookApi’], Yii::app()->params[‘facebookSecretCode’]);

print_r($facebook);

Thanks