mhd-euro-flash/laravel-social-auth-support-laravel-9
Composer 安装命令:
composer require mhd-euro-flash/laravel-social-auth-support-laravel-9
包简介
Easy social auth integration with a lot of available providers
README 文档
README
Social Authentication
This package give ability to
- Sign In
- Sign Up
- Attach/Detach social network provider to an existing account
This package based on laravel/socialite and provide an easy ability of usage a lot of additional providers from Socialite Providers.
Install
Via Composer:
$ composer require mad-web/laravel-social-auth
and add the service provider in config/app.php file:
'providers' => [ // ... MadWeb\SocialAuth\SocialAuthServiceProvider::class, ];
Next, publish the migration with:
$ php artisan vendor:publish --provider="MadWeb\SocialAuth\SocialAuthServiceProvider" --tag="migrations"
The package assumes that your users table name is called "users". If this is not the case you should manually edit the published migration to use your custom table name.
After the migration has been published you can create the social_providers table for storing supported
providers and user_has_social_provider pivot table for attaching providers to users by running migrations:
$ php artisan migrate
You can publish the config file with:
$ php artisan vendor:publish --provider="MadWeb\SocialAuth\SocialAuthServiceProvider" --tag="config"
This is the contents of the published config/social-auth.php config file:
return [ /* |-------------------------------------------------------------------------- | Additional service providers |-------------------------------------------------------------------------- | | The social providers listed here will enable support for additional social | providers which provided by https://socialiteproviders.github.io/ just | add new event listener from the installation guide | */ 'providers' => [ // ], 'models' => [ /* * When using the "UserSocialite" trait from this package, we need to know which * Eloquent model should be used to retrieve your available social providers. Of course, it * is often just the "SocialProvider" model but you may use whatever you like. */ 'social' => \MadWeb\SocialAuth\Models\SocialProvider::class, /* * User model which you will use as "SocialAuthenticatable" */ 'user' => \App\User::class, ], 'table_names' => [ /* |-------------------------------------------------------------------------- | Users Table |-------------------------------------------------------------------------- | | The table for storing relation between users and social providers. Also there is | a place for saving "user social network id", "token", "expiresIn" if it exist | */ 'user_has_social_provider' => 'user_has_social_provider', /* |-------------------------------------------------------------------------- | Social Providers Table |-------------------------------------------------------------------------- | | The table that contains all social network providers which your application use. | */ 'social_providers' => 'social_providers' ], 'foreign_keys' => [ /* * The name of the foreign key to the users table. */ 'users' => 'user_id', /* * The name of the foreign key to the socials table */ 'socials' => 'social_id' ], /* |-------------------------------------------------------------------------- | Authentication redirection |-------------------------------------------------------------------------- | | Redirect path after success/error login via social network | */ 'redirect' => '/home' ];
Or you can publish and modify view templates with:
$ php artisan vendor:publish --provider="MadWeb\SocialAuth\SocialAuthServiceProvider" --tag="views"
Also you can publish and modify translation file:
$ php artisan vendor:publish --provider="MadWeb\SocialAuth\SocialAuthServiceProvider" --tag="lang"
Add credetials to your project
Add providers to config/services.php:
'facebook' => [ 'client_id' => env('FB_ID'), 'client_secret' => env('FB_SECRET'), 'redirect' => env('FB_REDIRECT'), ], 'google' => [ 'client_id' => env('GOOGLE_ID'), 'client_secret' => env('GOOGLE_SECRET'), 'redirect' => env('GOOGLE_REDIRECT'), ], 'github' => [ 'client_id' => env('GITHUB_ID'), 'client_secret' => env('GITHUB_SECRET'), 'redirect' => env('GITHUB_REDIRECT'), ]
Add credantials to .env:
FB_ID= FB_SECRET= FB_REDIRECT=https://app.domain/social/facebook/callback GOOGLE_ID= GOOGLE_SECRET= GOOGLE_REDIRECT=https://app.domain/social/google/callback GITHUB_ID= GITHUB_SECRET= GITHUB_REDIRECT=https://app.domain/social/github/callback
After that, create your social providers in the database.
Using console command:
php artisan social-auth:add google --label=Google+
By model, for example in seeder:
SocialProvider::create(['slug' => 'google', 'label' => 'Google+']);
Or add records directly.
You can add additional scopes and parameters to the social auth request:
SocialProvider::create([ 'label' => 'github', 'slug' => 'Github', 'scopes' => ['foo', 'bar'], 'parameters' => ['foo' => 'bar'] ]);
To override default scopes:
$SocialProvider->setScopes(['foo', 'bar'], true);
Include social buttons into your templates
@include('social-auth::attach') // for authenticated user to attach/detach another socials @include('social-auth::buttons') // for guests to login via
Prepare your user model
Implement SocialAuthenticatable interface and add UserSocialite trait to your User model:
namespace App\Models; use Illuminate\Database\Eloquent\Model; use MadWeb\SocialAuth\Traits\UserSocialite; use MadWeb\SocialAuth\Contracts\SocialAuthenticatable; class User extends Model implements SocialAuthenticatable { use UserSocialite; ... }
Additional Providers
To use any of additional providers from socialiteproviders.netlify.com, at first install it:
composer require socialiteproviders/instagram
next, add an event listener from guide to the social-auth config file:
/* |-------------------------------------------------------------------------- | Additional service providers |-------------------------------------------------------------------------- | | The social providers listed here will enable support for additional social | providers which provided by https://socialiteproviders.netlify.com just | add new event listener from the installation guide | */ 'providers' => [ SocialiteProviders\Instagram\InstagramExtendSocialite::class, ], ...
Customization
Routes
If you need do some custom with social flow, you should define yourself controllers and put your custom url into routes file.
For example:
Route::get('social/{social}', 'Auth\SocialAuthController@getAccount'); Route::get('social/{social}/callback', 'Auth\SocialAuthController@callback'); Route::get('social/{social}/detach', 'Auth\SocialAuthController@detachAccount');
In case if you no need any special functionality ypu can use our default controllers.
Custom User Model
User model we takes from the social-auth.models.user.
User Properties Mapping
SocialAuthenticatable interface contains method mapSocialData for mapping social fields for user model.
If you need customize a data mapping, you can override this method for your preferences project in the User model.
Default mapping method:
public function mapSocialData(User $socialUser) { $raw = $socialUser->getRaw(); $name = $socialUser->getName() ?? $socialUser->getNickname(); $name = $name ?? $socialUser->getEmail(); $result = [ $this->getEmailField() => $socialUser->getEmail(), 'name' => $name, 'verified' => $raw['verified'] ?? true, 'avatar' => $socialUser->getAvatar(), ]; return $result; }
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CONDUCT for details.
Security
If you discover any security related issues, please email madweb.dev@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
mhd-euro-flash/laravel-social-auth-support-laravel-9 适用场景与选型建议
mhd-euro-flash/laravel-social-auth-support-laravel-9 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 848 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 12 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「login」 「auth」 「social」 「laravel」 「signin」 「socialite」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mhd-euro-flash/laravel-social-auth-support-laravel-9 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mhd-euro-flash/laravel-social-auth-support-laravel-9 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mhd-euro-flash/laravel-social-auth-support-laravel-9 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
WeChat OAuth SDK
Laravel Multiauth package
Simple Sharing generates social media share links within CP entry pages, allowing you to quickly & easily share entries.
Debugging a problem and need to login as one of your customers? This allows you to authenticate as any of your customers.
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
LinkedIn integration for Social
统计信息
- 总下载量: 848
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-12-28