audunru / social-accounts
为您的 Laravel 应用添加社交登录
Requires
- php: ^8.2
- laravel/framework: ^11.0
- laravel/socialite: ^5.0
- spatie/laravel-package-tools: ^1.9
Requires (Dev)
- fakerphp/faker: ^1.12
- friendsofphp/php-cs-fixer: ^3.0
- mockery/mockery: ^1.3
- orchestra/testbench: ^9.0
- php-coveralls/php-coveralls: ^2.2
- phpmd/phpmd: ^2.10
- phpunit/phpunit: ^11.0
- roave/security-advisories: dev-latest
This package is auto-updated.
Last update: 2024-09-25 07:30:48 UTC
README
将社交登录(Google、Facebook 等)添加到您的 Laravel 应用中。
此包使用 Laravel Socialite 进行用户认证,并负责将提供者(例如 Google)和提供者用户 ID(例如 123456789)存储为 SocialAccount(User 模型的相关模型)。
您的用户可以为他们的账户添加一个或多个社交登录。您可以选择他们是否首先使用普通用户名和密码注册,或者他们是否可以通过提供者直接登录。
该包还有一个 JSON API,您可以显示用户已登录的社交账户,并允许他们删除它们。
安装
步骤 1:使用 Composer 安装
composer require audunru/social-accounts
步骤 2:修改您的代码
首先,您必须将 HasSocialAccounts
特性添加到您的 User
模型中
use audunru\SocialAccounts\Traits\HasSocialAccounts; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use HasSocialAccounts; /** * Get user who has logged in with Google account ID 123456789 * $user = User::findBySocialAccount('google', '123456789') * * Retrieve all social accounts belonging to $user * $user->socialAccounts */ }
其次,您需要指定您要支持哪些提供者。发布配置,然后在 config/social-accounts.php
中打开并添加它们到数组中。
php artisan vendor:publish --tag=social-accounts-config
有一个名为 "providers" 的数组,您可以指定您想要的提供者
'providers' => [ // 'bitbucket', // 'facebook', // 'github', // 'gitlab', 'google', // 'linkedin', // 'twitter', ],
第三,您需要在 config/services.php
中为支持的社交登录提供者添加凭据。要使用 Google 登录,您需要将以下内容添加到 config/services.php
'google' => [ 'client_id' => env('GOOGLE_CLIENT_ID'), // Get your client ID and secret from https://console.developers.google.com 'client_secret' => env('GOOGLE_CLIENT_SECRET'), // Note: The "redirect" setting will be configured automatically. You are not required to add it to services.php yourself. If you don't want to use this package's default routes, you will need to configure it. If so, the package will use the value from services.php. // 'redirect' => '/social-accounts/login/google/callback', ],
第四,您应该在 AuthServiceProvider 的 boot 方法中调用 SocialAccounts::routes
方法。此方法将注册登录所需的路由,并注册用户检索和删除社交账户所需的 API 路由。
<?php namespace App\Providers; use audunru\SocialAccounts\SocialAccounts; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; class AuthServiceProvider extends ServiceProvider { /** * Register any authentication / authorization services. */ public function boot() { $this->registerPolicies(); SocialAccounts::routes(); } }
可选:您可以在单独的步骤中添加 Web 和 API 路由。
SocialAccounts::routes( function ($router) { $router->forWeb(); } ); SocialAccounts::routes( function ($router) { $router->forApi(); } );
步骤 3:配置和自定义
您可以在 config/social-accounts.php 中找到所有选项的配置和文档。
步骤 4:运行迁移
php artisan vendor:publish --tag=social-accounts-migrations php artisan migrate
迁移将创建一个 social_accounts
表,其中将存储所有添加的社交账户。
如果您将 config/social-accounts.php
中的 "automatically_create_users" 选项设置为 true
,则您的 users
表中的 email
和 password
列将被设置为可空。并非所有提供者都要求用户必须具有电子邮件地址,并且 password
列必须是可空的,因为以这种方式注册的用户将没有密码。
使用方法
为现有用户添加社交登录
如果您想允许现有用户使用 Google 登录,请将链接添加到 /social-accounts/login/google
中的某个位置
@auth <a href="/social-accounts/login/google">Add Google login to my account</a> @endauth
点击此链接后,用户将被重定向到 Google,在那里他们必须授权请求。之后,他们将返回您的应用。然后,将添加一个新的 SocialAccount
作为 User
的相关模型。
注册用户
如果您想允许用户使用此包进行注册,您必须首先发布配置文件,然后将 automatically_create_users
设置为 true
。
'automatically_create_users' => true,
然后,运行迁移以使电子邮件和密码列可空。
php artisan migrate
然后添加到 /social-accounts/login/google
的链接
<a href="/social-accounts/login/google">Sign up with Google</a>
登录
对于未登录的用户,只需添加到 /social-accounts/login/google
的链接即可
<a href="/social-accounts/login/google">Sign in with Google</a>
记住我
将 "remember" 添加到登录 URL 以保持用户登录状态
<a href="/social-accounts/login/google?remember">Sign in with Google</a>
API
JSON API,默认情况下在/social-accounts
处可访问,允许认证用户获取他们的社交账户并删除它们。
要获取社交账户数组,请向/social-accounts
发送GET请求。
要获取单个社交账户,请向/social-accounts/123
发送GET请求,其中123是账户的ID。
要删除社交账户,请向/social-accounts/123
发送DELETE请求。
用户不能通过API更新社交账户,他们必须先删除它们,然后再次授权。
可选参数、访问范围和无状态认证
要在请求中包含可选参数,请在对外观对象调用registerProviderSettings
方法。该方法接受三个参数,提供者名称、在提供者对象上调用的方法名称以及参数数组。
例如,您可以使用此功能将Google用户可以选择的登录域限制为一个
SocialAccounts::registerProviderSettings('google', 'with', ['hd' => 'seinfeld.com']);
有关可选参数、访问范围和无状态认证的更多信息,请参阅Socialite的文档
大门
您可以使用大门来允许或拒绝某些操作。应在您的AuthServiceProvider的boot方法中定义大门。
<?php namespace App\Providers; use App\User; use audunru\SocialAccounts\SocialAccounts; use Laravel\Socialite\Contracts\User as ProviderUser; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; class AuthServiceProvider extends ServiceProvider { /** * Register any authentication / authorization services. */ public function boot() { $this->registerPolicies(); /* * If your company uses G Suite and you want to ensure that only employees can log in, you can define a "login-with-provider" gate. * * If you don't define this gate, any ProviderUser is allowed to pass through. */ Gate::define('login-with-provider', function (?User $user, ProviderUser $providerUser) { /* * $providerUser->user['hd'] contains the domain name the Google account belongs to. * * It's good practice to verify that the account does in fact belong to your company after the user has authorized with Google and returned to your application. */ return 'company.com' === $providerUser->user['hd']; }); /* * If you want to restrict who can add social accounts, you can define a "add-social-account" gate. * * If you don't define this gate, any authenticated user can add a social account. */ Gate::define('add-social-account', function (User $user, ProviderUser $providerUser) { /* * isAdmin() is a hypothetical method you could define on your User model. * * In this case, only administrators would be allowed to add social accounts. */ return $user->isAdmin(); }); SocialAccounts::routes(); } }
事件
当用户在通过提供者认证后创建时,会触发一个SocialUserCreated
事件。该事件接收用户、社交账户和提供者用户模型。
当社交账户添加到现有用户时,会触发一个SocialAccountAdded
事件。该事件接收用户、社交账户和提供者用户模型。
例如,您可以监听事件并获取更多有关用户的详细信息。
<?php namespace App\Listeners; use audunru\SocialAccounts\Events\SocialUserCreated; class AddUserAvatar { /** * Handle the event. * * @param SocialUserCreated $event * @return void */ public function handle(SocialUserCreated $event) { /* * The package only saves the user's name and email when creating a new user. * * By listening for the event, we can grab more details about the user. */ $event->user->update([ 'avatar' => $event->providerUser->getAvatar(); ]); } }
替代方案
开发
测试
运行测试
composer test