it-devgroup / laravel-user-oauth2-attach
Laravel用户从社交网络通过OAuth2进行关联
1.0.2
2021-05-21 12:36 UTC
Requires
- php: ^7.4|^8.0
- illuminate/database: ^7.0|^8.0
- illuminate/support: ^7.0|^8.0
- nesbot/carbon: ^1.0|^2.0
Suggests
- facebook/graph-sdk: For working OAuth2 Facebook (tested for version ^5.7)
- google/apiclient: For working OAuth2 google (tested for version ^2.9)
README
描述
使用Auth2将社交网络资料库的资料加入网站。可用于实现通过社交网络的授权。支持的服务:谷歌和Facebook。
Lumen安装
1. 打开文件 bootstrap/app.php
取消注释字符串
$app->withFacades();
$app->withEloquent();
在 $app->configure('app'); 后添加
$app->configure('user_oauth2_attach');
添加新的服务提供者
$app->register(\ItDevgroup\LaravelUserOAuth2Attach\Providers\UserOAuth2AttachServiceProvider::class);
2. 运行命令
创建配置文件
php artisan user:oauth2:attach:publish --tag=config
创建迁移文件
php artisan user:oauth2:attach:publish --tag=migration
生成表
php artisan migrate
Laravel安装
1. 打开文件 config/app.php 并搜索
'providers' => [
...
]
添加到部分
\ItDevgroup\LaravelUserOAuth2Attach\Providers\UserOAuth2AttachServiceProvider::class,
示例
'providers' => [
...
\ItDevgroup\LaravelUserOAuth2Attach\Providers\UserOAuth2AttachServiceProvider::class,
]
2. 运行命令
创建配置文件
php artisan vendor:publish --provider="ItDevgroup\LaravelUserOAuth2Attach\Providers\UserOAuth2AttachServiceProvider" --tag=config
创建迁移文件
php artisan user:oauth2:attach:publish --tag=migration
生成表
php artisan migrate
自定义模型
步骤1
创建用户OAuth2的自定义模型
示例
文件: app/CustomFile.php
内容
<?php
namespace App;
class CustomFile extends \ItDevgroup\LaravelUserOAuth2Attach\Model\UserOAuth2
{
}
如果需要更改表名或需要添加其他代码
<?php
namespace App;
class CustomFile extends \ItDevgroup\LaravelUserOAuth2Attach\Model\UserOAuth2
{
protected $table = 'YOUR_TABLE_NAME';
// other code
}
步骤2
打开 config/user_oauth2_attach.php 并更改参数 "model",例如
...
// replace
'model' => \ItDevgroup\LaravelUserOAuth2Attach\Model\UserOAuth2::class,
// to
'model' => \App\CustomFile::class,
步骤3
在所有地方使用自定义模型 \App\CustomFile,而不是标准模型 \ItDevgroup\LaravelUserOAuth2Attach\Model\UserOAuth2
自定义通过OAuth2登录的社交网络服务
- 创建自定义类
示例
文件: app/CustomSocialFile.php
内容
<?php
namespace App;
use ItDevgroup\LaravelUserOAuth2Attach\Data\UserData;
use Illuminate\Support\Collection;
class CustomSocialFile implements ServiceInterface
{
/**
* @return string
*/
public function getLoginUrl(): string
{
return 'http://';
}
/**
* @param Collection $data
* @return UserData
*/
public function authorize(Collection $data): UserData
{
return UserData::register(...);
}
}
- 在配置文件 config/user_oauth2_attach.php 的 "services" 部分添加新的社交网络服务
自定义用户模型(可选)
如果配置了,则在方法 getOrCreateModelFromData 中禁用标准电子邮件搜索。
在用户模型中,您需要添加一个公共方法 userOAuth2Find 并返回用户对象或null。
class User {
...
public function userOAuth2Find(UserData $userData)
{
return self::query()
->where('email', '=', $userData->getEmail())
->first();
}
}
用法
初始化服务
$service = app(\ItDevgroup\LaravelUserOAuth2Attach\UserOAuth2AttachServiceInterface::class);
或注入
// use
use ItDevgroup\LaravelUserOAuth2Attach\UserOAuth2AttachServiceInterface;
// constructor
public function __construct(
UserOAuth2AttachServiceInterface $userOAuth2AttachService
)
我们将使用变量 $service
登录活动的服务列表
// $service->getServiceList(): array
$array = $service->getServiceList();
初始化社交网络服务
// $service->loadService(string $service): void
$service->loadService('google');
获取OAuth2登录的URL
// $service->getLoginLink(): string
$url = $service->getLoginLink();
通过OAuth2授权用户(在获取登录URL之后)
// $service->authorize(Collection $data): UserData
$userData = $service->authorize(collect($request->all()));
从数据中获取或创建用户模型
$newUser - 新的完全填充的用户实体但未保存。
// $service->getOrCreateModelFromData(UserData $userData, Model $newUser): UserOAuth2
$userOAuth2 = $service->getOrCreateModelFromData($userData, $newUser);
CRUD方法
// $service->getModelListByUser(int $userId): CollectionEloquent
$eloquentCollection = $service->getModelListByUser(1);
// $service->getModelById(int $id): UserOAuth2
$userOAuth2 = $service->getModelById(1);
// $service->getModelByUserAndService(int $userId, string $service): UserOAuth2
$userOAuth2 = $service->getModelByUserAndService(1, 'google');
// $service->getModelByServiceAndExternalId(string $service, string $externalId): UserOAuth2
$userOAuth2 = $service->getModelByServiceAndExternalId('google', 'abcd...');
// $service->modelCreate(UserOAuth2 $model): bool
$service->modelCreate($userOAuth2);
// $service->modelUpdate(UserOAuth2 $model): bool
$service->modelUpdate($userOAuth2);
// $service->modelDelete(UserOAuth2 $model): bool
$service->modelDelete($userOAuth2);
用户OAuth2模型创建
$userOAuth2 = \ItDevgroup\LaravelUserOAuth2Attach\Model\UserOAuth2::register(
$user, // User model
'google',
$userData->getId(),
$userData->getEmail(),
$userData->getPhone(),
$userData->getFirstName(),
$userData->getLastName(),
$userData->getGender(),
$userData->getLink(),
$userData->getProperties()
);
基本用法
获取重定向到社交网络登录页面的URL
$service->loadService('google');
$url = $service->getLoginLink();
授权社交网络并获取令牌
使用上述链接进行授权,并在从社交网络重定向到网站之后
$service->loadService('google');
// $request - Request class
$userData = $service->authorize(collect($request->all()));
$newUser = new User();
$newUser->email = $userData->getEmail();
$userOAuth2 = $service->getOrCreateModelFromData($userData, $newUser);
$user = $userOAuth2->user;