如何将hybridauth插件与CakePHP 3.x一起使用?
我使用cakephp3.x创建一个可以进行社交登录的页面。我发现HybridAuth插件可以做到这一点。但是,我不能理解配置和流程。谁习惯于使用这个插件如何将hybridauth插件与CakePHP 3.x一起使用?,php,cakephp-3.0,hybridauth,Php,Cakephp 3.0,Hybridauth,我使用cakephp3.x创建一个可以进行社交登录的页面。我发现HybridAuth插件可以做到这一点。但是,我不能理解配置和流程。谁习惯于使用这个插件 请帮帮我。你读过这一页了吗? 这将有助于您将hybridauth放入CakePHP3.0,但您需要以CakePHP3的方式更改某些点,如: // config/hybridauth.php return [ 'HybridAuth' => [ 'base_url' => 'URL here', 'provid
请帮帮我。你读过这一页了吗? 这将有助于您将hybridauth放入CakePHP3.0,但您需要以CakePHP3的方式更改某些点,如:
// config/hybridauth.php
return [
'HybridAuth' => [
'base_url' => 'URL here',
'providers' => [
'Twitter' => [...]
]
];
及
此外,请检查此文档
它说您需要初始化Auth组件,但它不是这样工作的,所以我将这些选项设置为:
// 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',
],
]);
}
你读过这一页了吗? 这将有助于您将hybridauth放入CakePHP3.0,但您需要以CakePHP3的方式更改某些点,如:
// config/hybridauth.php
return [
'HybridAuth' => [
'base_url' => 'URL here',
'providers' => [
'Twitter' => [...]
]
];
及
此外,请检查此文档
它说您需要初始化Auth组件,但它不是这样工作的,所以我将这些选项设置为:
// 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',
],
]);
}
首先,我必须感谢我的朋友帮助我用CakePHP3解决了这个谜 我提供了如何在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" => "",
);
第三步:
现在,在您的用户控制器中,(我已使用用户控制器,以满足我的需要)
现在你的控制器应该是这样的
<?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());
}
}
}
首先,我必须感谢我的朋友帮助我用CakePHP3解决了这个谜 我提供了如何在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" => "",
);
第三步:
现在,在您的用户控制器中,(我已使用用户控制器,以满足我的需要)
现在你的控制器应该是这样的
<?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());
}
}
}
我在这些代码中出错,并使用以下方法修复了它: 错误:Hybriauth配置在给定路径上不存在 解决方案: 在用户控制器中调用(require)config.php
require_once(ROOT . DS . 'vendor' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'config.php');
您需要在变量$config中存储所需的内容:
$config = require_once(ROOT . DS . 'vendor' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'config.php');
我在这些代码中出错,并使用以下方法修复了它: 错误:Hybriauth配置在给定路径上不存在 解决方案: 在用户控制器中调用(require)config.php
require_once(ROOT . DS . 'vendor' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'config.php');
您需要在变量$config中存储所需的内容:
$config = require_once(ROOT . DS . 'vendor' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'hybridauth' . DS . 'config.php');
谢谢你的支持。但当我在AppController中加载组件时:$this->loadComponent('Auth',['authenticate'=>Form','ADmad/HybridAuth.HybridAuth']);我得到一个错误:未找到身份验证适配器“ADmad/HybridAuth.HybridAuth”。你能告诉我怎么解决吗?许多用户使用默认的身份验证组件进行登录操作。检查数据库中是否存在该电子邮件,否则请存储电子邮件地址和提供商id,然后进行身份验证。您可以将您的示例项目发送给我吗?抱歉@Luc Le我无法,因为我已集成到机密项目中。我会把它分开,然后把文件寄给你。但是我需要一些时间。由于给定路径上不存在Hybriauth配置,因此出现错误。感谢您的支持。但当我在AppController中加载组件时:$this->loadComponent('Auth',['authenticate'=>Form','ADmad/HybridAuth.HybridAuth']);我得到一个错误:未找到身份验证适配器“ADmad/HybridAuth.HybridAuth”。你能告诉我怎么解决吗?许多用户使用默认的身份验证组件进行登录操作。检查数据库中是否存在该电子邮件,否则请存储电子邮件地址和提供商id,然后进行身份验证。您可以将您的示例项目发送给我吗?抱歉@Luc Le我无法,因为我已集成到机密项目中。我会把它分开,然后把文件寄给你。但我需要一些时间。由于给定路径上不存在Hybriauth配置,因此出现错误。