本文介紹了Laravel 5.2 需要一個實現默認身份驗證驅動程序/“多重身份驗證"的示例.現在需要做很多工作的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
身份驗證驅動程序/多重身份驗證"
Authentication Drivers / "Multi-Auth"
在 laravel 5.2 發布之前,據說多重身份驗證支持開箱即用.但是沒有任何示例代碼顯示如何使用不同的驅動程序進行身份驗證和路由.所以我需要幫助使用默認的 laravel 5.2 設置多重身份驗證
as prior to release of laravel 5.2 it is stated that multi auth suppots out of the box. but there is no any example codes showing how to authenticate using different drivers with routes. So I need help setting up the multi-auth using default laravel 5.2
推薦答案
創建兩個新模型:AppAdmin
和 AppUser
.更新config/auth.php
:
Create two new models: AppAdmin
and AppUser
. Update config/auth.php
:
return [
'defaults' => [
'guard' => 'user',
'passwords' => 'user',
],
'guards' => [
'user' => [
'driver' => 'session',
'provider' => 'user',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
],
'providers' => [
'user' => [
'driver' => 'eloquent',
'model' => 'AppUser',
],
'admin' => [
'driver' => 'eloquent',
'model' => 'AppAdmin',
],
],
'passwords' => [
'user' => [
'provider' => 'user',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
'admin' => [
'provider' => 'admin',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
]
]
];
在 kernel.php 中
In kernel.php
protected $middleware = [
IlluminateFoundationHttpMiddlewareCheckForMaintenanceMode::class,
IlluminateSessionMiddlewareStartSession::class,
IlluminateViewMiddlewareShareErrorsFromSession::class
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
AppHttpMiddlewareEncryptCookies::class,
IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
//AppHttpMiddlewareVerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
],
];
在Route.php中設置下面的代碼和測試
and in Route.php set below code and test
Route::get('/login', function() {
$auth = auth()->guard('admin');
$credentials = [
'email' => 'admin@gmail.com',
'password' => 'password',
];
if ($auth->attempt($credentials)) {
return redirect('/profile');
}
});
Route::get('/profile', function() {
if(auth()->guard('admin')->check()){
print_r(auth()->guard('admin')->user()->toArray());
}
if(auth()->guard('user')->check()){
print_r(auth()->guard('user')->user()->toArray());
}
});
這篇關于Laravel 5.2 需要一個實現默認身份驗證驅動程序/“多重身份驗證"的示例.現在需要做很多工作的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!