SHARE
TWEET

Untitled

rodrigogaster Jun 14th, 2017 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace App\Controller;
  4. use Cake\Routing\Router;
  5. use Cake\Utility\Security;
  6. use Facebook\Exceptions\FacebookSDKException;
  7. use Facebook\Facebook;
  8.  
  9. class LoginFacebookController extends AppController
  10. {
  11.     public $autoRender = false;
  12.     public $layout = false;
  13.     private $appID = '1941518196096276';
  14.     private $appSecret = 'cf188c4dca7061a86709c17e4d3a4a19';
  15.     private $fbObject;
  16.  
  17.     public function initialize()
  18.     {
  19.         parent::initialize();
  20.         $this->request->session()->start();
  21.         $this->fbObject = new Facebook(
  22.             [
  23.                 'app_id' => $this->appID,
  24.                 'app_secret' => $this->appSecret,
  25.                 'default_graph_version' => 'v2.5',
  26.             ]
  27.         );
  28.         $this->Auth->allow(['auth', 'callback']);
  29.     }
  30.     public function auth()
  31.     {
  32.         $helper = $this->fbObject->getRedirectLoginHelper();
  33.         $permissions = ['email'];
  34.         $loginUrl = $helper->getLoginUrl(
  35.             Router::url(['action' => 'callback'], true),
  36.             $permissions
  37.         );
  38.         $this->redirect($loginUrl);
  39.     }
  40.  
  41.     public function callback()
  42.     {
  43.         $helper = $this->fbObject->getRedirectLoginHelper();
  44.         try
  45.         {
  46.             $accessToken = $helper->getAccessToken();
  47.             $response = $this->fbObject->get('/me?fields=id,name,first_name,last_name,email', $accessToken);
  48.             $fb_user = $response->getGraphUser();
  49.             debug($fb_user); exit();
  50.             $this->loadModel('Users');
  51.             $user = $this->Users->findByEmail($fb_user['email'])->first();
  52.             if(empty($user))
  53.             {
  54.                 $password = Security::hash(time(), 'sha1', true);
  55.                 $data = [
  56.                     'email' => $fb_user['email'],
  57.                     'password' => $password,
  58.                     'fname' => $fb_user['first_name'],
  59.                     'lname' => $fb_user['last_name']
  60.                 ];
  61.                 $user = $this->Users->newEntity($data);
  62.                 $this->Users->save($user);
  63.                 $this->Auth->setUser($data);
  64.                 unset($data['password']);
  65.                 $this->redirect($this->Auth->redirectUrl());
  66.             }else{
  67.                 $data = [
  68.                     'email' => $fb_user['email'],
  69.                     'fname' => $fb_user['first_name']
  70.                 ];
  71.                 $this->Auth->setUser($data);
  72.                 $this->request->session()->delete("Auth.User.password");
  73.                 $this->redirect($this->Auth->redirectUrl());
  74.             }
  75.         }
  76.         catch (FacebookSDKException $e)
  77.         {
  78.             throw new FacebookSDKException( sprintf("No access Token: %s", $e->getMessage()));
  79.         }
  80.     }
  81. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top