sathomsen / cakephp-facebook-auth
CakePHP插件,用于通过Facebook令牌进行身份验证
Requires
- php: >=5.5.9
- cakephp/cakephp: >=3.1.0 <4.0.0
- facebook/graph-sdk: ^5.4
- josegonzalez/dotenv: dev-master
Requires (Dev)
- phpunit/phpunit: ^5.7|^6.0
This package is not auto-updated.
Last update: 2024-09-28 20:14:41 UTC
README
简介
此插件允许通过使用CakePHP的认证组件轻松通过Facebook进行授权。插件包括一个AuthComponent认证类以及一个Facebook包装组件。Facebook令牌通过授权头传输到应用程序。
需求
- CakePHP 3.1+
安装
您可以使用 composer 将此插件安装到您的CakePHP应用程序中。
安装composer包的推荐方法是
composer require SAThomsen/FacebookAuth v0.0.2
##配置
引导
该插件通过您的应用程序中的config/boostrap.php文件读取应用程序详情。请将以下行添加到引导中,并填写您的Facebook应用程序详情。字段指定您从图中请求的信息。当在前端生成令牌时,应请求相同的字段。
Configure::write('facebook.appId', 'REPLACE WITH APP ID'); Configure::write('facebook.appSecret', 'REPLACE WITH APP SECRET'); Configure::write('facebook.graphVersion', 'v2.8'); //The api version you intend to use. Configure::write('facebook.fields', 'id,name,first_name,middle_name,last_name,gender,email');
要针对您的Facebook账户运行测试,您必须在引导中输入账户标识符。
Configure::write('facebook.identifier', 'REPLACE WITH FACEBOOK IDENTIFIER'));
您还可以通过在/tests/config中的.env文件中声明环境变量来完成此操作。或者,您可以在本地环境中全局声明它们。我在/tests/config中提供了一个模板。
关联
为了使认证工作,应在表示用户的表中添加一个关联。默认为"Users"。
$this->hasMany('SocialProfiles', [ 'className' => 'SAThomsen/FacebookAuth.SocialProfiles', 'foreignKey' => 'user_id', 'dependent' => true, ]);
组件配置
应将组件添加到AppController的认证配置中。
$this->loadComponent('Auth', [ 'authenticate' => [ 'SAThomsen/FacebookAuth.Facebook', ], ]);
插件开箱即用。以下显示了可用的字段及其默认值
// Specifies the username field of the finder 'fields' => [ 'username' => 'email' ], // Specifies the finder method 'finder' => 'authFB', // Specifies the header used to transfer token 'header' => 'authorization', // specifies the identifying prefix used 'prefix' => 'facebook', // specifies the query parameter to look for in the URL, will be ignored if empty 'parameter' => '', // specifies the exeption to throw 'unauthenticatedException' => '\Cake\Network\Exception\UnauthorizedException', // specifies where to find the finder method 'userModel' => 'SAThomsen/FacebookAuth.SocialProfiles',
如果您不确定配置如何工作,以下是如何配置查找器查找的字段的示例
$this->loadComponent('Auth', [ 'authenticate' => [ 'SAThomsen/FacebookAuth.Facebook' => [ 'fields' => [ 'username' => 'username' ], ], ], ]);
加载Facebook组件
可以通过以下行在控制器中加载Facebook组件。这在添加新用户或将现有用户账户与社会配置文件链接时很有用。
$this->loadComponent('SAThomsen/FacebookAuth.Facebook');
迁移
可以通过迁移文件导入SocialData的数据库模式。
cake migrations migrate -p SAThomsen/FacebookAuth
测试
要成功执行测试,请确保您已通过要测试的应用程序进行认证。此外,您应修改tests/bootstrap.php,使用您的应用程序凭据和您的账户凭据。