2015-04-08 70 views

回答

8

首先,我必須感謝我的朋友幫我解決了cakephp 3中的這個謎題。

我提供了關於如何在cakephp 3中使用插件的完整選項,可能會給出一個解決方案並探索該插件的更多改進。

步驟1:在作曲家

php composer.phar require hybridauth/hybridauth:~2.5.0 

運行這必須在以下路徑安裝插件,

/your-app-folder/vendor/hybridauth/.. 

步驟2:初始化插件。

A.修改config.php文件在以下文件夾,

/your-app-folder/vendor/hybridauth/hybridauth/hybridauth/config.php 

到所需的方法,如添加的應用ID和祕密ID等

$config = array(
      "base_url" => "http://localhost/your-app-folder/users/social_redirect/",//You have to change the above according to yours 

      "providers" => array(
       // openid providers 
       "OpenID" => array(
        "enabled" => true 
       ), 
       "Yahoo" => array(
        "enabled" => true, 
        "keys" => array("key" => "", "secret" => ""), 
       ), 
       "AOL" => array(
        "enabled" => true 
       ), 
       "Google" => array(
        "enabled" => true, 
        "keys" => array("id" => "", "secret" => ""), 
       ), 
       "Facebook" => array(
        "enabled" => true, 
        "keys" => array("id" => "", "secret" => ""), 
        "scope" => "email, user_about_me, user_birthday, user_hometown", 
        "trustForwarded" => false 
       ), 
       "Twitter" => array(
        "enabled" => true, 
        "keys" => array("key" => "", "secret" => "") 
       ), 
       // windows live 
       "Live" => array(
        "enabled" => true, 
        "keys" => array("id" => "", "secret" => "") 
       ), 
       "LinkedIn" => array(
        "enabled" => true, 
        "keys" => array("key" => "", "secret" => "") 
       ), 
       "Foursquare" => array(
        "enabled" => true, 
        "keys" => array("id" => "", "secret" => "") 
       ), 
      ), 
      // If you want to enable logging, set 'debug_mode' to true. 
      // You can also set it to 
      // - "error" To log only error messages. Useful in production 
      // - "info" To log info and error messages (ignore debug messages) 
      "debug_mode" => false, 
      // Path to file writable by the web server. Required if 'debug_mode' is not false 
      "debug_file" => "", 
); 

步驟3 : 現在在你的用戶控制器中,(我用用戶控制器爲http://localhost/your-app-folder/users/social - 根據我的需要)

N流量控制器應該是這樣的,

<?php 

namespace App\Controller; 

use App\Controller\AppController; 

class UsersController extends AppController { 

    public function beforeFilter(\Cake\Event\Event $event) { 
     parent::beforeFilter($event); 
     $this->Auth->allow(['register','social', 'social_redirect']); 
    } 

    public function index() { 
     return $this->redirect(['controller' => 'Users', 'action' => 'add']); 
    } 

    public function social($provider) { 

    /* Include the Config File */ 
    require_once(ROOT . DS . 'vendor' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'config.php'); 
    require_once(ROOT . DS . 'vendor' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'Hybrid' . DS . 'Auth.php'); 

    /* Initiate Hybrid_Auth Function*/ 
    $hybridauth = new \Hybrid_Auth($config); 
    $authProvider = $hybridauth->authenticate($provider); 
    $user_profile = $authProvider->getUserProfile(); 

    /*Modify here as per you needs. This is for demo */ 
    if ($user_profile && isset($user_profile->identifier)) { 
     echo "<b>Name</b> :" . $user_profile->displayName . "<br>"; 
     echo "<b>Profile URL</b> :" . $user_profile->profileURL . "<br>"; 
     echo "<b>Image</b> :" . $user_profile->photoURL . "<br> "; 
     echo "<img src='" . $user_profile->photoURL . "'/><br>"; 
     echo "<b>Email</b> :" . $user_profile->email . "<br>"; 
     echo "<br> <a href='logout.php'>Logout</a>"; 
    } 
    exit; 

    /*Example Demo For FB authorize Action*/ 
    #Facebook authorize 
    if ($this->request->params['pass'][0] == 'Facebook') { 
     if ($user_profile && isset($user_profile->identifier)) { 
      $this->authorize_facebook($user_profile); 
     } 
    } 
} 

public function social_redirect() { 
    $this->layout = false; 
    $this->autoRender = false; 
    require_once(ROOT . DS . 'vendor' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'config.php'); 
    require_once(ROOT . DS . 'vendor' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'Hybrid' . DS . 'Auth.php'); 
    require_once(ROOT . DS . 'vendor' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'Hybrid' . DS . 'Endpoint.php'); 
    $hybridauth = new \Hybrid_Auth($config); 
    \Hybrid_Endpoint::process(); 
} 


public function authorize_facebook($user_profile) { 

     $provider = "Facebook"; 
     $provider_uid = $user_profile->identifier; 

     $userExist = $this->Users->find('all')->where(['Users.provider' => $provider, 'Users.provider_uid' => $user_profile->identifier])->first(); 


     if ((isset($userExist)) && ($userExist)) { 

      $session = $this->request->session(); 
      $session->delete('auth_sess_var'); 
      $session->destroy(); 
      $this->Auth->setUser($userExist->toArray()); 
      $session->write('auth_sess_var', $userExist); 
      return $this->redirect($this->Auth->redirectUrl()); 
     } else { 

      /* Create new user entity */ 
      $user = $this->Users->newEntity(); 
      $tmp_hash = md5(rand(0, 1000)); 
      $tmp_id = time(); 

      /* Save individual data */ 
      $user->tmp_id = $tmp_id; 
      $user->firstname = (!empty($user_profile->firstName)) ? $user_profile->firstName : ""; 
      $user->lastname = (!empty($user_profile->lastName)) ? $user_profile->lastName : ""; 
      $user->username = (!empty($user_profile->lastName) && !empty($user_profile->lastName)) ? strtolower($user_profile->firstName) . "." . strtolower($user_profile->lastName) : ""; 
      $user->avatar = (!empty($user_profile->photoURL)) ? $user_profile->photoURL : ""; 
      $user->role = "public"; 
      $user->provider = $provider; 
      $user->provider_uid = $user_profile->identifier; 
      $user->gender = !empty($user_profile->gender) ? (($user_profile->gender == 'male') ? 'm' : 'f') : ""; 
      $user->provider_email = !empty($user_profile->email) ? $user_profile->email : ""; 
      $user->password = $user_profile->identifier; 
      $user->confirm_password = $user_profile->identifier; 
      $user->tmp_hash = $tmp_hash; 
      $user->isverified = (!empty($user_profile->emailVerified)) ? 1 : 0; 
      $user = $this->Users->patchEntity($user, $this->request->data); 
      $this->Users->save($user); 

      $userDetails = $this->Users->find('all')->where(['Users.provider' => $provider, 'Users.provider_uid' => $user_profile->identifier])->first(); 

      /* Destroy previous session before setting new Session */ 
      $session = $this->request->session(); 
      $session->delete('auth_sess_var'); 
      $session->destroy(); 

      /* Set user */ 
      $this->Auth->setUser($userDetails->toArray()); 
      $session->write('auth_sess_var', $userDetails); 
      return $this->redirect($this->Auth->redirectUrl()); 
     } 
    } 

} 

注:修改的東西,根據您的需求和設計表按你的要求。

步驟4

調用混合AUTH:

For Ex: <a href="https://stackoverflow.com/users/social/Facebook">Facebook<a> 

對於Facebook登錄;

尤里卡。它會像魅力一樣工作。

欲瞭解更多信息,請點擊此處。

編輯2:

例登錄動作(缺省Auth控制)

在應用控制器,

public function initialize() { 
     parent::initialize(); 
     $this->loadComponent('Flash'); 

     /* Authentication */ 
     $this->loadComponent('Auth', [ 
      'authenticate' => [ 
       'Form' => [ 
        'fields' => [ 
         'username' => 'email', 
         'password' => 'password' 
        ] 
       ] 
      ], 
      'loginRedirect' => [ 
       'controller' => 'controller', 
       'action' => 'action' 
      ], 
      'logoutRedirect' => [ 
       'controller' => 'Users', 
       'action' => 'login' 
      ], 
      'loginAction' => [ 
       'controller' => 'Users', 
       'action' => 'login' 
      ] 
     ]); 

    } 
+0

謝謝您的支持。但是,當我在AppController中加載組件時:$ this-> loadComponent('Auth',[ 'authenticate'=> [ 'Form', 'ADmad/HybridAuth.HybridAuth' ] ] 我收到一個錯誤:未找到身份驗證適配器「ADmad/HybridAuth.HybridAuth」。 你能告訴我如何解決它嗎?許多感謝 –

+0

使用默認auth組件進行登錄操作。檢查數據庫中是否存在電子郵件,否則存儲電子郵件地址和提供者ID,然後進行身份驗證。 –

+0

接受我的答案,如果它的作品.. –

1

你讀過這頁嗎? http://miftyisbored.com/complete-social-login-application-tutorial-cakephp-2-3-twitter-facebook-google/

這將有助於你把hybridauth到CakePHP的3.0,但你需要在CakePHP3的方式來改變一些觀點,如:

// config/hybridauth.php 

return [ 
    'HybridAuth' => [ 
    'base_url' => 'URL here', 
    'providers' => [ 
     'Twitter' => [...] 
    ] 
]; 

// src/Controller/Component/HybridauthComponent.php 

// App::import('Vendor', 'hybridauth/Hybrid/Auth'); 
// $this->hybridauth = new Hybrid_Auth($config); 
$this->hybridauth = new \Hybrid_Auth($config); 

此外,檢查此文件。

https://github.com/ADmad/CakePHP-HybridAuth/blob/master/README.md

它說你需要初始化驗證組件,但它並沒有以這種方式工作,所以我把這些選項是這樣的:

// src/Controller/AppController.php 

public function initialize() 
{ 
    $this->loadComponent('Auth', [ 
    'authenticate' => [ 
     'ADmad/HybridAuth.HybridAuth' 
    ], 
    // redirect here if the user not authorized 
    'loginAction' => [ 
     'controller' => 'User', 
     'action' => 'login', 
    ], 
    ]); 
} 
0

我得到錯誤與這些代碼和i相修復它: 錯誤:Hybriauth配置在給定路徑上不存在。

解決方案: 在用戶控制器,你打電話(需要)的config.php

require_once(ROOT . DS . 'vendor' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'config.php'); 

你需要存儲你在變量$配置要求:

$config = require_once(ROOT . DS . 'vendor' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'config.php');