How to use app facebook login with cakephp

PHP TECHNOLOGY

Posted on 2016-12-01 03:27:51


1. You must create app facebook at https://developers.facebook.com/ You can click menu Apps then create a new app. 

2. Import folder facebook to vendor folder of cakephp You can download file zip facebook at facebook then you extract it and copy folder facebook to vendor of project. 

3. In controller, you need write function for facebook login

 /* Hainq: login with facebook { */
 function fb_login()
 {
 $user = $this->facebook->getUser();
 if($user)
 {
 try 
 {
 // Proceed knowing you have a logged in user who's authenticated.
 $user = $this->facebook->api('/me');
 } 
 catch (FacebookApiException $e) 
 {
 error_log($e.'==');
 $user = null;
 }
 }
 
 if(!empty($user))
 {
 // check email exist 
 // Not exist => create new & get info from DB => save session 'MemberInfo'
 // Exist FB => get info from DB => save session 'MemberInfo'
 // Exist H1 => not allow to login using facebook => show message ...
 
 //$facebook_token = $this->facebook->getAccessToken();
 // Member
 
 // $auth_token = $id + 'member_key' + datetime login => update DB for API use
 $member = $this->Member->find('first', array('conditions'=>array('email'=>$user['email'])));
 if(!empty($member))
 {
 if($member['Member']['type'] == 'H1')
 {
 //Exist H1 => not allow to login using facebook
 $this->flashWarning(__('Member account type cannot be login using Facebook'));
 $this->referer();
 }
 else if($member['Member']['type'] == 'FB')
 {
 // Exist FB
 if($member['Member']['active'])
 {
 // Create login key
 $loginTime = date("Y-m-d H:i:s");
 $authToken = $member['Member']['id'] . "member_key" . $loginTime;
 $authToken = sha1($authToken); 
 // ----------------
// update login info
 $this->Member->updateAll(array('Member.auth_token'=>"'$authToken'"),array('Member.id'=>$member['Member']['id']));
 $memberAfterUpdate = $this->Member->find('first', array('conditions'=>array('email'=>$member['Member']['email'])));
 $this->Session->write('MemberInfo', $memberAfterUpdate['Member']);
 $this->redirect(array('controller'=>'pages', 'action'=>'index'));
 }
 else
 {
 $this->flashWarning(__('This member is currently disabled'));
 $this->referer();
 }
 }
 else
 {
 die('Never go here');
 }
 }
 else
 {
 $birthdate = date('Y-m-d',strtotime($user['birthday']));
 $memberData = array(
 'type'=>'FB',
 'email'=>$user['email'],
 'first_name'=>$user['first_name'],
 'last_name'=>$user['last_name'],
 'gender'=>($user['gender'] == 'male') ? 1 : 0,
 'birthdate'=>$birthdate,
 'auth_token' => sha1('$id' + 'member_key' + date('Y-m-d H:i:s')),
 'active'=>true
 
 );
 //debug($memberData); die;
 $this->Member->create();
 if($this->Member->save($memberData))
 {
 $idMember = $this->Member->field('id', array("id = (Select max(id) From members)"));
 $memberAfterSave = $this->Member->find('first', array('conditions'=>array('id'=>$idMember)));
 $this->Session->write('MemberInfo', $memberAfterSave['Member']);
 $this->redirect(array('controller'=>'pages', 'action'=>'index')); 
 }
 else
 {
 $this->flashWarning(__('Cannot create member account'));
 $this->referer();
 }
 
 }
 }
 else
 {
 $params = array
 (
 'scope' => array('email', 'public_profile', 'user_birthday'),
 'redirect_uri' => Router::url('/',true).'members/fb_login'
 );
 $loginUrl = $this->facebook->getLoginUrl($params);
 $this->redirect($loginUrl);
 }
 }
 /* Hainq: login with facebook } */

4. In view, you can see example:

<a href="<?php echo $this->Html->webroot('/', true).'members/fb_login';?>"><?php echo $this->Html->image('frontend/slide4/fb.png', array('id'=>'facebook')); ?></a>

5. I have done for login with facebook on cakephp.

 If you have problem you can write comment at here.

I will take care it.