stechstudio/laravel-socialite-auth
Composer 安装命令:
composer require stechstudio/laravel-socialite-auth
包简介
Use socialite as a Laravel auth driver.
README 文档
README
This package provides quick wiring and simple configuration for the most common ways in which you may wish to integrate Laravel Socialite into your application. In addition it extends that authentication flow, enabling further logic to verify that a user meets your unique requirements before logging in.
Currently supports:
- Use as the primary and only authentication for your application.
- Route-specific authentication alongside Laravel's own Auth scaffolding or other authentication drivers.
- Use for authentication into Laravel Nova in addition to or separate from front-end app authentication.
Not yet provided
- OAuth-only authentication with Socialite, not requiring users to be present in a Laravel user provider.
- Use with multiple Socialite providers (eg. Google and Facebook).
- Use of different providers for different routes (eg. Facebook for frontend, Google for Nova).
Getting started
-
Install the composer package
composer require stechstudio/laravel-socialite-auth
-
Configure the Socialite provider you wish to use in
config/services.php, specifying the redirect path as either the named route ofsocialite-auth.callbackor the relative path/socialite-auth/callback. For example:'google' => [ 'client_id' => env('GOOGLE_CLIENT_ID'), 'client_secret' => env('GOOGLE_CLIENT_SECRET'), 'redirect' => route('socialite-auth.callback') ],
-
Implement this package's
SocialiteAuthenticatableInterface on each user class in your application that corresponds to a Socialite user. These two methods return the field and value which will be compared to the user returned by Socialite.public function getSocialiteIdentifierName() { return 'email'; } public function getSocialiteIdentifier() { return $this->email; }
By default these values will be compared to the
emailattribute returned from socialite. Below you may configure this and other options. -
Beyond matching a Socialite user to one in your database, you may optionally provide a closure to ensure any additional requirements are met. Provide whatever logic you require by using this package's facade within your AppServiceProvider's
boot()method as follows:SocialiteAuth::beforeLogin(function($user) { return str_contains($user->email, ['example.com']); });
Configuration
After publishing this package's configration, the following options will be available to you in config/socialite-auth.php.
| Field | Description |
|---|---|
| driver | The OAuth service to be used by Socialite. The corresponding credentials must be provided in your services config. The default is google. |
| provider | The user provider which contains the users you are authenticating with Socialite. Default is users |
| middleware | Additional middleware to apply to the OAuth routes. Default is web. |
| field | The socialite field which will be compared to the value returned by the SocialiteAuthenticatable interface. Default is email. |
Usage
Use as primary authentication
Remove any existing authentication routes, such as those added by Auth::routes(). Then in config/auth.php, set the default guard to socialite
'defaults' => [ 'guard' => 'socialite', ... ],
Mixed authentication
-
This package includes its own Authenticate middleware which can be applied to perform the correct redirect in a mixed authentication environment. First add it to your route middleware in
app/Http/Kernel.php.protected $routeMiddleware = [ ... 'socialite' => \STS\SocialiteAuth\Authenticate::class, ... ];
-
Further down, add it above the default Authenticate middleware in the priority array to ensure it takes priority over the default when applied.
protected $middlewarePriority = [ ... \STS\SocialiteAuth\Authenticate::class, \App\Http\Middleware\Authenticate::class, ... ];
Now you may freely specify Socialite authentication on some routes...
Route::get(...)->middleware(['auth:socialite', 'socialite']);
...and in-app authentication on others.
Route::get(...)->middleware('auth');
Laravel Nova authentication
Finally, it's quick to set up socialite authentication with Laravel Nova.
- In the NovaServiceProvider added within your Providers directory, disable the
authentication related routes which are generated by default.
protected function routes() { Nova::routes(); // ->withAuthenticationRoutes() // ->withPasswordResetRoutes() // ->register(); }
- If Socialite is configured as your default guard as above, you're all set.
Otherwise add a
NOVA_GUARDvalue to your .env file indicating thatsocialiteis your desired guard.NOVA_GUARD=socialite
stechstudio/laravel-socialite-auth 适用场景与选型建议
stechstudio/laravel-socialite-auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 294 次下载、GitHub Stars 达 6, 最近一次更新时间为 2019 年 02 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「stechstudio」 「laravel-socialite-auth」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 stechstudio/laravel-socialite-auth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 stechstudio/laravel-socialite-auth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 stechstudio/laravel-socialite-auth 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A fast and simple streaming zip file downloader for Laravel.
A Laravel SDK for the HubSpot CRM Api
Assists with connecting to various cloud storage services and pushing files
This is my package laravel-shuttle
Use socialite as a Laravel auth driver.
统计信息
- 总下载量: 294
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-02-22