chuoke / laravel-user-identify
Laravel 多标识符认证提供程序,用于简化用户模型/表。
v0.3.0
2024-09-19 14:48 UTC
Requires
- php: ^8.0
- spatie/laravel-package-tools: ^1.16.4
Requires (Dev)
- nunomaduro/collision: ^8.4.0
- orchestra/testbench: ^9.3.0
- pestphp/pest: ^2.35.1
- pestphp/pest-plugin-laravel: ^2.4.0
- vimeo/psalm: ^5.25.0
README
为什么是这个?
通过转换其功能,深入学习和理解Laravel。
如今,登录方式有很多种,特别是社交登录变得非常流行。通常我们会将登录标识符添加到 users
表中,虽然这使得使用此标识信息和访问Laravel的现有逻辑变得简单。但这也使得 users
表变得非常臃肿,这些登录标识符在业务逻辑中通常用处不大。为了简化 users
表和更方便地添加登录方式,我创建了此包。
借助此包,我的 user
表更加简洁。
CREATE TABLE `users` ( `id` bigint unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `avatar` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`) )
将 users
表的 email
、password
、remember_token
和任何社交登录标识符转换为标识凭证表 user_identifiers
。
如果您需要添加其他认证标识符,只需添加一条记录。
安装
目前不可用 !!!!
目前,我只使用了一些基于此包的登录方法,欢迎改进。
您可以通过 composer 安装此包
composer require chuoke/laravel-user-identify
php artisan vendor:publish --provider="Chuoke\UserIdentify\UserIdentifyServiceProvider"
您可以使用以下命令发布和运行迁移
php artisan vendor:publish --tag="user-identify-migrations"
php artisan migrate
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="user-identify-config"
配置
这是发布配置文件的内容
在 config/user-identify.php
return [ 'idetifier_model' => Chuoke\UserIdentify\Models\UserIdentifier::class, 'idetifier_table' => 'user_identifiers', 'idetifier_user_key' => 'user_id', 'user_model' => App\Models\User::class, 'user_key' => 'id', 'auth_provider_name' => 'user_identify', 'actions' => [ 'user_save_from_socialite' => \Chuoke\UserIdentify\Actions\UserSaveFromSocialite::class, ], ];
将 user_identify
守护提供程序添加到 auth.php
配置文件中,如下所示
在 config/auth.php
return [ 'defaults' => [ 'guard' => env('AUTH_GUARD', 'web'), 'passwords' => env('AUTH_PASSWORD_BROKER', 'users'), ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'user_identify', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, ], 'user_identify' => [ 'driver' => config('user-identify.auth_provider_name', 'user_identify'), ], ], // ... ];
接下来,注册认证提供程序
在 AuthServiceProvider.php
use Chuoke\UserIdentify\UserIdentifyProvider; use Illuminate\Contracts\Foundation\Application; class AppServiceProvider extends ServiceProvider { public function boot(): void { Auth::provider(config('user-identify.auth_provider_name'), function (Application $app, array $config) { return new UserIdentifyProvider( $app['hash'], config('user-identify.user_model'), config('user-identify.idetifier_model'), ); }); } }
用法
使用 GitHub 账户登录。
<?php namespace App\Web\Controllers\Auth; use Illuminate\Http\Request; use App\Web\Controllers\Controller; use Illuminate\Support\Facades\Auth; use Laravel\Socialite\Facades\Socialite; class GithubController extends Controller { public function redirect(Request $request) { return Socialite::driver('github')->redirect(); } public function callback(Request $request) { /** @var \Laravel\Socialite\AbstractUser $githubUser */ $githubUser = Socialite::driver('github')->user(); $githubUser['socialite_type'] = 'github'; $successful = Auth::attempt([ 'identifier' => $githubUser, ], true); if ($successful) { return redirect('/dashboard'); } return redirect('/login'); } }
如果用户不存在,则会自动创建。
您可以通过查看源代码了解更多详细信息。
变更日志
请参阅 CHANGELOG 了解最近更改的详细信息。
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。