2016-01-15 45 views
1

我已經做了多重驗證,但是我最終的代碼有問題。我有這樣的代碼如何在Laravel 5.2中創建多重身份驗證

php artisan make:auth 

它會生成用戶表的基本登錄/註冊路由,視圖和控制器。

爲簡單起見,將用戶表製作爲管理員表。

控制器對於管理員 應用程序/ HTTP /控制器/ AdminAuth/AuthController 應用程序/ HTTP /控制器/ AdminAuth/PasswordController :

(注意我剛剛從應用程序/ HTTP /控制器/認證/ AuthController這裏複製這些文件)

配置/ auth.php

//Authenticating guards 
'guards' => [ 
    'user' =>[ 
     'driver' => 'session', 
     'provider' => 'user', 
    ], 
    'admin' => [ 
     'driver' => 'session', 
     'provider' => 'admin', 
    ], 
], 

//User Providers 
'providers' => [ 
    'user' => [ 
     'driver' => 'eloquent', 
     'model' => App\User::class, 
    ], 
    'admin' => [ 
     'driver' => 'eloquent', 
     'model' => App\Admin::class, 
    ] 
], 

//Resetting Password 
'passwords' => [ 
    'clients' => [ 
     'provider' => 'client', 
     'email' => 'auth.emails.password', 
     'table' => 'password_resets', 
     'expire' => 60, 
    ], 
    'admins' => [ 
     'provider' => 'admin', 
     'email' => 'auth.emails.password', 
     'table' => 'password_resets', 
     'expire' => 60, 
    ], 
], 

route.php

Route::group(['middleware' => ['web']], function() { 
    //Login Routes... 
    Route::get('/admin/login','AdminAuth\[email protected]'); 
    Route::post('/admin/login','AdminAuth\[email protected]'); 
    Route::get('/admin/logout','AdminAuth\[email protected]'); 

    // Registration Routes... 
    Route::get('admin/register', 'AdminAuth\[email protected]'); 
    Route::post('admin/register', 'AdminAuth\[email protected]'); 

    Route::get('/admin', '[email protected]'); 

}); 

AdminAuth/AuthController.php

添加兩種方法,並指定$ redirectTo和$後衛

protected $redirectTo = '/admin'; 
protected $guard = 'admin'; 
public function showLoginForm() 
{ 
    if (view()->exists('auth.authenticate')) { 
     return view('auth.authenticate'); 
    } 

    return view('admin.auth.login'); 
} 
public function showRegistrationForm() 
{ 
    return view('admin.auth.register'); 
} 

它會幫助你打開另一登錄表單的管理

爲管理員創建一箇中間件

class RedirectIfNotAdmin 
{ 
/** 
* Handle an incoming request. 
* 
* @param \Illuminate\Http\Request $request 
* @param \Closure $next 
* @param string|null $guard 
* @return mixed 
*/ 
public function handle($request, Closure $next, $guard = 'admin') 
{ 
    if (!Auth::guard($guard)->check()) { 
     return redirect('/'); 
    } 

    return $next($request); 
} 
} 

kernel.php中的註冊中間件

protected $routeMiddleware = [ 
    'admin' => \App\Http\Middleware\RedirectIfNotAdmin::class, 
]; 
use this middleware in AdminController e.g., 
namespace App\Http\Controllers; 

use Illuminate\Http\Request; 

use App\Http\Requests; 
use App\Http\Controllers\Controller; 
use Illuminate\Support\Facades\Auth; 

class AdminController extends Controller 
{ 
    public function __construct(){ 
     $this->middleware('admin'); 
    } 
public function index(){ 
     return view('admin.dashboard'); 
    } 
} 

這段代碼是什麼意思Auth::guard('admin')->user()?我在哪裏輸入該代碼?

回答

1

這段代碼的含義是什麼Auth :: guard('admin') - > user()?

在簡單的一句話,驗證::後衛(「管理員」) - >用戶()的時候,你需要獲得的登錄用戶的詳細信息使用。但是,在多身份驗證系統中,可以有兩個登錄用戶(管理員/客戶端)。所以你需要指定你想得到的用戶。所以通過後衛('admin'),你告訴從admin表中獲取用戶。

我在哪裏輸入該代碼?

從答案,你可以明白,你必須在哪裏使用它。但我仍然可以用例子來解釋。假設有多個管理員。每個人都可以批准用戶請求(如帖子/評論等)。因此,當管理員批准任何請求時,要將該管理員的id插入批准的帖子列中,您必須使用此行。