問(wèn)題描述
我的 Laravel 5.3 有三種不同類型的用戶.我希望他們?cè)诘卿浐蟊恢囟ㄏ虻讲煌膬x表板頁(yè)面.例如:
I have Laravel 5.3 with three different types of users. I want them to be redirected to different dashboard pages after logging in. For example:
用戶 ->登錄 ->用戶儀表板
user -> login -> user-dashboard
管理員 ->登錄 ->管理儀表板
admin -> login -> admin-dashboard
我創(chuàng)建了一個(gè)名為 CheckRole
的中間件:
I have created a middleware called CheckRole
:
public function handle($request, Closure $next)
{
if($request->user() === null) {
return response("Insufficient Permissions" , 401);
}
$actions = $request->route()->getAction();
$roles = isset($actions['roles']) ? $actions['roles'] : null;
if($request->user()->hasAnyRole($roles) || !$roles) {
return $next($request);
}
return response("Insufficient Permissions" , 401);
}
路線
Route::group(['middleware' => ['auth','roles'], 'roles' => 'Admin'], function () {
// Routes here
}
角色運(yùn)行良好.
現(xiàn)在 redirectTo='';
在 LoginContoller
中只指向一個(gè)視圖.我檢查了文檔,我相信這與沒(méi)有解釋如何設(shè)置它的守衛(wèi)有關(guān).
Now redirectTo= '';
in the LoginContoller
points to one view only. I have checked the documentation and I believe this has something to do with guards which have no explanation on how to set it up.
我也見(jiàn)過(guò)多重身份驗(yàn)證,但我認(rèn)為為不同的用戶創(chuàng)建不同的表并因此尋找替代答案是不明智的.
I have also seen multiauth, but I do not think it is wise to create different tables for different users and hence looking for an alternate answer.
任何建議將不勝感激.
我的表是這樣的:
Table users
id | name | email
---------
1 | John | john@blah.com
2 | Michael | michael@blah.com
Table roles
id | name
---------
1 | Admin
2 | PrivilegedMember
3 | Subscriber
Table user_role
id | user_id | role_id
----------------------
1 | 1 | 1
2 | 2 | 2
這可能與以下問(wèn)題重復(fù),但提供的答案沒(méi)有解釋多次重定向.
This might be a duplicate of the below question but the answer provided leaves without explaining multiple redirections.
Laravel 5.3 中的多重身份驗(yàn)證
推薦答案
在你的 LoginController
中實(shí)現(xiàn)一個(gè) authenticated()
方法并在那里添加重定向邏輯:
Implement an authenticated()
method in your LoginController
and add the redirection logic there:
<?php
namespace AppHttpControllersAuth;
use AppHttpControllersController;
use IlluminateFoundationAuthAuthenticatesUsers;
class LoginController extends Controller
{
use AuthenticatesUsers;
// ...
/**
* The user has been authenticated.
*
* @param IlluminateHttpRequest $request
* @param mixed $user
*
* @return mixed
*/
protected function authenticated(Request $request, $user)
{
if($user->hasRole('Admin')) {
return redirect()->intended('admin');
}
if ($user->hasRole('PrivilegedMember')) {
return redirect()->intended('PriviligedMember/index');
}
}
// ...
}
該方法在用戶通過(guò)身份驗(yàn)證后調(diào)用.查看sendLoginResponse
的最后兩行:
The method is called after the user is authenticated. See the last two lines of sendLoginResponse
:
/**
* Send the response after the user was authenticated.
*
* @param IlluminateHttpRequest $request
*
* @return IlluminateHttpResponse
*/
protected function sendLoginResponse(Request $request)
{
$request->session()->regenerate();
$this->clearLoginAttempts($request);
return $this->authenticated($request, $this->guard()->user())
?: redirect()->intended($this->redirectPath());
}
因此它是此類邏輯的完美候選.
So it's a perfect candidate for such logics.
關(guān)于您自己的答案的另一個(gè)注意事項(xiàng),AuthenticatesUser
是水平擴(kuò)展 LoginController
的特征,您可以安全地覆蓋控制器中的任何方法,而無(wú)需觸及核心文件.
One other note on your own answer, the AuthenticatesUser
is a trait that horizontally extends the LoginController
, you can safely override any of its methods in your controller without touching the core files.
這篇關(guān)于Laravel 5.3 登錄重定向到多個(gè)用戶的不同頁(yè)面的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!