ghi / laravel-intranet-auth
Laravel 与 Hermes 集团内部网认证集成包
Requires
- ghi/core-domain: ^1.0
- laravel/framework: ^5.1
This package is not auto-updated.
Last update: 2024-09-14 17:32:10 UTC
README
安装
首先,通过 composer 安装此包。
"require": { "ghi/laravel-intranet-auth": "~1.0" }
如果你使用 Laravel 5.*,在 config/app.php
中包含服务提供者。
'providers' => [ Ghi\IntranetAuth\IntranetAuthServiceProvider::class ];
配置
认证驱动
需要在 config/auth.php
中更改 driver
键。
'driver' => 'ghi-intranet',
认证模型
Laravel 使用 app/User
模型进行认证,你也可以继续使用此模型,只需将 AuthenticatableUser
trait 改为 AuthenticatableIntranetUser
。
// app/Model.php use Ghi\Core\App\Auth\AuthenticatableIntranetUser; class User extends Model implements AuthenticatableContract, CanResetPasswordContract { use AuthenticatableIntranetUser, CanResetPassword; // }
此包包含一个代表 Ghi 内部网用户的 User
模型。如果需要此模型的最小功能,可以使用它来避免配置 Laravel 内置的模型。该模型已预配置,可直接用于内部网用户。
要使用它,只需在 config/auth.php
中更改 model
键。
'model' => Ghi\Core\Models\User::class,
使用方法
控制器
然后在你的认证控制器中,将 AuthenticatesAndRegistersUsers
trait 替换为 AuthenticatesIntranetUsers
// app/Http/Controllers/AuthController.php use Ghi\IntranetAuth\AuthenticatesIntranetUsers; class AuthController extends Controller { use AuthenticatesIntranetUsers, ThrottlesLogins; // }
此 trait 包含预定义的 postLogin
和 getLogout
方法,用于认证和注销。
你可以自定义用户认证成功后将被重定向的路由。只需在认证控制器中添加此属性
protected $redirectPath = '/home';
视图
此包包含一个预定义的视图,其中包含用于登录所需字段的表单,并且该视图已优化并准备好与 bootstrap 一起使用。
要使用它,只需在 resources/views/auth
中创建 login.blade.php
视图,并在其中包含以下内容
@include('ghi::login')
此视图中的表单包含 3 个字段
- 用户名
- 密码
- 记住我
这些数据将被发送到你的认证控制器(AuthController)。
如果你需要对此视图进行任何更改,可以使用 artisan 发布它
php artisan vendor:publish
这将复制包含在包中的 login.blade.php
视图到 resources/views/vendor/ghi
,以便你可以进行必要的调整。
路由
最后,在 app/Http/routes.php
中定义认证路由
Route::get('auth/login', [ 'as' => 'auth.login', 'uses' => 'Auth\AuthController@getLogin' ]); Route::post('auth/login', [ 'as' => 'auth.login', 'uses' => 'Auth\AuthController@postLogin' ]); Route::get('auth/logout', [ 'as' => 'auth.logout', 'uses' => 'Auth\AuthController@getLogout' ]);